Skip to content

Commit

Permalink
fix(eslint-plugin): [consistent-type-assertions] wrap object type ass…
Browse files Browse the repository at this point in the history
…ertion with parentheses
  • Loading branch information
dora1998 committed Apr 11, 2023
1 parent a4b633b commit d2b2a32
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
20 changes: 14 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,24 @@ 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(
);
yield fixer.insertTextAfter(
node,
` as ${getTextWithParentheses(node.typeAnnotation)}`,
),
]
);

if (
node.expression.type === AST_NODE_TYPES.ObjectExpression &&
node.parent?.type === AST_NODE_TYPES.ArrowFunctionExpression
) {
yield fixer.insertTextBefore(node, '(');
yield fixer.insertTextAfter(node, ')');
}
}
: undefined,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ 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>('string');`;

const ANGLE_BRACKET_TESTS = `${ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE}
const x = <const>{ key: 'value' };
Expand All @@ -29,7 +31,9 @@ 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 = () => ('string') as Foo;`;

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

0 comments on commit d2b2a32

Please sign in to comment.