Skip to content

Commit c1e3c40

Browse files
committed
feature: @putout/plugin-extract-keywords-from-variables: add support of import, let, var
1 parent 65ea420 commit c1e3c40

File tree

5 files changed

+66
-17
lines changed

5 files changed

+66
-17
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"rules": {
3-
"extract-keywords-from-variables": "on"
3+
"extract-keywords-from-variables": "off"
44
}
55
}

packages/plugin-extract-keywords-from-variables/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
>
88
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token)
99
10-
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to extract `keywords` from variables. Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/fcaedaa9daf7f3a771274aca0da9ab1b).
10+
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to extract `keywords` from variables. Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/fcaedaa9daf7f3a771274aca0da9ab1b/4496811b3f092b8124703fd69c84c51be86cf90a).
1111

1212
## Install
1313

packages/plugin-extract-keywords-from-variables/lib/extract-keywords-from-variables.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
'use strict';
22

33
const {types, operator} = require('putout');
4-
const {remove, replaceWith} = operator;
4+
const buildDeclaration = (type) => (path) => {
5+
const {left, right} = path.node.expression;
6+
replaceWith(path, VariableDeclaration(type, [VariableDeclarator(left, right)]));
7+
};
8+
59
const {
10+
ImportDefaultSpecifier,
11+
ImportDeclaration,
12+
ExportNamedDeclaration,
613
VariableDeclarator,
714
VariableDeclaration,
8-
ExportNamedDeclaration,
915
} = types;
1016

17+
const {remove, replaceWith} = operator;
18+
1119
const keywords = [
1220
'export',
1321
'const',
22+
'var',
23+
'let',
24+
'import',
1425
];
1526

27+
const builders = {
28+
const: buildDeclaration('const'),
29+
var: buildDeclaration('var'),
30+
let: buildDeclaration('let'),
31+
import: buildImport,
32+
export: buildExport,
33+
};
34+
1635
module.exports.report = () => `Extract 'export' from variable`;
1736

1837
module.exports.fix = ({path, nextPath}) => {
19-
if (path.node.id.name === 'export')
20-
replaceWith(nextPath, ExportNamedDeclaration(nextPath.node));
21-
22-
if (path.node.id.name === 'const')
23-
replaceWith(nextPath, VariableDeclaration('const', [VariableDeclarator(nextPath.node.expression.left, nextPath.node.expression.right)]));
38+
const {name} = path.node.id;
2439

40+
builders[name](nextPath);
2541
remove(path);
2642
};
2743

2844
module.exports.traverse = ({push}) => ({
2945
VariableDeclarator(path) {
30-
if (!keywords.includes(path.node.id.name))
46+
const {name} = path.node.id;
47+
48+
if (!keywords.includes(name))
3149
return;
3250

3351
const topPath = getTopPath(path);
@@ -39,7 +57,7 @@ module.exports.traverse = ({push}) => ({
3957
nextPath,
4058
});
4159

42-
if (nextPath.isExpressionStatement() && nextPath.get('expression').isAssignmentExpression())
60+
if (nextPath.isExpressionStatement())
4361
return push({
4462
path,
4563
nextPath,
@@ -53,3 +71,18 @@ function getTopPath(path) {
5371

5472
return path.parentPath;
5573
}
74+
75+
function buildExport(path) {
76+
replaceWith(path, ExportNamedDeclaration(path.node));
77+
}
78+
79+
function buildImport(path) {
80+
const fromPath = path.getNextSibling();
81+
const sourcePath = fromPath.getNextSibling();
82+
const source = sourcePath.node.expression;
83+
const local = path.node.expression;
84+
85+
replaceWith(path, ImportDeclaration([ImportDefaultSpecifier(local, local)], source));
86+
remove(sourcePath);
87+
remove(fromPath);
88+
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle';
22
export const isTemplateTail = (a) => a?.type === 'TemplateTail';
33

4-
export const a = 1;
4+
export const a = (a) => a?.type === 'TemplateMiddle';
55
const b = 5;
66

7-
const a1 = 1;
7+
const a1 = 2;
88

9-
const b2 = 5;
9+
import x from 'b';
10+
11+
const z1 = 1;
12+
13+
var z2 = 2;
14+
15+
const a4 = 1;
16+
17+
let b4 = 2;
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle',
22
export const isTemplateTail = (a) => a?.type === 'TemplateTail';
33

4-
export const a = 1;
4+
export const a = (a) => a?.type === 'TemplateMiddle',
55
const b = 5;
66

7-
const a1 = 1,
8-
const b2 = 5;
7+
const a1 = 2,
8+
import x from 'b';
9+
10+
11+
const z1 = 1,
12+
var z2 = 2;
13+
14+
const a4 = 1,
15+
let b4 = 2;
16+

0 commit comments

Comments
 (0)