[WIP] no-semi comments (#1257)

It doesn't yet work but I think it's close
master
Christopher Chedeau 2017-04-13 18:33:46 -07:00 committed by GitHub
parent 088754fb1e
commit 17051ece2c
4 changed files with 112 additions and 9 deletions

View File

@ -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();

View File

@ -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
);
}

View File

@ -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

11
tests/no-semi/comments.js Normal file
View File

@ -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 */