Ensure computed method names don't lose quotes (#419)

master
Brian Ng 2017-01-22 20:41:31 -06:00 committed by Christopher Chedeau
parent 105a164423
commit 2a0e7b575c
3 changed files with 24 additions and 6 deletions

View File

@ -1587,17 +1587,20 @@ function printStatementSequence(path, options, print) {
}
function printPropertyKey(path, options, print) {
var node = path.getNode().key;
const node = path.getNode();
const key = node.key;
if (
(node.type === "StringLiteral" ||
node.type === "Literal" && typeof node.value === "string") &&
isIdentifierName(node.value) &&
(key.type === "StringLiteral" ||
key.type === "Literal" && typeof key.value === "string") &&
isIdentifierName(key.value) &&
!node.computed &&
// There's a bug in the flow parser where it throws if there are
// unquoted unicode literals as keys. Let's quote them for now.
(options.parser !== "flow" || node.value.match(/[a-zA-Z0-9$_]/))
(options.parser !== "flow" || key.value.match(/[a-zA-Z0-9$_]/))
) {
// 'a' -> a
return node.value;
return key.value;
}
return path.call(print, "key");
}

View File

@ -1,3 +1,15 @@
exports[`test classes.js 1`] = `
"class c {
[\"i\"]() {}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class c {
[\"i\"]() {
}
}
"
`;
exports[`test test.js 1`] = `
"var ColorId = {
RED: \'R\',

View File

@ -0,0 +1,3 @@
class c {
["i"]() {}
}