Skip to content

fix(eslint-plugin): [no-unnecessary-type-assertion] provide valid fixes for assertions with extra tokens before as keyword #8326

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

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';
Expand Down Expand Up @@ -263,23 +263,38 @@ export default createRule<Options, MessageIds>({
messageId: 'unnecessaryAssertion',
fix(fixer) {
if (node.type === AST_NODE_TYPES.TSTypeAssertion) {
const openingAngleBracket = sourceCode.getTokenBefore(
node.typeAnnotation,
token =>
token.type === AST_TOKEN_TYPES.Punctuator &&
token.value === '<',
)!;
const closingAngleBracket = sourceCode.getTokenAfter(
node.typeAnnotation,
);
return closingAngleBracket?.value === '>'
? fixer.removeRange([
node.range[0],
closingAngleBracket.range[1],
])
: null;
token =>
token.type === AST_TOKEN_TYPES.Punctuator &&
token.value === '>',
)!;
// < ( number ) > ( 3 + 5 )
// ^---remove---^
Comment on lines +278 to +279
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-Actionable] Somehow my brain immediately parsed this as an emoticon smiley... amused.

< ( number ) >
^---remove---^

return fixer.removeRange([
openingAngleBracket.range[0],
closingAngleBracket.range[1],
]);
}
return fixer.removeRange([
node.expression.range[1] +
(node.expression.type === AST_NODE_TYPES.CallExpression
? 0
: 1),
node.range[1],
]);
// `as` is always present in TSAsExpression
const asToken = sourceCode.getTokenAfter(
node.expression,
token =>
token.type === AST_TOKEN_TYPES.Identifier &&
token.value === 'as',
)!;
const tokenBeforeAs = sourceCode.getTokenBefore(asToken, {
includeComments: true,
})!;
// ( 3 + 5 ) as number
// ^--remove--^
return fixer.removeRange([tokenBeforeAs.range[1], node.range[1]]);
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
import { noFormat, RuleTester } from '@typescript-eslint/rule-tester';
import path from 'path';

import rule from '../../src/rules/no-unnecessary-type-assertion';
Expand Down Expand Up @@ -660,5 +660,160 @@ const item = arr[0];
},
],
},
{
code: noFormat`
const foo = ( 3 + 5 ) as number;
`,
output: `
const foo = ( 3 + 5 );
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = ( 3 + 5 ) /*as*/ as number;
`,
output: `
const foo = ( 3 + 5 ) /*as*/;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = ( 3 + 5
) /*as*/ as //as
(
number
);
`,
output: `
const foo = ( 3 + 5
) /*as*/;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = (3 + (5 as number) ) as number;
`,
output: `
const foo = (3 + (5 as number) );
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = 3 + 5/*as*/ as number;
`,
output: `
const foo = 3 + 5/*as*/;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = 3 + 5/*a*/ /*b*/ as number;
`,
output: `
const foo = 3 + 5/*a*/ /*b*/;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = <(number)>(3 + 5);
`,
output: `
const foo = (3 + 5);
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = < ( number ) >( 3 + 5 );
`,
output: `
const foo = ( 3 + 5 );
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = <number> /* a */ (3 + 5);
`,
output: `
const foo = /* a */ (3 + 5);
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
{
code: noFormat`
const foo = <number /* a */>(3 + 5);
`,
output: `
const foo = (3 + 5);
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 2,
column: 13,
},
],
},
],
});