Skip to content

Commit fa8c80e

Browse files
fiskersindresorhus
authored andcommitted
Fix prevent-abbreviations fixer bug (#444)
1 parent 20dfb65 commit fa8c80e

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

rules/prevent-abbreviations.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,26 @@ const shouldFix = variable => {
364364
return !variableIdentifiers(variable).some(isExportedIdentifier);
365365
};
366366

367+
const isIdentifierKeyOfNode = (identifier, node) =>
368+
node.key === identifier ||
369+
// In `babel-eslint` parent.key is not reference of identifier
370+
// https://github.com/babel/babel-eslint/issues/809
371+
(
372+
node.key.type === identifier.type &&
373+
node.key.name === identifier.name
374+
);
375+
367376
const isShorthandPropertyIdentifier = identifier => {
368377
return identifier.parent.type === 'Property' &&
369-
identifier.parent.key === identifier &&
370-
identifier.parent.shorthand;
378+
identifier.parent.shorthand &&
379+
isIdentifierKeyOfNode(identifier, identifier.parent);
371380
};
372381

373382
const isAssignmentPatternShorthandPropertyIdentifier = identifier => {
374383
return identifier.parent.type === 'AssignmentPattern' &&
375384
identifier.parent.left === identifier &&
376385
identifier.parent.parent.type === 'Property' &&
377-
identifier.parent.parent.key === identifier &&
386+
isIdentifierKeyOfNode(identifier, identifier.parent.parent) &&
378387
identifier.parent.parent.value === identifier.parent &&
379388
identifier.parent.parent.shorthand;
380389
};

test/prevent-abbreviations.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,12 @@ ruleTester.run('prevent-abbreviations', rule, {
643643
options: customOptions,
644644
errors: createErrors()
645645
},
646+
{
647+
code: 'err => ({err})',
648+
output: 'error => ({err: error})',
649+
options: customOptions,
650+
errors: createErrors()
651+
},
646652
{
647653
code: 'const {err} = foo;',
648654
output: 'const {err: error} = foo;',
@@ -1389,8 +1395,34 @@ babelRuleTester.run('prevent-abbreviations', rule, {
13891395
}
13901396
`
13911397
],
1392-
13931398
invalid: [
1399+
{
1400+
code: outdent`
1401+
function unicorn(unicorn) {
1402+
const {prop = {}} = unicorn;
1403+
return property;
1404+
}
1405+
`,
1406+
output: outdent`
1407+
function unicorn(unicorn) {
1408+
const {prop: property = {}} = unicorn;
1409+
return property;
1410+
}
1411+
`,
1412+
errors: createErrors()
1413+
},
1414+
{
1415+
code: '({err}) => err',
1416+
output: '({err: error}) => error',
1417+
options: customOptions,
1418+
errors: createErrors()
1419+
},
1420+
{
1421+
code: 'err => ({err})',
1422+
output: 'error => ({err: error})',
1423+
options: customOptions,
1424+
errors: createErrors()
1425+
},
13941426
{
13951427
code: 'Foo.customProps = {}',
13961428
options: checkPropertiesOptions,

0 commit comments

Comments
 (0)