Skip to content

Commit

Permalink
fix(eslint-plugin): [consistent-type-assertions] wrap object return v…
Browse files Browse the repository at this point in the history
…alue with parentheses
  • Loading branch information
dora1998 committed Apr 11, 2023
1 parent a4b633b commit 8b43da5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
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, ')');
}

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

0 comments on commit 8b43da5

Please sign in to comment.