From 69879e23bc84ae1a136d4becb55caeb85b4f6cc1 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Wed, 5 Apr 2017 17:10:10 -0300 Subject: [PATCH] Print comment after key (#1131) --- src/printer.js | 5 ++++- .../__snapshots__/jsfmt.spec.js.snap | 19 +++++++++++++++++++ tests/object_property_comment/after-key.js | 7 +++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/object_property_comment/after-key.js diff --git a/src/printer.js b/src/printer.js index c0f6aa33..696ce605 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1985,7 +1985,10 @@ function printPropertyKey(path, options, print) { (options.parser !== "flow" || key.value.match(/[a-zA-Z0-9$_]/)) ) { // 'a' -> a - return key.value; + return path.call( + keyPath => comments.printComments(keyPath, p => key.value, options), + "key" + ); } return path.call(print, "key"); } diff --git a/tests/object_property_comment/__snapshots__/jsfmt.spec.js.snap b/tests/object_property_comment/__snapshots__/jsfmt.spec.js.snap index 85e493ef..96c3e9f2 100644 --- a/tests/object_property_comment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/object_property_comment/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`after-key.js 1`] = ` +"let a = { + a /* comment */: () => 1 +}; + +let a = { + \\"a\\" /* comment */: () => 1 +}; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let a = { + a /* comment */: () => 1 +}; + +let a = { + a /* comment */: () => 1 +}; +" +`; + exports[`comment.js 1`] = ` "o = { name: diff --git a/tests/object_property_comment/after-key.js b/tests/object_property_comment/after-key.js new file mode 100644 index 00000000..40b49c87 --- /dev/null +++ b/tests/object_property_comment/after-key.js @@ -0,0 +1,7 @@ +let a = { + a /* comment */: () => 1 +}; + +let a = { + "a" /* comment */: () => 1 +};