Skip to content

Commit

Permalink
Fix prevent-abbreviations fixer bug (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and sindresorhus committed Nov 29, 2019
1 parent 20dfb65 commit fa8c80e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
15 changes: 12 additions & 3 deletions rules/prevent-abbreviations.js
Expand Up @@ -364,17 +364,26 @@ const shouldFix = variable => {
return !variableIdentifiers(variable).some(isExportedIdentifier);
};

const isIdentifierKeyOfNode = (identifier, node) =>
node.key === identifier ||
// In `babel-eslint` parent.key is not reference of identifier
// https://github.com/babel/babel-eslint/issues/809
(
node.key.type === identifier.type &&
node.key.name === identifier.name
);

const isShorthandPropertyIdentifier = identifier => {
return identifier.parent.type === 'Property' &&
identifier.parent.key === identifier &&
identifier.parent.shorthand;
identifier.parent.shorthand &&
isIdentifierKeyOfNode(identifier, identifier.parent);
};

const isAssignmentPatternShorthandPropertyIdentifier = identifier => {
return identifier.parent.type === 'AssignmentPattern' &&
identifier.parent.left === identifier &&
identifier.parent.parent.type === 'Property' &&
identifier.parent.parent.key === identifier &&
isIdentifierKeyOfNode(identifier, identifier.parent.parent) &&
identifier.parent.parent.value === identifier.parent &&
identifier.parent.parent.shorthand;
};
Expand Down
34 changes: 33 additions & 1 deletion test/prevent-abbreviations.js
Expand Up @@ -643,6 +643,12 @@ ruleTester.run('prevent-abbreviations', rule, {
options: customOptions,
errors: createErrors()
},
{
code: 'err => ({err})',
output: 'error => ({err: error})',
options: customOptions,
errors: createErrors()
},
{
code: 'const {err} = foo;',
output: 'const {err: error} = foo;',
Expand Down Expand Up @@ -1389,8 +1395,34 @@ babelRuleTester.run('prevent-abbreviations', rule, {
}
`
],

invalid: [
{
code: outdent`
function unicorn(unicorn) {
const {prop = {}} = unicorn;
return property;
}
`,
output: outdent`
function unicorn(unicorn) {
const {prop: property = {}} = unicorn;
return property;
}
`,
errors: createErrors()
},
{
code: '({err}) => err',
output: '({err: error}) => error',
options: customOptions,
errors: createErrors()
},
{
code: 'err => ({err})',
output: 'error => ({err: error})',
options: customOptions,
errors: createErrors()
},
{
code: 'Foo.customProps = {}',
options: checkPropertiesOptions,
Expand Down

0 comments on commit fa8c80e

Please sign in to comment.