Skip to content

Commit

Permalink
Fix prevent-abbreviations breaking shorthand properties in assignme…
Browse files Browse the repository at this point in the history
…nt patterns (#268)
  • Loading branch information
futpib authored and sindresorhus committed Mar 28, 2019
1 parent 79a8cd6 commit d30751a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rules/prevent-abbreviations.js
Expand Up @@ -402,9 +402,19 @@ const shouldFix = variable => {

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

const isAssignmentPatternShorthandPropertyIdentifier = identifier => {
return identifier.parent.type === 'AssignmentPattern' &&
identifier.parent.left === identifier &&
identifier.parent.parent.type === 'Property' &&
identifier.parent.parent.key === identifier &&
identifier.parent.parent.value === identifier.parent &&
identifier.parent.parent.shorthand;
};

const isShorthandImportIdentifier = identifier => {
return identifier.parent.type === 'ImportSpecifier' &&
identifier.parent.imported.name === identifier.name &&
Expand All @@ -418,7 +428,7 @@ const isShorthandExportIdentifier = identifier => {
};

const fixIdentifier = (fixer, replacement) => identifier => {
if (isShorthandPropertyIdentifier(identifier)) {
if (isShorthandPropertyIdentifier(identifier) || isAssignmentPatternShorthandPropertyIdentifier(identifier)) {
return fixer.replaceText(identifier, `${identifier.name}: ${replacement}`);
}

Expand Down
22 changes: 22 additions & 0 deletions test/prevent-abbreviations.js
Expand Up @@ -580,6 +580,12 @@ ruleTester.run('prevent-abbreviations', rule, {
options: customOptions,
errors: createErrors()
},
{
code: 'const {err} = foo;',
output: 'const {err: error} = foo;',
options: customOptions,
errors: createErrors()
},

noFixingTestCase({
code: 'const foo = {err: 1}',
Expand Down Expand Up @@ -890,6 +896,22 @@ ruleTester.run('prevent-abbreviations', rule, {
}
`,
errors: createErrors()
},

{
code: `
function unicorn(unicorn) {
const {docs = {}} = unicorn;
return docs;
}
`,
output: `
function unicorn(unicorn) {
const {docs: documents = {}} = unicorn;
return documents;
}
`,
errors: createErrors()
}
]
});
Expand Down

0 comments on commit d30751a

Please sign in to comment.