From 696f1295f0c98d5ac605f397a0448d3589720cb5 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Fri, 26 Apr 2024 00:09:21 +0900 Subject: [PATCH] fix(eslint-plugin): [consistent-type-assertions] handle tagged templates --- .../src/rules/consistent-type-assertions.ts | 5 +- .../rules/consistent-type-assertions.test.ts | 46 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-assertions.ts b/packages/eslint-plugin/src/rules/consistent-type-assertions.ts index e3d88fb69c7c..4a8ac4d4d794 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-assertions.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-assertions.ts @@ -223,7 +223,10 @@ export default createRule({ node.parent.type === AST_NODE_TYPES.CallExpression || node.parent.type === AST_NODE_TYPES.ThrowStatement || node.parent.type === AST_NODE_TYPES.AssignmentPattern || - node.parent.type === AST_NODE_TYPES.JSXExpressionContainer) + node.parent.type === AST_NODE_TYPES.JSXExpressionContainer || + (node.parent.type === AST_NODE_TYPES.TemplateLiteral && + node.parent.parent.type === + AST_NODE_TYPES.TaggedTemplateExpression)) ) { return; } diff --git a/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts b/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts index 3a6b28650c06..6d3b64bbfde3 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts @@ -26,7 +26,8 @@ const x = new (Generic)(); const x = new (Generic)('string'); const x = () => { bar: 5 }; const x = () => ({ bar: 5 }); -const x = () => bar;`; +const x = () => bar; +const x = bar\`\${"baz"}\`;`; const ANGLE_BRACKET_TESTS = `${ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE} const x = { key: 'value' }; @@ -45,7 +46,8 @@ const x = new (Generic as Foo)(); const x = new (Generic as Foo)('string'); const x = () => ({ bar: 5 } as Foo); const x = () => ({ bar: 5 } as Foo); -const x = () => (bar as Foo);`; +const x = () => (bar as Foo); +const x = bar\`\${"baz"}\` as Foo;`; const AS_TESTS = `${AS_TESTS_EXCEPT_CONST_CASE} const x = { key: 'value' } as const; @@ -69,6 +71,7 @@ function b(x = {} as Foo.Bar) {} function c(x = {} as Foo) {} print?.({ bar: 5 } as Foo) print?.call({ bar: 5 } as Foo) +print\`\${{ bar: 5 } as Foo}\` `; const OBJECT_LITERAL_ARGUMENT_ANGLE_BRACKET_CASTS = ` print({ bar: 5 }) @@ -76,6 +79,7 @@ new print({ bar: 5 }) function foo() { throw { bar: 5 } } print?.({ bar: 5 }) print?.call({ bar: 5 }) +print\`\${{ bar: 5 }}\` `; ruleTester.run('consistent-type-assertions', rule, { @@ -230,6 +234,10 @@ ruleTester.run('consistent-type-assertions', rule, { messageId: 'angle-bracket', line: 15, }, + { + messageId: 'angle-bracket', + line: 16, + }, ], }), ...batchedSingleLineTests({ @@ -296,6 +304,10 @@ ruleTester.run('consistent-type-assertions', rule, { messageId: 'as', line: 15, }, + { + messageId: 'as', + line: 16, + }, ], output: AS_TESTS, }), @@ -359,6 +371,10 @@ ruleTester.run('consistent-type-assertions', rule, { messageId: 'never', line: 14, }, + { + messageId: 'never', + line: 15, + }, ], }), ...batchedSingleLineTests({ @@ -421,6 +437,10 @@ ruleTester.run('consistent-type-assertions', rule, { messageId: 'never', line: 14, }, + { + messageId: 'never', + line: 15, + }, ], }), ...batchedSingleLineTests({ @@ -660,6 +680,17 @@ ruleTester.run('consistent-type-assertions', rule, { }, ], }, + { + messageId: 'unexpectedObjectTypeAssertion', + line: 12, + suggestions: [ + { + messageId: 'replaceObjectTypeAssertionWithSatisfies', + data: { cast: 'Foo' }, + output: `print\`\${{ bar: 5 } satisfies Foo}\``, + }, + ], + }, ], }), ...batchedSingleLineTests({ @@ -769,6 +800,17 @@ ruleTester.run('consistent-type-assertions', rule, { }, ], }, + { + messageId: 'unexpectedObjectTypeAssertion', + line: 10, + suggestions: [ + { + messageId: 'replaceObjectTypeAssertionWithSatisfies', + data: { cast: 'Foo' }, + output: `print\`\${{ bar: 5 } satisfies Foo}\``, + }, + ], + }, ], }), {