Fix comment around as/satisfies expression (#18162)
This commit is contained in:
1 parent
062ab10601
commit
7d3ff5d2fc
7 files changed
+249
-56
No files matched your search
@@ -0,0 +1,28 @@
|
||||
#### Fix comment around `as`/`satisfies` expression (#18162 by @fisker)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```tsx
|
||||
// Input
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
const;
|
||||
|
||||
// Prettier stable (First format)
|
||||
1 /*
|
||||
comment
|
||||
*/ as const;
|
||||
|
||||
// Prettier stable (Second format)
|
||||
SyntaxError: Unexpected keyword or identifier. (3:4)
|
||||
1 | 1 /*
|
||||
2 | comment
|
||||
> 3 | */ as const;
|
||||
| ^
|
||||
4 |
|
||||
|
||||
// Prettier main
|
||||
1 as const /*
|
||||
comment
|
||||
*/;
|
||||
```
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
createTypeCheckFunction,
|
||||
getCallArguments,
|
||||
getFunctionParameters,
|
||||
isBinaryCastExpression,
|
||||
isCallExpression,
|
||||
isCallLikeExpression,
|
||||
isConditionalType,
|
||||
@@ -71,6 +72,7 @@ function handleOwnLineComment(context) {
|
||||
handleNestedConditionalExpressionComments,
|
||||
handleCommentsInDestructuringPattern,
|
||||
handleTSMappedTypeComments,
|
||||
handleBinaryCastExpressionComment,
|
||||
].some((fn) => fn(context));
|
||||
}
|
||||
|
||||
@@ -99,6 +101,7 @@ function handleEndOfLineComment(context) {
|
||||
handleLastBinaryOperatorOperand,
|
||||
handleTSMappedTypeComments,
|
||||
handlePropertySignatureComments,
|
||||
handleBinaryCastExpressionComment,
|
||||
].some((fn) => fn(context));
|
||||
}
|
||||
|
||||
@@ -118,6 +121,7 @@ function handleRemainingComment(context) {
|
||||
handleFunctionNameComments,
|
||||
handleBreakAndContinueStatementComments,
|
||||
handleTSFunctionTrailingComments,
|
||||
handleBinaryCastExpressionComment,
|
||||
].some((fn) => fn(context));
|
||||
}
|
||||
|
||||
@@ -1121,6 +1125,28 @@ function handlePropertySignatureComments({
|
||||
}
|
||||
}
|
||||
|
||||
function handleBinaryCastExpressionComment({
|
||||
enclosingNode,
|
||||
precedingNode,
|
||||
followingNode,
|
||||
comment,
|
||||
text,
|
||||
}) {
|
||||
// Avoid break before `as` and `satisfies`
|
||||
if (
|
||||
isBinaryCastExpression(enclosingNode) &&
|
||||
precedingNode === enclosingNode.expression &&
|
||||
!isSingleLineComment(comment, text)
|
||||
) {
|
||||
if (followingNode) {
|
||||
addLeadingComment(followingNode, comment);
|
||||
} else {
|
||||
addTrailingComment(enclosingNode, comment);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
|
||||
@@ -21,25 +21,6 @@ const;
|
||||
|
||||
1 as /* comment */ not_const;
|
||||
|
||||
// https://github.com/prettier/prettier/issues/18160
|
||||
// 1 as /*
|
||||
// 1
|
||||
// */ const;
|
||||
1 as
|
||||
/*
|
||||
2
|
||||
*/ const;
|
||||
// https://github.com/prettier/prettier/issues/18160
|
||||
// 1 as /*
|
||||
// 3
|
||||
// */
|
||||
// const;
|
||||
1 as
|
||||
/*
|
||||
4
|
||||
*/
|
||||
const;
|
||||
|
||||
=====================================output=====================================
|
||||
1 /* comment */ as const;
|
||||
1 /* comment */ as const;
|
||||
@@ -53,23 +34,5 @@ const;
|
||||
|
||||
1 as /* comment */ not_const;
|
||||
|
||||
// https://github.com/prettier/prettier/issues/18160
|
||||
// 1 as /*
|
||||
// 1
|
||||
// */ const;
|
||||
1 as const;
|
||||
/*
|
||||
2
|
||||
*/
|
||||
// https://github.com/prettier/prettier/issues/18160
|
||||
// 1 as /*
|
||||
// 3
|
||||
// */
|
||||
// const;
|
||||
1 as const;
|
||||
/*
|
||||
4
|
||||
*/
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -12,22 +12,3 @@ const;
|
||||
1 as const; // comment
|
||||
|
||||
1 as /* comment */ not_const;
|
||||
|
||||
// https://github.com/prettier/prettier/issues/18160
|
||||
// 1 as /*
|
||||
// 1
|
||||
// */ const;
|
||||
1 as
|
||||
/*
|
||||
2
|
||||
*/ const;
|
||||
// https://github.com/prettier/prettier/issues/18160
|
||||
// 1 as /*
|
||||
// 3
|
||||
// */
|
||||
// const;
|
||||
1 as
|
||||
/*
|
||||
4
|
||||
*/
|
||||
const;
|
||||
@@ -0,0 +1,58 @@
|
||||
1 as /*
|
||||
comment
|
||||
*/const;
|
||||
1 as /*
|
||||
comment
|
||||
*/Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/Foo;
|
||||
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/const;
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/Foo;
|
||||
1 satisfies
|
||||
/*
|
||||
comment
|
||||
*/Foo;
|
||||
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
const;
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
const;
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
1 satisfies
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
|
||||
1 as // comment
|
||||
const;
|
||||
1 as // comment
|
||||
Foo;
|
||||
1 satisfies // comment
|
||||
Foo;
|
||||
@@ -0,0 +1,134 @@
|
||||
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
||||
|
||||
exports[`18160.ts [hermes] format 1`] = `
|
||||
"';' expected (7:2)
|
||||
5 | comment
|
||||
6 | */Foo;
|
||||
> 7 | 1 satisfies /*
|
||||
| ^
|
||||
8 | comment
|
||||
9 | */Foo;
|
||||
10 |
|
||||
Cause: ';' expected (7:2)
|
||||
1 satisfies /*
|
||||
^"
|
||||
`;
|
||||
|
||||
exports[`18160.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript", "flow"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
1 as /*
|
||||
comment
|
||||
*/const;
|
||||
1 as /*
|
||||
comment
|
||||
*/Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/Foo;
|
||||
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/const;
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/Foo;
|
||||
1 satisfies
|
||||
/*
|
||||
comment
|
||||
*/Foo;
|
||||
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
const;
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
const;
|
||||
1 as
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
1 satisfies
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
|
||||
1 as // comment
|
||||
const;
|
||||
1 as // comment
|
||||
Foo;
|
||||
1 satisfies // comment
|
||||
Foo;
|
||||
|
||||
=====================================output=====================================
|
||||
1 as const /*
|
||||
comment
|
||||
*/;
|
||||
1 as /*
|
||||
comment
|
||||
*/ Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/ Foo;
|
||||
|
||||
1 as const;
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
1 as /*
|
||||
comment
|
||||
*/ Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/ Foo;
|
||||
|
||||
1 as const /*
|
||||
comment
|
||||
*/;
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
|
||||
1 as const;
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
1 as /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
1 satisfies /*
|
||||
comment
|
||||
*/
|
||||
Foo;
|
||||
|
||||
1 as const; // comment
|
||||
1 as Foo; // comment
|
||||
1 satisfies Foo; // comment
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -0,0 +1,3 @@
|
||||
runFormatTest(import.meta, ["typescript", "flow"], {
|
||||
errors: { hermes: ["18160.ts"] },
|
||||
});
|
||||
Reference in new issue
Block a user