Skip to content

Commit

Permalink
feature: @putout/plugin-apply-shorthand-properties: ImportSpecifier: …
Browse files Browse the repository at this point in the history
…<StringLiteral>imported.value === <Identifier>local.name'
  • Loading branch information
coderaiser committed Nov 28, 2024
1 parent 86aaf50 commit be091a1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/plugin-apply-shorthand-properties/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
'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);

path.node.shorthand = true;
};

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {a} from 'a';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {'a' as a} from 'a';

0 comments on commit be091a1

Please sign in to comment.