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 prevent-abbreviations fixer bug #444

Merged
merged 3 commits into from Nov 29, 2019
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
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