Preserve trailing double spaces in JSDoc (#18594)
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
1 parent
9fbc9ce22d
commit
303391a94a
6 files changed
+207
-10
No files matched your search
@@ -0,0 +1,13 @@
|
||||
#### Preserve trailing double spaces in JSDoc (#18594 by @seiyab)
|
||||
|
||||
Some tools treat these spaces as meaningful.
|
||||
|
||||
```bash
|
||||
await require('prettier').format('/**\n * With 2 spaces \n */', {parser: 'babel'});
|
||||
|
||||
# Prettier stable
|
||||
// -> '/**\n * With 2 spaces\n */\n'
|
||||
|
||||
# Prettier main
|
||||
// -> '/**\n * With 2 spaces \n */\n'
|
||||
```
|
||||
@@ -1,9 +1,19 @@
|
||||
import { hardline, join, replaceEndOfLine } from "../../document/index.js";
|
||||
import {
|
||||
hardline,
|
||||
literalline,
|
||||
markAsRoot,
|
||||
replaceEndOfLine,
|
||||
} from "../../document/index.js";
|
||||
import { locEnd, locStart } from "../loc.js";
|
||||
import isBlockComment from "../utilities/is-block-comment.js";
|
||||
import isIndentableBlockComment from "../utilities/is-indentable-block-comment.js";
|
||||
import isLineComment from "../utilities/is-line-comment.js";
|
||||
|
||||
/**
|
||||
@import {Doc} from "../../document/index.js"
|
||||
@import {Comment} from "../types/estree.js"
|
||||
*/
|
||||
|
||||
function printComment(path, options) {
|
||||
const comment = path.node;
|
||||
|
||||
@@ -26,19 +36,33 @@ function printComment(path, options) {
|
||||
throw new Error("Not a comment: " + JSON.stringify(comment));
|
||||
}
|
||||
|
||||
/**
|
||||
@param {Comment} comment
|
||||
@returns {Doc}
|
||||
*/
|
||||
function printIndentableBlockComment(comment) {
|
||||
const lines = comment.value.split("\n");
|
||||
const isJsdoc = comment.value[0] === "*" && comment.value[1] !== "*";
|
||||
|
||||
return [
|
||||
"/*",
|
||||
join(
|
||||
hardline,
|
||||
lines.map((line, index) =>
|
||||
index === 0
|
||||
? line.trimEnd()
|
||||
: " " + (index < lines.length - 1 ? line.trim() : line.trimStart()),
|
||||
),
|
||||
),
|
||||
lines.map((line, index) => {
|
||||
if (index === 0) {
|
||||
return [line.trimEnd(), hardline];
|
||||
}
|
||||
|
||||
if (index === lines.length - 1) {
|
||||
return [" ", line.trimStart()];
|
||||
}
|
||||
|
||||
const trimmed = line.trim();
|
||||
const content = [" ", trimmed];
|
||||
if (isJsdoc && trimmed !== "*" && line.endsWith(" ")) {
|
||||
return [content, " ", markAsRoot(literalline)];
|
||||
}
|
||||
|
||||
return [content, hardline];
|
||||
}),
|
||||
"*/",
|
||||
];
|
||||
}
|
||||
|
||||
@@ -3374,6 +3374,7 @@ function HelloWorld() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
=====================================output=====================================
|
||||
/** @type {any} */
|
||||
const x = (
|
||||
@@ -3437,6 +3438,7 @@ function HelloWorld() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
=====================================output=====================================
|
||||
/** @type {any} */
|
||||
const x = (
|
||||
|
||||
@@ -23,4 +23,4 @@ function HelloWorld() {
|
||||
<span>Test</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import { outdent } from "outdent";
|
||||
|
||||
const fixtures = {
|
||||
importMeta: import.meta,
|
||||
snippets: [
|
||||
{
|
||||
name: "Preserve trailing double spaces in JSDoc",
|
||||
code: outdent`
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "Misaligned",
|
||||
code: outdent`
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "Non-top-level",
|
||||
code: outdent`
|
||||
function foo() {
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/
|
||||
}
|
||||
`,
|
||||
output: outdent`
|
||||
function foo() {
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/
|
||||
}\n
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "Preserve trailing 2+ spaces in JSDoc",
|
||||
code: outdent`
|
||||
/**
|
||||
* 3 spaces.${" ".repeat(3)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/**
|
||||
* 3 spaces.${" ".repeat(2)}
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "Spaces on start line",
|
||||
code: outdent`
|
||||
/**${" ".repeat(2)}
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/**
|
||||
* 2 spaces.${" ".repeat(2)}
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "Remove on blank line",
|
||||
code: outdent`
|
||||
/**
|
||||
*${" ".repeat(2)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/**
|
||||
*
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "Remove trailing single space in JSDoc",
|
||||
code: outdent`
|
||||
/**
|
||||
* 1 space.${" ".repeat(1)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/**
|
||||
* 1 space.
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
/*
|
||||
> Each comment must start with a `/**` sequence in order to be recognized by the JSDoc parser.
|
||||
> Comments beginning with `/*`, `/***`, or more than 3 stars will be ignored.
|
||||
> This is a feature to allow you to suppress parsing of comment blocks.
|
||||
|
||||
https://jsdoc.app/about-getting-started
|
||||
*/
|
||||
{
|
||||
name: "Remove trailing single space in non-JSDoc",
|
||||
code: outdent`
|
||||
/***
|
||||
* NOT JSDOC.${" ".repeat(2)}
|
||||
*/
|
||||
`,
|
||||
output: outdent`
|
||||
/***
|
||||
* NOT JSDOC.
|
||||
*/\n
|
||||
`,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
runFormatTest(fixtures, ["babel", "flow", "typescript"]);
|
||||
@@ -0,0 +1,34 @@
|
||||
import { outdent } from "outdent";
|
||||
|
||||
const fixtures = {
|
||||
importMeta: import.meta,
|
||||
snippets: [
|
||||
{
|
||||
name: "Preserve trailing double spaces in JSDoc",
|
||||
code: outdent`
|
||||
<script>
|
||||
function foo
|
||||
(
|
||||
uglyParameter,
|
||||
toEnsure, thisCode,
|
||||
isFormatted, byJsParser) {
|
||||
/**
|
||||
* Should not break when html parser add indention.${" ".repeat(2)}
|
||||
*/
|
||||
}
|
||||
</script>
|
||||
`,
|
||||
output: outdent`
|
||||
<script>
|
||||
function foo(uglyParameter, toEnsure, thisCode, isFormatted, byJsParser) {
|
||||
/**
|
||||
* Should not break when html parser add indention.${" ".repeat(2)}
|
||||
*/
|
||||
}
|
||||
</script>\n
|
||||
`,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
runFormatTest(fixtures, ["vue"], { vueIndentScriptAndStyle: true });
|
||||
Reference in new issue
Block a user