diff --git a/packages/plugin-apply-shorthand-properties/README.md b/packages/plugin-apply-shorthand-properties/README.md index 795976251..e42eac97b 100644 --- a/packages/plugin-apply-shorthand-properties/README.md +++ b/packages/plugin-apply-shorthand-properties/README.md @@ -32,12 +32,16 @@ npm i @putout/plugin-apply-shorthand-properties -D ### ❌ Example of incorrect code ```js +import {'b' as b} from 'b'; + const {a: a} = b; ``` ### ✅ Example of correct code ```js +import {b} from 'b'; + const {a} = b; ``` diff --git a/packages/plugin-apply-shorthand-properties/lib/apply-shorthand-properties.js b/packages/plugin-apply-shorthand-properties/lib/apply-shorthand-properties.js index 1ece3ab62..73365f450 100644 --- a/packages/plugin-apply-shorthand-properties/lib/apply-shorthand-properties.js +++ b/packages/plugin-apply-shorthand-properties/lib/apply-shorthand-properties.js @@ -1,11 +1,18 @@ 'use strict'; -const {operator} = require('putout'); +const {types, operator} = require('putout'); const {findBinding, rename} = operator; +const {isImportSpecifier} = types; + module.exports.report = () => `Use shorthand properties`; module.exports.fix = ({path, from, to, toRename}) => { + if (isImportSpecifier(path)) { + path.node.imported = path.node.local; + return; + } + if (toRename) rename(path, from, to); @@ -13,6 +20,12 @@ module.exports.fix = ({path, from, to, toRename}) => { }; module.exports.traverse = ({push, options}) => ({ + ImportSpecifier(path) { + if (path.node.imported.value === path.node.local.name) + push({ + path, + }); + }, '__object'(path) { for (const propPath of path.get('properties')) { const {shorthand} = propPath.node; diff --git a/packages/plugin-apply-shorthand-properties/test/apply-shorthand-properties.js b/packages/plugin-apply-shorthand-properties/test/apply-shorthand-properties.js index 0c5258019..5b391fffb 100644 --- a/packages/plugin-apply-shorthand-properties/test/apply-shorthand-properties.js +++ b/packages/plugin-apply-shorthand-properties/test/apply-shorthand-properties.js @@ -43,6 +43,11 @@ test('plugin-apply-shorthand-properties: transform: destructuring', (t) => { t.end(); }); +test('plugin-apply-shorthand-properties: transform: as', (t) => { + t.transform('as'); + t.end(); +}); + test('plugin-apply-shorthand-properties: transform: rename-and-destructuring', (t) => { t.transform('rename-and-destructuring'); t.end(); diff --git a/packages/plugin-apply-shorthand-properties/test/fixture/as-fix.js b/packages/plugin-apply-shorthand-properties/test/fixture/as-fix.js new file mode 100644 index 000000000..32efb9c4a --- /dev/null +++ b/packages/plugin-apply-shorthand-properties/test/fixture/as-fix.js @@ -0,0 +1 @@ +import {a} from 'a'; diff --git a/packages/plugin-apply-shorthand-properties/test/fixture/as.js b/packages/plugin-apply-shorthand-properties/test/fixture/as.js new file mode 100644 index 000000000..36183b464 --- /dev/null +++ b/packages/plugin-apply-shorthand-properties/test/fixture/as.js @@ -0,0 +1 @@ +import {'a' as a} from 'a';