Print end-of-line comment for continue/break statements (#12075)

* Add handleBreakAndContinueStatementComments to handleEndOfLineComment

* Add tests

* Add changelog

* Update tests

* Add more tests

* Fix for own line comments

* Fix tests

* Fix tests

* Fix tests

* Add unstable tests
This commit is contained in:
Sosuke Suzuki authored and GitHub committed 2022-01-19 22:29:37 +09:00
1 parent 27e7376229
commit db73cc8e0c
9 files changed
+1087 -7

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
#### Print end-of-line comment for `continue`/`break` statements (#12075 by @sosukesuzuki)
<!-- Optional description if it makes sense. -->
<!-- prettier-ignore -->
```jsx
// Input
for (;;) continue // comment
;
// Prettier stable
Error: Comment "comment" was not printed. Please report this error!
// Prettier main
for (;;)
continue; // comment
```
+2
View File
@@ -68,6 +68,7 @@ function handleOwnLineComment(context) {
handleAssignmentPatternComments,
handleMethodNameComments,
handleLabeledStatementComments,
handleBreakAndContinueStatementComments,
].some((fn) => fn(context));
}
@@ -91,6 +92,7 @@ function handleEndOfLineComment(context) {
handleOnlyComments,
handleTypeAliasComments,
handleVariableDeclaratorComments,
handleBreakAndContinueStatementComments,
].some((fn) => fn(context));
}
+1
View File
@@ -39,6 +39,7 @@ const unstableTests = new Map(
["flow/no-semi/comments.js", (options) => options.semi === false],
"typescript/prettier-ignore/mapped-types.ts",
"js/comments/html-like/comment.js",
"js/for/continue-and-break-comment-without-blocks.js",
].map((fixture) => {
const [file, isUnstable = () => true] = Array.isArray(fixture)
? fixture
@@ -74,6 +74,709 @@ for (x of y);
================================================================================
`;
exports[`continue-and-break-comment-1.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
for(;;) {
continue // comment
;
}
for (;;) {
break // comment
;
}
for (const f of []) {
continue // comment
;
}
for (const f of []) {
break // comment
;
}
for (const f in {}) {
continue // comment
;
}
for (const f in {}) {
break // comment
;
}
while(true) {
continue // comment
;
}
while (true) {
break // comment
;
}
do {
continue // comment
;
} while(true);
do {
break // comment
;
} while(true);
label1: for (;;) {
continue label1 // comment
;
}
label2: {
break label2 // comment
;
};
for(;;) {
continue /* comment */
;
}
for (;;) {
break /* comment */
;
}
for (const f of []) {
continue /* comment */
;
}
for (const f of []) {
break /* comment */
;
}
for (const f in {}) {
continue /* comment */
;
}
for (const f in {}) {
break /* comment */
;
}
while(true) {
continue /* comment */
;
}
while (true) {
break /* comment */
;
}
do {
continue /* comment */
;
} while(true);
do {
break /* comment */
;
} while(true);
label1: for (;;) {
continue label1 /* comment */
;
}
label2: {
break label2 /* comment */
;
};
=====================================output=====================================
for (;;) {
continue; // comment
}
for (;;) {
break; // comment
}
for (const f of []) {
continue; // comment
}
for (const f of []) {
break; // comment
}
for (const f in {}) {
continue; // comment
}
for (const f in {}) {
break; // comment
}
while (true) {
continue; // comment
}
while (true) {
break; // comment
}
do {
continue; // comment
} while (true);
do {
break; // comment
} while (true);
label1: for (;;) {
continue label1; // comment
}
label2: {
break label2; // comment
}
for (;;) {
continue; /* comment */
}
for (;;) {
break; /* comment */
}
for (const f of []) {
continue; /* comment */
}
for (const f of []) {
break; /* comment */
}
for (const f in {}) {
continue; /* comment */
}
for (const f in {}) {
break; /* comment */
}
while (true) {
continue; /* comment */
}
while (true) {
break; /* comment */
}
do {
continue; /* comment */
} while (true);
do {
break; /* comment */
} while (true);
label1: for (;;) {
continue label1 /* comment */;
}
label2: {
break label2 /* comment */;
}
================================================================================
`;
exports[`continue-and-break-comment-2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
for(;;) {
continue
// comment
;
}
for (;;) {
break
// comment
;
}
for (const f of []) {
continue
// comment
;
}
for (const f of []) {
break
// comment
;
}
for (const f in {}) {
continue
// comment
;
}
for (const f in {}) {
break
// comment
;
}
while(true) {
continue
// comment
;
}
while (true) {
break
// comment
;
}
do {
continue
// comment
;
} while(true);
do {
break
// comment
;
} while(true);
label1: for (;;) {
continue label1
// comment
;
}
label2: {
break label2
// comment
;
};
for(;;) {
continue
/* comment */
;
}
for (;;) {
break
/* comment */
;
}
for (const f of []) {
continue
/* comment */
;
}
for (const f of []) {
break
/* comment */
;
}
for (const f in {}) {
continue
/* comment */
;
}
for (const f in {}) {
break
/* comment */
;
}
while(true) {
continue
/* comment */
;
}
while (true) {
break
/* comment */
;
}
do {
continue
/* comment */
;
} while(true);
do {
break
/* comment */
;
} while(true);
label1: for (;;) {
continue label1
/* comment */
;
}
label2: {
break label2
/* comment */
;
};
=====================================output=====================================
for (;;) {
continue;
// comment
}
for (;;) {
break;
// comment
}
for (const f of []) {
continue;
// comment
}
for (const f of []) {
break;
// comment
}
for (const f in {}) {
continue;
// comment
}
for (const f in {}) {
break;
// comment
}
while (true) {
continue;
// comment
}
while (true) {
break;
// comment
}
do {
continue;
// comment
} while (true);
do {
break;
// comment
} while (true);
label1: for (;;) {
continue label1;
// comment
}
label2: {
break label2;
// comment
}
for (;;) {
continue;
/* comment */
}
for (;;) {
break;
/* comment */
}
for (const f of []) {
continue;
/* comment */
}
for (const f of []) {
break;
/* comment */
}
for (const f in {}) {
continue;
/* comment */
}
for (const f in {}) {
break;
/* comment */
}
while (true) {
continue;
/* comment */
}
while (true) {
break;
/* comment */
}
do {
continue;
/* comment */
} while (true);
do {
break;
/* comment */
} while (true);
label1: for (;;) {
continue label1;
/* comment */
}
label2: {
break label2;
/* comment */
}
================================================================================
`;
exports[`continue-and-break-comment-without-blocks.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
for(;;) continue
// comment
;
for (;;) break
// comment
;
for (const f of []) continue
// comment
;
for (const f of []) break
// comment
;
for (const f in {}) continue
// comment
;
for (const f in {}) break
// comment
;
for(;;) continue // comment
;
for (;;) break // comment
;
for (const f of []) continue // comment
;
for (const f of []) break // comment
;
for (const f in {}) continue // comment
;
for (const f in {}) break // comment
;
for(;;) continue /* comment */
;
for (;;) break /* comment */
;
for (const f of []) continue /* comment */
;
for (const f of []) break /* comment */
;
for (const f in {}) continue /* comment */
;
for (const f in {}) break /* comment */
;
for(;;) continue
/* comment */
;
for (;;) break
/* comment */
;
for (const f of []) continue
/* comment */
;
for (const f of []) break
/* comment */
;
for (const f in {}) continue
/* comment */
;
for (const f in {}) break
/* comment */
;
label1: for (;;) continue label1 /* comment */
;
label1: for (;;) continue label1
/* comment */
;
label1: for (;;) continue label1 // comment
;
label1: for (;;) continue label1
// comment
;
=====================================output=====================================
for (;;)
continue;
// comment
for (;;)
break;
// comment
for (const f of [])
continue;
// comment
for (const f of [])
break;
// comment
for (const f in {})
continue;
// comment
for (const f in {})
break;
// comment
for (;;)
continue; // comment
for (;;)
break; // comment
for (const f of [])
continue; // comment
for (const f of [])
break; // comment
for (const f in {})
continue; // comment
for (const f in {})
break; // comment
for (;;) continue; /* comment */
for (;;) break; /* comment */
for (const f of []) continue; /* comment */
for (const f of []) break; /* comment */
for (const f in {}) continue; /* comment */
for (const f in {}) break; /* comment */
for (;;)
continue;
/* comment */
for (;;)
break;
/* comment */
for (const f of [])
continue;
/* comment */
for (const f of [])
break;
/* comment */
for (const f in {})
continue;
/* comment */
for (const f in {})
break;
/* comment */
label1: for (;;) continue label1 /* comment */;
label1: for (;;)
continue label1;
/* comment */
label1: for (;;)
continue label1; // comment
label1: for (;;)
continue label1;
// comment
================================================================================
`;
exports[`for.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
@@ -0,0 +1,121 @@
for(;;) {
continue // comment
;
}
for (;;) {
break // comment
;
}
for (const f of []) {
continue // comment
;
}
for (const f of []) {
break // comment
;
}
for (const f in {}) {
continue // comment
;
}
for (const f in {}) {
break // comment
;
}
while(true) {
continue // comment
;
}
while (true) {
break // comment
;
}
do {
continue // comment
;
} while(true);
do {
break // comment
;
} while(true);
label1: for (;;) {
continue label1 // comment
;
}
label2: {
break label2 // comment
;
};
for(;;) {
continue /* comment */
;
}
for (;;) {
break /* comment */
;
}
for (const f of []) {
continue /* comment */
;
}
for (const f of []) {
break /* comment */
;
}
for (const f in {}) {
continue /* comment */
;
}
for (const f in {}) {
break /* comment */
;
}
while(true) {
continue /* comment */
;
}
while (true) {
break /* comment */
;
}
do {
continue /* comment */
;
} while(true);
do {
break /* comment */
;
} while(true);
label1: for (;;) {
continue label1 /* comment */
;
}
label2: {
break label2 /* comment */
;
};
@@ -0,0 +1,145 @@
for(;;) {
continue
// comment
;
}
for (;;) {
break
// comment
;
}
for (const f of []) {
continue
// comment
;
}
for (const f of []) {
break
// comment
;
}
for (const f in {}) {
continue
// comment
;
}
for (const f in {}) {
break
// comment
;
}
while(true) {
continue
// comment
;
}
while (true) {
break
// comment
;
}
do {
continue
// comment
;
} while(true);
do {
break
// comment
;
} while(true);
label1: for (;;) {
continue label1
// comment
;
}
label2: {
break label2
// comment
;
};
for(;;) {
continue
/* comment */
;
}
for (;;) {
break
/* comment */
;
}
for (const f of []) {
continue
/* comment */
;
}
for (const f of []) {
break
/* comment */
;
}
for (const f in {}) {
continue
/* comment */
;
}
for (const f in {}) {
break
/* comment */
;
}
while(true) {
continue
/* comment */
;
}
while (true) {
break
/* comment */
;
}
do {
continue
/* comment */
;
} while(true);
do {
break
/* comment */
;
} while(true);
label1: for (;;) {
continue label1
/* comment */
;
}
label2: {
break label2
/* comment */
;
};
@@ -0,0 +1,97 @@
for(;;) continue
// comment
;
for (;;) break
// comment
;
for (const f of []) continue
// comment
;
for (const f of []) break
// comment
;
for (const f in {}) continue
// comment
;
for (const f in {}) break
// comment
;
for(;;) continue // comment
;
for (;;) break // comment
;
for (const f of []) continue // comment
;
for (const f of []) break // comment
;
for (const f in {}) continue // comment
;
for (const f in {}) break // comment
;
for(;;) continue /* comment */
;
for (;;) break /* comment */
;
for (const f of []) continue /* comment */
;
for (const f of []) break /* comment */
;
for (const f in {}) continue /* comment */
;
for (const f in {}) break /* comment */
;
for(;;) continue
/* comment */
;
for (;;) break
/* comment */
;
for (const f of []) continue
/* comment */
;
for (const f of []) break
/* comment */
;
for (const f in {}) continue
/* comment */
;
for (const f in {}) break
/* comment */
;
label1: for (;;) continue label1 /* comment */
;
label1: for (;;) continue label1
/* comment */
;
label1: for (;;) continue label1 // comment
;
label1: for (;;) continue label1
// comment
;
@@ -23,8 +23,6 @@ exports[`invalid-jsx-1.ts [typescript] format 1`] = `
4 |"
`;
exports[`issue-8773.ts [typescript] format 1`] = `"Comment \\"comment\\" was not printed. Please report this error!"`;
exports[`module-attributes-static.ts [typescript] format 1`] = `
"';' expected. (1:28)
> 1 | import foo from \\"foo.json\\" with type: \\"json\\";
@@ -1,5 +0,0 @@
// prettier-ignore
foo + foo;
continue // comment
;