Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-type-assertion] correct bad fix f…
Browse files Browse the repository at this point in the history
…or angle bracket assertion (#3244)
  • Loading branch information
yeonjuan committed Mar 29, 2021
1 parent 1b41d60 commit 265a039
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Expand Up @@ -258,15 +258,21 @@ export default util.createRule<Options, MessageIds>({
node,
messageId: 'unnecessaryAssertion',
fix(fixer) {
return originalNode.kind === ts.SyntaxKind.TypeAssertionExpression
? fixer.removeRange([
node.range[0],
node.expression.range[0] - 1,
])
: fixer.removeRange([
node.expression.range[1] + 1,
node.range[1],
]);
if (originalNode.kind === ts.SyntaxKind.TypeAssertionExpression) {
const closingAngleBracket = sourceCode.getTokenAfter(
node.typeAnnotation,
);
return closingAngleBracket?.value === '>'
? fixer.removeRange([
node.range[0],
closingAngleBracket.range[1],
])
: null;
}
return fixer.removeRange([
node.expression.range[1] + 1,
node.range[1],
]);
},
});
}
Expand Down
Expand Up @@ -358,6 +358,22 @@ function foo<T extends string>(bar: T) {
},
{
code: `
declare const foo: Foo;
const bar = <Foo>foo;
`,
output: `
declare const foo: Foo;
const bar = foo;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 3,
},
],
},
{
code: `
declare function nonNull(s: string | null);
let s: string | null = null;
nonNull(s!);
Expand Down

0 comments on commit 265a039

Please sign in to comment.