From 17051ece2c06bdfe821c7d0a258295abe1bb37fa Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 13 Apr 2017 18:33:46 -0700 Subject: [PATCH] [WIP] no-semi comments (#1257) It doesn't yet work but I think it's close --- src/comments.js | 4 +- src/printer.js | 17 +++- .../no-semi/__snapshots__/jsfmt.spec.js.snap | 89 ++++++++++++++++++- tests/no-semi/comments.js | 11 +++ 4 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 tests/no-semi/comments.js diff --git a/src/comments.js b/src/comments.js index 5d891374a..53010eec5 100644 --- a/src/comments.js +++ b/src/comments.js @@ -891,7 +891,7 @@ function printDanglingComments(path, options, sameIndent) { return indent(concat([hardline, join(hardline, parts)])); } -function printComments(path, print, options) { +function printComments(path, print, options, needsSemi) { var value = path.getValue(); var parent = path.getParentNode(); var printed = print(path); @@ -902,7 +902,7 @@ function printComments(path, print, options) { } var leadingParts = []; - var trailingParts = [printed]; + var trailingParts = [needsSemi ? ";" : "", printed]; path.each(function(commentPath) { var comment = commentPath.getValue(); diff --git a/src/printer.js b/src/printer.js index 84d58d146..9199efe36 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2006,10 +2006,20 @@ function printStatementSequence(path, options, print) { // in no-semi mode, prepend statement with semicolon if it might break ASI if (!options.semi && !isClass && stmtNeedsASIProtection(stmtPath)) { - parts.push(";"); + if ( + stmt.comments && + stmt.comments.some(comment => comment.leading) + ) { + // Note: stmtNeedsASIProtection requires stmtPath to already be printed + // as it reads needsParens which is mutated on the instance + parts.push(print(stmtPath, { needsSemi: true })); + } else { + parts.push(";", stmtPrinted); + } + } else { + parts.push(stmtPrinted); } - parts.push(stmtPrinted); if (!options.semi && isClass) { if (classPropMayCauseASIProblems(stmtPath)) { @@ -3572,7 +3582,8 @@ function printAstToDoc(ast, options) { return comments.printComments( path, p => genericPrint(p, options, printGenerically, args), - options + options, + args && args.needsSemi ); } diff --git a/tests/no-semi/__snapshots__/jsfmt.spec.js.snap b/tests/no-semi/__snapshots__/jsfmt.spec.js.snap index 431ed42ce..e85646e42 100644 --- a/tests/no-semi/__snapshots__/jsfmt.spec.js.snap +++ b/tests/no-semi/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,86 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`comments.js 1`] = ` +let error = new Error(response.statusText); +// comment +(error: any).response = response + +x; + +/* comment */ (error: any).response = response + +x; + +(error: any).response = response; /* comment */ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let error = new Error(response.statusText); +// comment +(error: any).response = response; + +x; + +/* comment */ (error: any).response = response; + +x; + +(error: any).response = response; /* comment */ + +`; + +exports[`comments.js 2`] = ` +let error = new Error(response.statusText); +// comment +(error: any).response = response + +x; + +/* comment */ (error: any).response = response + +x; + +(error: any).response = response; /* comment */ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let error = new Error(response.statusText) +// comment +;(error: any).response = response + +x + +/* comment */ ;(error: any).response = response + +x + +;(error: any).response = response /* comment */ + +`; + +exports[`comments.js 3`] = ` +let error = new Error(response.statusText); +// comment +(error: any).response = response + +x; + +/* comment */ (error: any).response = response + +x; + +(error: any).response = response; /* comment */ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let error = new Error(response.statusText) +// comment +;(error: any).response = response + +x + +/* comment */ ;(error: any).response = response + +x + +;(error: any).response = response /* comment */ + +`; + exports[`no-semi.js 1`] = ` // with preexisting semi @@ -591,9 +672,9 @@ if (true) { ;(() => {})() } -;// flow +// flow -(x: void) +;(x: void) ;(y: void) // check statement clauses @@ -913,9 +994,9 @@ if (true) { ;(() => {})() } -;// flow +// flow -(x: void) +;(x: void) ;(y: void) // check statement clauses diff --git a/tests/no-semi/comments.js b/tests/no-semi/comments.js new file mode 100644 index 000000000..34f27c796 --- /dev/null +++ b/tests/no-semi/comments.js @@ -0,0 +1,11 @@ +let error = new Error(response.statusText); +// comment +(error: any).response = response + +x; + +/* comment */ (error: any).response = response + +x; + +(error: any).response = response; /* comment */