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): always ignore assignments in no-unnecessary-type-assertion #3235

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 17 additions & 12 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Expand Up @@ -137,19 +137,24 @@ export default util.createRule<Options, MessageIds>({
TSNonNullExpression(node): void {
if (
node.parent?.type === AST_NODE_TYPES.AssignmentExpression &&
node.parent?.operator === '=' &&
node.parent.left === node
node.parent.operator === '='
) {
context.report({
node,
messageId: 'contextuallyUnnecessary',
fix(fixer) {
return fixer.removeRange([
node.expression.range[1],
node.range[1],
]);
},
});
if (node.parent.left === node) {
context.report({
node,
messageId: 'contextuallyUnnecessary',
fix(fixer) {
return fixer.removeRange([
node.expression.range[1],
node.range[1],
]);
},
});
}
// for all other = assignments we ignore non-null checks
// this is because non-null assertions can change the type-flow of the code
// so whilst they might be unnecessary for the assignment - they are necessary
// for following code
return;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Expand Up @@ -202,6 +202,12 @@ let a: { b?: string } | undefined;
a!.b = '';
`,
},
`
let value: number | undefined;
let values: number[] = [];

value = values.pop()!;
`,
],

invalid: [
Expand Down Expand Up @@ -477,14 +483,10 @@ y! = 0;
output: `
let x: number | undefined;
let y: number | undefined;
y = x;
y = x!;
y = 0;
`,
errors: [
{
messageId: 'contextuallyUnnecessary',
line: 4,
},
{
messageId: 'contextuallyUnnecessary',
line: 5,
Expand Down