Skip to content

Commit 4e9c4e2

Browse files
committed
apply review
1 parent fdcd4ce commit 4e9c4e2

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

Diff for: packages/eslint-plugin/src/rules/consistent-type-assertions.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type OptUnion =
3535
};
3636
export type Options = readonly [OptUnion];
3737

38+
type AsExpressionOrTypeAssertion =
39+
| TSESTree.TSAsExpression
40+
| TSESTree.TSTypeAssertion;
41+
3842
export default createRule<Options, MessageIds>({
3943
name: 'consistent-type-assertions',
4044
meta: {
@@ -122,7 +126,7 @@ export default createRule<Options, MessageIds>({
122126
}
123127

124128
function reportIncorrectAssertionType(
125-
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
129+
node: AsExpressionOrTypeAssertion,
126130
): void {
127131
const messageId = options.assertionStyle;
128132

@@ -209,7 +213,7 @@ export default createRule<Options, MessageIds>({
209213
}
210214

211215
function getSuggestions(
212-
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
216+
node: AsExpressionOrTypeAssertion,
213217
annotationMessageId: MessageIds,
214218
satisfiesMessageId: MessageIds,
215219
): TSESLint.ReportSuggestionArray<MessageIds> {
@@ -251,9 +255,7 @@ export default createRule<Options, MessageIds>({
251255
return suggestions;
252256
}
253257

254-
function isAsParameter(
255-
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
256-
): boolean {
258+
function isAsParameter(node: AsExpressionOrTypeAssertion): boolean {
257259
return (
258260
node.parent.type === AST_NODE_TYPES.NewExpression ||
259261
node.parent.type === AST_NODE_TYPES.CallExpression ||
@@ -266,7 +268,7 @@ export default createRule<Options, MessageIds>({
266268
}
267269

268270
function checkExpressionForObjectAssertion(
269-
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
271+
node: AsExpressionOrTypeAssertion,
270272
): void {
271273
if (
272274
options.assertionStyle === 'never' ||
@@ -278,14 +280,7 @@ export default createRule<Options, MessageIds>({
278280

279281
if (
280282
options.objectLiteralTypeAssertions === 'allow-as-parameter' &&
281-
(node.parent.type === AST_NODE_TYPES.NewExpression ||
282-
node.parent.type === AST_NODE_TYPES.CallExpression ||
283-
node.parent.type === AST_NODE_TYPES.ThrowStatement ||
284-
node.parent.type === AST_NODE_TYPES.AssignmentPattern ||
285-
node.parent.type === AST_NODE_TYPES.JSXExpressionContainer ||
286-
(node.parent.type === AST_NODE_TYPES.TemplateLiteral &&
287-
node.parent.parent.type ===
288-
AST_NODE_TYPES.TaggedTemplateExpression))
283+
isAsParameter(node)
289284
) {
290285
return;
291286
}
@@ -306,7 +301,7 @@ export default createRule<Options, MessageIds>({
306301
}
307302

308303
function checkExpressionForArrayAssertion(
309-
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
304+
node: AsExpressionOrTypeAssertion,
310305
): void {
311306
if (
312307
options.assertionStyle === 'never' ||

Diff for: packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts

+75
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ function foo() {
226226
},
227227
],
228228
},
229+
{
230+
code: 'new Print([5] as Foo);',
231+
options: [
232+
{
233+
arrayLiteralTypeAssertions: 'allow-as-parameter',
234+
assertionStyle: 'as',
235+
},
236+
],
237+
},
238+
{
239+
code: 'const bar = <Foo style={[5] as Bar} />;',
240+
languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } },
241+
options: [
242+
{
243+
arrayLiteralTypeAssertions: 'allow-as-parameter',
244+
assertionStyle: 'as',
245+
},
246+
],
247+
},
229248
{
230249
code: 'print(<Foo>[5]);',
231250
options: [
@@ -284,6 +303,15 @@ function foo() {
284303
},
285304
],
286305
},
306+
{
307+
code: 'new Print(<Foo>[5]);',
308+
options: [
309+
{
310+
arrayLiteralTypeAssertions: 'allow-as-parameter',
311+
assertionStyle: 'angle-bracket',
312+
},
313+
],
314+
},
287315
{ code: 'const x = <const>[1];', options: [{ assertionStyle: 'never' }] },
288316
{ code: 'const x = [1] as const;', options: [{ assertionStyle: 'never' }] },
289317
{
@@ -1037,6 +1065,27 @@ function foo() {
10371065
},
10381066
],
10391067
},
1068+
{
1069+
code: 'const foo = () => [5] as Foo',
1070+
errors: [
1071+
{
1072+
messageId: 'unexpectedArrayTypeAssertion',
1073+
suggestions: [
1074+
{
1075+
data: { cast: 'Foo' },
1076+
messageId: 'replaceArrayTypeAssertionWithSatisfies',
1077+
output: 'const foo = () => [5] satisfies Foo',
1078+
},
1079+
],
1080+
},
1081+
],
1082+
options: [
1083+
{
1084+
arrayLiteralTypeAssertions: 'allow-as-parameter',
1085+
assertionStyle: 'as',
1086+
},
1087+
],
1088+
},
10401089
{
10411090
code: 'new print(<Foo>[5]);',
10421091
errors: [
@@ -1129,5 +1178,31 @@ function foo() {
11291178
},
11301179
],
11311180
},
1181+
{
1182+
code: 'const foo = <Foo>[5];',
1183+
errors: [
1184+
{
1185+
messageId: 'unexpectedArrayTypeAssertion',
1186+
suggestions: [
1187+
{
1188+
data: { cast: 'Foo' },
1189+
messageId: 'replaceArrayTypeAssertionWithAnnotation',
1190+
output: 'const foo: Foo = [5];',
1191+
},
1192+
{
1193+
data: { cast: 'Foo' },
1194+
messageId: 'replaceArrayTypeAssertionWithSatisfies',
1195+
output: 'const foo = [5] satisfies Foo;',
1196+
},
1197+
],
1198+
},
1199+
],
1200+
options: [
1201+
{
1202+
arrayLiteralTypeAssertions: 'allow-as-parameter',
1203+
assertionStyle: 'angle-bracket',
1204+
},
1205+
],
1206+
},
11321207
],
11331208
});

0 commit comments

Comments
 (0)