Skip to content

Commit

Permalink
fix(eslint-plugin): [no-dynamic-delete] correct invalid fixer for ide…
Browse files Browse the repository at this point in the history
…ntifiers (#1244)
  • Loading branch information
a-tarasyuk authored and bradzacher committed Nov 20, 2019
1 parent 6cfd468 commit 5b1300d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 2 additions & 6 deletions packages/eslint-plugin/src/rules/no-dynamic-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ export default util.createRule({
) {
return createPropertyReplacement(
member.property,
member.property.value,
`.${member.property.value}`,
);
}

if (member.property.type === AST_NODE_TYPES.Identifier) {
return createPropertyReplacement(member.property, member.property.name);
}

return undefined;
}

Expand Down Expand Up @@ -69,7 +65,7 @@ export default util.createRule({
replacement: string,
) {
return (fixer: TSESLint.RuleFixer): TSESLint.RuleFix =>
fixer.replaceTextRange(getTokenRange(property), `.${replacement}`);
fixer.replaceTextRange(getTokenRange(property), replacement);
}

function getTokenRange(property: TSESTree.Expression): [number, number] {
Expand Down
23 changes: 23 additions & 0 deletions packages/eslint-plugin/tests/rules/no-dynamic-delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ ruleTester.run('no-dynamic-delete', rule, {
code: `const container: { [i: string]: 0 } = {};
delete container['aa' + 'b'];`,
errors: [{ messageId: 'dynamicDelete' }],
output: `const container: { [i: string]: 0 } = {};
delete container['aa' + 'b'];`,
},
{
code: `const container: { [i: string]: 0 } = {};
Expand All @@ -64,16 +66,22 @@ ruleTester.run('no-dynamic-delete', rule, {
code: `const container: { [i: string]: 0 } = {};
delete container[-Infinity];`,
errors: [{ messageId: 'dynamicDelete' }],
output: `const container: { [i: string]: 0 } = {};
delete container[-Infinity];`,
},
{
code: `const container: { [i: string]: 0 } = {};
delete container[+Infinity];`,
errors: [{ messageId: 'dynamicDelete' }],
output: `const container: { [i: string]: 0 } = {};
delete container[+Infinity];`,
},
{
code: `const container: { [i: string]: 0 } = {};
delete container[NaN];`,
errors: [{ messageId: 'dynamicDelete' }],
output: `const container: { [i: string]: 0 } = {};
delete container[NaN];`,
},
{
code: `const container: { [i: string]: 0 } = {};
Expand All @@ -94,11 +102,26 @@ ruleTester.run('no-dynamic-delete', rule, {
const name = 'name';
delete container[name];`,
errors: [{ messageId: 'dynamicDelete' }],
output: `const container: { [i: string]: 0 } = {};
const name = 'name';
delete container[name];`,
},
{
code: `const container: { [i: string]: 0 } = {};
const getName = () => 'aaa';
delete container[getName()];`,
output: `const container: { [i: string]: 0 } = {};
const getName = () => 'aaa';
delete container[getName()];`,
errors: [{ messageId: 'dynamicDelete' }],
},
{
code: `const container: { [i: string]: 0 } = {};
const name = { foo: { bar: 'bar' } };
delete container[name.foo.bar];`,
output: `const container: { [i: string]: 0 } = {};
const name = { foo: { bar: 'bar' } };
delete container[name.foo.bar];`,
errors: [{ messageId: 'dynamicDelete' }],
},
],
Expand Down

0 comments on commit 5b1300d

Please sign in to comment.