Skip to content

Commit

Permalink
[WIP] no-semi comments (#1257)
Browse files Browse the repository at this point in the history
It doesn't yet work but I think it's close
  • Loading branch information
vjeux committed Apr 14, 2017
1 parent 088754f commit 17051ec
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/comments.js
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions src/printer.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -3572,7 +3582,8 @@ function printAstToDoc(ast, options) {
return comments.printComments(
path,
p => genericPrint(p, options, printGenerically, args),
options
options,
args && args.needsSemi
);
}

Expand Down
89 changes: 85 additions & 4 deletions 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
Expand Down Expand Up @@ -591,9 +672,9 @@ if (true) {
;(() => {})()
}
;// flow
// flow
(x: void)
;(x: void)
;(y: void)
// check statement clauses
Expand Down Expand Up @@ -913,9 +994,9 @@ if (true) {
;(() => {})()
}
;// flow
// flow
(x: void)
;(x: void)
;(y: void)
// check statement clauses
Expand Down
11 changes: 11 additions & 0 deletions 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 */

0 comments on commit 17051ec

Please sign in to comment.