Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51d2193

Browse files
authored
fix(eslint-plugin): [consistent-type-assertions] handle tagged templates (typescript-eslint#8993)
1 parent 2995be8 commit 51d2193

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ export default createRule<Options, MessageIds>({
223223
node.parent.type === AST_NODE_TYPES.CallExpression ||
224224
node.parent.type === AST_NODE_TYPES.ThrowStatement ||
225225
node.parent.type === AST_NODE_TYPES.AssignmentPattern ||
226-
node.parent.type === AST_NODE_TYPES.JSXExpressionContainer)
226+
node.parent.type === AST_NODE_TYPES.JSXExpressionContainer ||
227+
(node.parent.type === AST_NODE_TYPES.TemplateLiteral &&
228+
node.parent.parent.type ===
229+
AST_NODE_TYPES.TaggedTemplateExpression))
227230
) {
228231
return;
229232
}

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const x = new (<Foo>Generic<string>)();
2626
const x = new (<Foo>Generic<string>)('string');
2727
const x = () => <Foo>{ bar: 5 };
2828
const x = () => <Foo>({ bar: 5 });
29-
const x = () => <Foo>bar;`;
29+
const x = () => <Foo>bar;
30+
const x = <Foo>bar<string>\`\${"baz"}\`;`;
3031

3132
const ANGLE_BRACKET_TESTS = `${ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE}
3233
const x = <const>{ key: 'value' };
@@ -45,7 +46,8 @@ const x = new (Generic<string> as Foo)();
4546
const x = new (Generic<string> as Foo)('string');
4647
const x = () => ({ bar: 5 } as Foo);
4748
const x = () => ({ bar: 5 } as Foo);
48-
const x = () => (bar as Foo);`;
49+
const x = () => (bar as Foo);
50+
const x = bar<string>\`\${"baz"}\` as Foo;`;
4951

5052
const AS_TESTS = `${AS_TESTS_EXCEPT_CONST_CASE}
5153
const x = { key: 'value' } as const;
@@ -69,13 +71,15 @@ function b(x = {} as Foo.Bar) {}
6971
function c(x = {} as Foo) {}
7072
print?.({ bar: 5 } as Foo)
7173
print?.call({ bar: 5 } as Foo)
74+
print\`\${{ bar: 5 } as Foo}\`
7275
`;
7376
const OBJECT_LITERAL_ARGUMENT_ANGLE_BRACKET_CASTS = `
7477
print(<Foo>{ bar: 5 })
7578
new print(<Foo>{ bar: 5 })
7679
function foo() { throw <Foo>{ bar: 5 } }
7780
print?.(<Foo>{ bar: 5 })
7881
print?.call(<Foo>{ bar: 5 })
82+
print\`\${<Foo>{ bar: 5 }}\`
7983
`;
8084

8185
ruleTester.run('consistent-type-assertions', rule, {
@@ -230,6 +234,10 @@ ruleTester.run('consistent-type-assertions', rule, {
230234
messageId: 'angle-bracket',
231235
line: 15,
232236
},
237+
{
238+
messageId: 'angle-bracket',
239+
line: 16,
240+
},
233241
],
234242
}),
235243
...batchedSingleLineTests<MessageIds, Options>({
@@ -296,6 +304,10 @@ ruleTester.run('consistent-type-assertions', rule, {
296304
messageId: 'as',
297305
line: 15,
298306
},
307+
{
308+
messageId: 'as',
309+
line: 16,
310+
},
299311
],
300312
output: AS_TESTS,
301313
}),
@@ -359,6 +371,10 @@ ruleTester.run('consistent-type-assertions', rule, {
359371
messageId: 'never',
360372
line: 14,
361373
},
374+
{
375+
messageId: 'never',
376+
line: 15,
377+
},
362378
],
363379
}),
364380
...batchedSingleLineTests<MessageIds, Options>({
@@ -421,6 +437,10 @@ ruleTester.run('consistent-type-assertions', rule, {
421437
messageId: 'never',
422438
line: 14,
423439
},
440+
{
441+
messageId: 'never',
442+
line: 15,
443+
},
424444
],
425445
}),
426446
...batchedSingleLineTests<MessageIds, Options>({
@@ -660,6 +680,17 @@ ruleTester.run('consistent-type-assertions', rule, {
660680
},
661681
],
662682
},
683+
{
684+
messageId: 'unexpectedObjectTypeAssertion',
685+
line: 12,
686+
suggestions: [
687+
{
688+
messageId: 'replaceObjectTypeAssertionWithSatisfies',
689+
data: { cast: 'Foo' },
690+
output: `print\`\${{ bar: 5 } satisfies Foo}\``,
691+
},
692+
],
693+
},
663694
],
664695
}),
665696
...batchedSingleLineTests<MessageIds, Options>({
@@ -769,6 +800,17 @@ ruleTester.run('consistent-type-assertions', rule, {
769800
},
770801
],
771802
},
803+
{
804+
messageId: 'unexpectedObjectTypeAssertion',
805+
line: 10,
806+
suggestions: [
807+
{
808+
messageId: 'replaceObjectTypeAssertionWithSatisfies',
809+
data: { cast: 'Foo' },
810+
output: `print\`\${{ bar: 5 } satisfies Foo}\``,
811+
},
812+
],
813+
},
772814
],
773815
}),
774816
{

0 commit comments

Comments
 (0)