From 2a0e7b575caee6f25aa9ee43189ca427e5361003 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Sun, 22 Jan 2017 20:41:31 -0600 Subject: [PATCH] Ensure computed method names don't lose quotes (#419) --- src/printer.js | 15 +++++++++------ .../__snapshots__/jsfmt.spec.js.snap | 12 ++++++++++++ tests/computed_props/classes.js | 3 +++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 tests/computed_props/classes.js diff --git a/src/printer.js b/src/printer.js index 8b197e5b..c7c2873e 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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"); } diff --git a/tests/computed_props/__snapshots__/jsfmt.spec.js.snap b/tests/computed_props/__snapshots__/jsfmt.spec.js.snap index 518241ff..b8d4c32a 100644 --- a/tests/computed_props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/computed_props/__snapshots__/jsfmt.spec.js.snap @@ -1,3 +1,15 @@ +exports[`test classes.js 1`] = ` +"class c { + [\"i\"]() {} +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class c { + [\"i\"]() { + } +} +" +`; + exports[`test test.js 1`] = ` "var ColorId = { RED: \'R\', diff --git a/tests/computed_props/classes.js b/tests/computed_props/classes.js new file mode 100644 index 00000000..d85ace1a --- /dev/null +++ b/tests/computed_props/classes.js @@ -0,0 +1,3 @@ +class c { + ["i"]() {} +}