From 0241d97b7783276e34b44234d8b1858ba14eb0b4 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 22 Feb 2017 14:43:29 -0800 Subject: [PATCH] Do not attach comments to EmptyStatements in try/catch (#763) We skip EmptyStatement when generating the list of preceding/enclosing/following nodes but didn't do the same for the exceptions. Fixes #695 --- src/comments.js | 5 +++-- tests/try/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/try/jsfmt.spec.js | 1 + tests/try/try.js | 4 ++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/try/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/try/jsfmt.spec.js create mode 100644 tests/try/try.js diff --git a/src/comments.js b/src/comments.js index 4897381e..54be8dd8 100644 --- a/src/comments.js +++ b/src/comments.js @@ -291,10 +291,11 @@ function addTrailingComment(node, comment) { } function addBlockStatementFirstComment(node, comment) { - if (node.body.length === 0) { + const body = node.body.filter(n => n.type !== "EmptyStatement"); + if (body.length === 0) { addDanglingComment(node, comment); } else { - addLeadingComment(node.body[0], comment); + addLeadingComment(body[0], comment); } } diff --git a/tests/try/__snapshots__/jsfmt.spec.js.snap b/tests/try/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..089c262f --- /dev/null +++ b/tests/try/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`try.js 1`] = ` +"try +/* missing comment */ +{;} +finally {} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +try { + /* missing comment */ +} finally { +} +" +`; diff --git a/tests/try/jsfmt.spec.js b/tests/try/jsfmt.spec.js new file mode 100644 index 00000000..989047bc --- /dev/null +++ b/tests/try/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname); diff --git a/tests/try/try.js b/tests/try/try.js new file mode 100644 index 00000000..8bc21ad0 --- /dev/null +++ b/tests/try/try.js @@ -0,0 +1,4 @@ +try +/* missing comment */ +{;} +finally {}