Skip to content

Commit d30751a

Browse files
futpibsindresorhus
authored andcommitted
Fix prevent-abbreviations breaking shorthand properties in assignment patterns (#268)
1 parent 79a8cd6 commit d30751a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

rules/prevent-abbreviations.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,19 @@ const shouldFix = variable => {
402402

403403
const isShorthandPropertyIdentifier = identifier => {
404404
return identifier.parent.type === 'Property' &&
405+
identifier.parent.key === identifier &&
405406
identifier.parent.shorthand;
406407
};
407408

409+
const isAssignmentPatternShorthandPropertyIdentifier = identifier => {
410+
return identifier.parent.type === 'AssignmentPattern' &&
411+
identifier.parent.left === identifier &&
412+
identifier.parent.parent.type === 'Property' &&
413+
identifier.parent.parent.key === identifier &&
414+
identifier.parent.parent.value === identifier.parent &&
415+
identifier.parent.parent.shorthand;
416+
};
417+
408418
const isShorthandImportIdentifier = identifier => {
409419
return identifier.parent.type === 'ImportSpecifier' &&
410420
identifier.parent.imported.name === identifier.name &&
@@ -418,7 +428,7 @@ const isShorthandExportIdentifier = identifier => {
418428
};
419429

420430
const fixIdentifier = (fixer, replacement) => identifier => {
421-
if (isShorthandPropertyIdentifier(identifier)) {
431+
if (isShorthandPropertyIdentifier(identifier) || isAssignmentPatternShorthandPropertyIdentifier(identifier)) {
422432
return fixer.replaceText(identifier, `${identifier.name}: ${replacement}`);
423433
}
424434

test/prevent-abbreviations.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ ruleTester.run('prevent-abbreviations', rule, {
580580
options: customOptions,
581581
errors: createErrors()
582582
},
583+
{
584+
code: 'const {err} = foo;',
585+
output: 'const {err: error} = foo;',
586+
options: customOptions,
587+
errors: createErrors()
588+
},
583589

584590
noFixingTestCase({
585591
code: 'const foo = {err: 1}',
@@ -890,6 +896,22 @@ ruleTester.run('prevent-abbreviations', rule, {
890896
}
891897
`,
892898
errors: createErrors()
899+
},
900+
901+
{
902+
code: `
903+
function unicorn(unicorn) {
904+
const {docs = {}} = unicorn;
905+
return docs;
906+
}
907+
`,
908+
output: `
909+
function unicorn(unicorn) {
910+
const {docs: documents = {}} = unicorn;
911+
return documents;
912+
}
913+
`,
914+
errors: createErrors()
893915
}
894916
]
895917
});

0 commit comments

Comments
 (0)