Improved multiline closure comment detection (#6070)

master
Yang Su 2019-05-07 03:30:07 -07:00 committed by Lucas Duailibe
parent 26183e5be2
commit 54b7655dab
4 changed files with 67 additions and 3 deletions

View File

@ -151,4 +151,39 @@ Examples:
{{/if}}
e
{{/if}}
* JavaScript: Improved multiline closure compiler typecast comment detection ([#6070] by [@yangsu])
Previously, multiline closure compiler typecast comments with lines that
start with \* weren't flagged correctly and the subsequent parenthesis were
stripped. Prettier master fixes this issue.
<!-- prettier-ignore --\>
```js
// Input
const style =/**
* @type {{
* width: number,
* }}
*/({
width,
});
// Output (Prettier stable)
const style =/**
* @type {{
* width: number,
* }}
*/ {
width,
};
// Output (Prettier master)
const style =/**
* @type {{
* width: number,
* }}
*/({
width,
});
```

View File

@ -45,13 +45,18 @@ function hasClosureCompilerTypeCastComment(text, path) {
}
function isTypeCastComment(comment) {
const trimmed = comment.trim();
if (!/^\*\s*@type\s*\{[^]+\}$/.test(trimmed)) {
const cleaned = comment
.trim()
.split("\n")
.map(line => line.replace(/^[\s*]+/, ""))
.join(" ")
.trim();
if (!/^@type\s+\{[^]+\}$/.test(cleaned)) {
return false;
}
let isCompletelyClosed = false;
let unpairedBracketCount = 0;
for (const char of trimmed) {
for (const char of cleaned) {
if (char === "{") {
if (isCompletelyClosed) {
return false;

View File

@ -65,6 +65,14 @@ const style = /** @type {{
...margins,
});
const style =/**
* @type {{
* width: number,
* }}
*/({
width,
});
=====================================output=====================================
// test to make sure comments are attached correctly
let inlineComment = /* some comment */ someReallyLongFunctionCall(
@ -128,5 +136,13 @@ const style = /** @type {{
...margins
});
const style = /**
* @type {{
* width: number,
* }}
*/ ({
width
});
================================================================================
`;

View File

@ -56,3 +56,11 @@ const style = /** @type {{
height,
...margins,
});
const style =/**
* @type {{
* width: number,
* }}
*/({
width,
});