Merge pull request #90 from bakkot/property-names

Fix #79: omit unnecessary quotes in property names
master
James Long 2017-01-12 09:11:36 -05:00 committed by GitHub
commit 1ccf6b0344
2 changed files with 30 additions and 15 deletions

View File

@ -16,6 +16,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",
"get-stdin": "^5.0.1",
"minimist": "^1.2.0",

View File

@ -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);
@ -643,13 +644,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"));
}
@ -1157,15 +1159,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);
@ -1610,6 +1615,15 @@ function printStatementSequence(path, options, print) {
return join(hardline, printed);
}
function printPropertyKey(path, print) {
var node = path.getNode().key;
if (node.type === "StringLiteral" && isIdentifierName(node.value)) {
// 'a' -> a
return node.value;
}
return path.call(print, "key");
}
function printMethod(path, options, print) {
var node = path.getNode();
var kind = node.kind;
@ -1635,7 +1649,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, "]" ]);
@ -1775,7 +1789,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, "]");