From fa8c80ec776fefbc1915bae3a8f8b7caa95717be Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 29 Nov 2019 17:17:26 +0800 Subject: [PATCH] Fix `prevent-abbreviations` fixer bug (#444) --- rules/prevent-abbreviations.js | 15 ++++++++++++--- test/prevent-abbreviations.js | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index 8c4e43123b..dff5c59090 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -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; }; diff --git a/test/prevent-abbreviations.js b/test/prevent-abbreviations.js index e7724fdd8b..279309cb2b 100644 --- a/test/prevent-abbreviations.js +++ b/test/prevent-abbreviations.js @@ -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;', @@ -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,