Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [consistent-type-assertions] wrap object return value with parentheses #6885

23 changes: 17 additions & 6 deletions packages/eslint-plugin/src/rules/consistent-type-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,27 @@ export default util.createRule<Options, MessageIds>({
: {},
fix:
messageId === 'as'
? (fixer): TSESLint.RuleFix[] => [
fixer.replaceText(
? function* (fixer): Generator<TSESLint.RuleFix> {
yield fixer.replaceText(
node,
getTextWithParentheses(node.expression),
),
fixer.insertTextAfter(
);

if (
node.expression.type === AST_NODE_TYPES.ObjectExpression &&
node.parent?.type ===
AST_NODE_TYPES.ArrowFunctionExpression &&
!util.isParenthesized(node.expression, sourceCode)
) {
yield fixer.insertTextBefore(node, '(');
yield fixer.insertTextAfter(node, ')');
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

yield fixer.insertTextAfter(
node,
` as ${getTextWithParentheses(node.typeAnnotation)}`,
),
]
);
}
: undefined,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const x = <A>!'string';
const x = <A>a + b;
const x = <(A)>a + (b);
const x = <Foo>(new Generic<string>());
const x = (new (<Foo>Generic<string>)());`;
const x = (new (<Foo>Generic<string>)());
const x = () => <Foo>{ bar: 5 };
const x = () => <Foo>({ bar: 5 });
const x = () => <Foo>bar;`;

const ANGLE_BRACKET_TESTS = `${ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE}
const x = <const>{ key: 'value' };
Expand All @@ -29,7 +32,10 @@ const x = !'string' as A;
const x = a as A + b;
const x = a as (A) + (b);
const x = (new Generic<string>()) as Foo;
const x = (new (Generic<string> as Foo)());`;
const x = (new (Generic<string> as Foo)());
const x = () => ({ bar: 5 }) as Foo;
const x = () => ({ bar: 5 }) as Foo;
const x = () => bar as Foo;`;

const AS_TESTS = `${AS_TESTS_EXCEPT_CONST_CASE}
const x = { key: 'value' } as const;
Expand Down Expand Up @@ -198,6 +204,18 @@ ruleTester.run('consistent-type-assertions', rule, {
messageId: 'angle-bracket',
line: 11,
},
{
messageId: 'angle-bracket',
line: 12,
},
{
messageId: 'angle-bracket',
line: 13,
},
{
messageId: 'angle-bracket',
line: 14,
},
],
}),
...batchedSingleLineTests({
Expand Down Expand Up @@ -248,6 +266,18 @@ ruleTester.run('consistent-type-assertions', rule, {
messageId: 'as',
line: 11,
},
{
messageId: 'as',
line: 12,
},
{
messageId: 'as',
line: 13,
},
{
messageId: 'as',
line: 14,
},
],
output: AS_TESTS,
}),
Expand Down Expand Up @@ -295,6 +325,18 @@ ruleTester.run('consistent-type-assertions', rule, {
messageId: 'never',
line: 10,
},
{
messageId: 'never',
line: 11,
},
{
messageId: 'never',
line: 12,
},
{
messageId: 'never',
line: 13,
},
],
}),
...batchedSingleLineTests({
Expand Down Expand Up @@ -341,6 +383,18 @@ ruleTester.run('consistent-type-assertions', rule, {
messageId: 'never',
line: 10,
},
{
messageId: 'never',
line: 11,
},
{
messageId: 'never',
line: 12,
},
{
messageId: 'never',
line: 13,
},
],
}),
...batchedSingleLineTests({
Expand Down