From da7c97e0bb84aa1c01f006cac23189cfd9fbcda7 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 10 Apr 2017 11:22:35 -0700 Subject: [PATCH] Stabilize label comments (#1179) Fixes #1160 --- src/comments.js | 9 +++++++++ tests/label/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/label/comment.js | 4 ++++ 3 files changed, 27 insertions(+) create mode 100644 tests/label/comment.js diff --git a/src/comments.js b/src/comments.js index 5b8bc1a9..aa89792e 100644 --- a/src/comments.js +++ b/src/comments.js @@ -196,6 +196,7 @@ function attach(comments, ast, text, options) { handleTemplateLiteralComments(enclosingNode, comment) || handleIfStatementComments(enclosingNode, followingNode, comment) || handleClassComments(enclosingNode, comment) || + handleLabeledStatementComments(enclosingNode, comment) || handleCallExpressionComments(precedingNode, enclosingNode, comment) || handlePropertyComments(enclosingNode, comment) || handleExportNamedDeclarationComments(enclosingNode, comment) || @@ -578,6 +579,14 @@ function handleObjectPropertyComments(enclosingNode, comment) { return false; } +function handleLabeledStatementComments(enclosingNode, comment) { + if (enclosingNode && enclosingNode.type === "LabeledStatement") { + addLeadingComment(enclosingNode, comment); + return true; + } + return false; +} + function handleCallExpressionComments(precedingNode, enclosingNode, comment) { if ( enclosingNode && diff --git a/tests/label/__snapshots__/jsfmt.spec.js.snap b/tests/label/__snapshots__/jsfmt.spec.js.snap index 2be86bcb..6677c80e 100644 --- a/tests/label/__snapshots__/jsfmt.spec.js.snap +++ b/tests/label/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`comment.js 1`] = ` +"{ + inf_leave: // goto emulation + for (;;) {} +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +{ + // goto emulation + inf_leave: for (;;) { + } +} +" +`; + exports[`empty_label.js 1`] = ` "a:; b diff --git a/tests/label/comment.js b/tests/label/comment.js new file mode 100644 index 00000000..45e9363f --- /dev/null +++ b/tests/label/comment.js @@ -0,0 +1,4 @@ +{ + inf_leave: // goto emulation + for (;;) {} +}