JS: Fix end-of-line comments if followed by trailing whitespace (#8069)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
1 parent
2a7515b77e
commit
1528e45ff2
5 files changed
+114
-4
No files matched your search
@@ -0,0 +1,33 @@
|
||||
#### Fix end-of-line comments if followed by trailing whitespace ([#8069](https://github.com/prettier/prettier/pull/8069) by [@shisama](https://github.com/shisama))
|
||||
|
||||
If comments line has trailing whitespaces, comments aren't detected as end-of-line.
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```jsx
|
||||
// Input
|
||||
var a = { /* extra whitespace --> */
|
||||
b };
|
||||
|
||||
var a = { /* no whitespace --> */
|
||||
b };
|
||||
|
||||
// Prettier stable
|
||||
var a = {
|
||||
/* extra whitespace --> */
|
||||
|
||||
b,
|
||||
};
|
||||
|
||||
var a = {
|
||||
/* no whitespace --> */ b,
|
||||
};
|
||||
|
||||
// Prettier master
|
||||
var a = {
|
||||
/* extra whitespace --> */ b,
|
||||
};
|
||||
|
||||
var a = {
|
||||
/* no whitespace --> */ b,
|
||||
};
|
||||
```
|
||||
@@ -14,6 +14,7 @@ const {
|
||||
const {
|
||||
hasNewline,
|
||||
skipNewline,
|
||||
skipSpaces,
|
||||
isPreviousLineEmpty,
|
||||
} = require("../common/util");
|
||||
const {
|
||||
@@ -520,7 +521,10 @@ function printComments(path, print, options, needsSemi) {
|
||||
leadingParts.push(contents);
|
||||
|
||||
const text = options.originalText;
|
||||
const index = skipNewline(text, options.locEnd(comment));
|
||||
const index = skipNewline(
|
||||
text,
|
||||
skipSpaces(text, options.locEnd(comment))
|
||||
);
|
||||
if (index !== false && hasNewline(text, index)) {
|
||||
leadingParts.push(hardline);
|
||||
}
|
||||
|
||||
@@ -3835,6 +3835,72 @@ if (true) {
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`snippet: #0 - {"semi":false} format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
semi: false
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
var a = { /* comment */
|
||||
b };
|
||||
=====================================output=====================================
|
||||
var a = {
|
||||
/* comment */ b,
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`snippet: #0 format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
var a = { /* comment */
|
||||
b };
|
||||
=====================================output=====================================
|
||||
var a = {
|
||||
/* comment */ b,
|
||||
};
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`snippet: #1 - {"semi":false} format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
semi: false
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
var a = { /* comment */
|
||||
b };
|
||||
=====================================output=====================================
|
||||
var a = {
|
||||
/* comment */ b,
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`snippet: #1 format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
var a = { /* comment */
|
||||
b };
|
||||
=====================================output=====================================
|
||||
var a = {
|
||||
/* comment */ b,
|
||||
};
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`switch.js - {"semi":false} format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow", "babel", "typescript"]
|
||||
|
||||
@@ -1,2 +1,10 @@
|
||||
run_spec(__dirname, ["flow", "babel", "typescript"]);
|
||||
run_spec(__dirname, ["flow", "babel", "typescript"], { semi: false });
|
||||
const fixtures = {
|
||||
dirname: __dirname,
|
||||
snippets: [
|
||||
"var a = { /* comment */ \nb };", // trailing whitespace after comment
|
||||
"var a = { /* comment */\nb };",
|
||||
],
|
||||
};
|
||||
|
||||
run_spec(fixtures, ["flow", "babel", "typescript"]);
|
||||
run_spec(fixtures, ["flow", "babel", "typescript"], { semi: false });
|
||||
@@ -26,7 +26,6 @@ function foo(bar) {
|
||||
/**
|
||||
* Some notes that should not be appended to
|
||||
*/
|
||||
|
||||
const fruit = "tomatoes";
|
||||
|
||||
================================================================================
|
||||
|
||||
Reference in new issue
Block a user