Format property names consistently
This commit is contained in:
1 parent
34dc37a816
commit
be8f65d23e
2 files changed
+36
-15
No files matched your search
@@ -8,6 +8,7 @@
|
||||
"dependencies": {
|
||||
"ast-types": "git+https://github.com/jlongster/ast-types.git",
|
||||
"babylon": "^6.15.0",
|
||||
"esutils": "^2.0.2",
|
||||
"flow-parser": "^0.37.0",
|
||||
"minimist": "^1.2.0",
|
||||
"private": "^0.1.6",
|
||||
|
||||
+35
-15
@@ -25,6 +25,7 @@ var isString = types.builtInTypes.string;
|
||||
var isObject = types.builtInTypes.object;
|
||||
var FastPath = require("./fast-path");
|
||||
var util = require("./util");
|
||||
var isIdentifierName = require("esutils").keyword.isIdentifierNameES6;
|
||||
|
||||
function PrintResult(code, sourceMap) {
|
||||
assert.ok(this instanceof PrintResult);
|
||||
@@ -641,13 +642,14 @@ function genericPrintNoParens(path, options, print) {
|
||||
return printMethod(path, options, print);
|
||||
}
|
||||
|
||||
if (n.computed) {
|
||||
parts.push("[", path.call(print, "key"), "]");
|
||||
if (n.shorthand) {
|
||||
parts.push(path.call(print, "value"));
|
||||
} else {
|
||||
parts.push(path.call(print, n.shorthand ? "value" : "key"));
|
||||
}
|
||||
|
||||
if (!n.shorthand) {
|
||||
if (n.computed) {
|
||||
parts.push("[", path.call(print, "key"), "]");
|
||||
} else {
|
||||
parts.push(printPropertyKey(path, print));
|
||||
}
|
||||
parts.push(": ", path.call(print, "value"));
|
||||
}
|
||||
|
||||
@@ -1155,15 +1157,18 @@ function genericPrintNoParens(path, options, print) {
|
||||
if (n.static)
|
||||
parts.push("static ");
|
||||
|
||||
var key = path.call(print, "key");
|
||||
var key;
|
||||
|
||||
if (n.computed) {
|
||||
key = concat([ "[", key, "]" ]);
|
||||
} else if (n.variance === "plus") {
|
||||
key = concat([ "+", key ]);
|
||||
} else if (n.variance === "minus") {
|
||||
key = concat([ "-", key ]);
|
||||
}
|
||||
key = concat([ "[", path.call(print, "key"), "]" ]);
|
||||
} else {
|
||||
key = printPropertyKey(path, print);
|
||||
if (n.variance === "plus") {
|
||||
key = concat([ "+", key ]);
|
||||
} else if (n.variance === "minus") {
|
||||
key = concat([ "-", key ]);
|
||||
}
|
||||
}
|
||||
|
||||
parts.push(key);
|
||||
|
||||
@@ -1608,6 +1613,21 @@ function printStatementSequence(path, options, print) {
|
||||
return join(hardline, printed);
|
||||
}
|
||||
|
||||
function printPropertyKey(path, print) {
|
||||
var node = path.getNode().key;
|
||||
if (node.type === "StringLiteral") {
|
||||
if (isIdentifierName(node.value)) { // 'a' -> a
|
||||
return node.value;
|
||||
}
|
||||
let val = parseFloat(node.value);
|
||||
let strVal = fromString(val);
|
||||
if (val >= 0 && strVal === node.value) { // '0' -> 0
|
||||
return strVal;
|
||||
}
|
||||
}
|
||||
return path.call(print, "key");
|
||||
}
|
||||
|
||||
function printMethod(path, options, print) {
|
||||
var node = path.getNode();
|
||||
var kind = node.kind;
|
||||
@@ -1633,7 +1653,7 @@ function printMethod(path, options, print) {
|
||||
parts.push(kind, " ");
|
||||
}
|
||||
|
||||
var key = path.call(print, "key");
|
||||
var key = printPropertyKey(path, print);
|
||||
|
||||
if (node.computed) {
|
||||
key = concat([ "[", key, "]" ]);
|
||||
@@ -1773,7 +1793,7 @@ function printObjectMethod(path, options, print) {
|
||||
return printMethod(path, options, print);
|
||||
}
|
||||
|
||||
var key = path.call(print, "key");
|
||||
var key = printPropertyKey(path, print);
|
||||
|
||||
if (objMethod.computed) {
|
||||
parts.push("[", key, "]");
|
||||
|
||||
Reference in new issue
Block a user