-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
79 lines (59 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {operator} from 'putout';
const {remove} = operator;
const getNode = (a) => a.node;
export const report = () => `Merge variables`;
export const fix = ({path, vars}) => {
path.node.declarations = vars.map(getNode);
for (const path of vars.slice(1)) {
remove(path.parentPath);
}
};
export const traverse = ({push, uplist}) => ({
VariableDeclarator: (path) => {
if (path.parentPath.node.declarations.length !== 1)
return;
if (path.parentPath.node.kind === 'let')
return;
const initPath = path.get('init');
if (initPath.isAwaitExpression())
return;
if (initPath.isNewExpression())
return;
if (initPath.isTemplateLiteral())
return;
if (path.parentPath.parentPath.isSwitchCase())
return;
if (path.parentPath.parentPath.isForOfStatement())
return;
uplist(path.scope.uid, path);
},
Program: {
exit() {
for (const allVars of uplist()) {
if (allVars.length < 2)
continue;
const [path, ...vars] = allVars;
const {kind} = path.parentPath.node;
for (const [index, path] of vars.entries()) {
const {node} = path;
if (kind === 'const' && !node.init) {
vars.splice(index, 1);
continue;
}
const prev = path.parentPath.getPrevSibling();
//if (prev.node && !prev.isVariableDeclaration()) {
if (!prev.isVariableDeclaration()) {
vars.splice(index, 1);
continue;
}
}
if (!vars.length)
continue;
push({
path: path.parentPath,
vars: [path, ...vars],
});
}
},
},
});