@putout/engine-runner 13.6.0 → 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -7
- package/lib/merge-visitors.js +8 -1
- package/lib/replace/index.js +3 -3
- package/lib/run-fix.js +5 -1
- package/lib/store.js +25 -19
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -65,6 +65,8 @@ module.exports.report = () => 'debugger should not be used';
|
|
|
65
65
|
module.exports.replace = () => ({
|
|
66
66
|
debugger: '',
|
|
67
67
|
});
|
|
68
|
+
|
|
69
|
+
// debugger; alert(); -> alert();
|
|
68
70
|
```
|
|
69
71
|
|
|
70
72
|
Templates:
|
|
@@ -75,6 +77,8 @@ module.exports.report = () => 'any message here';
|
|
|
75
77
|
module.exports.replace = () => ({
|
|
76
78
|
'var __a = 1': 'const __a = 1',
|
|
77
79
|
});
|
|
80
|
+
|
|
81
|
+
// var x = 1; -> const x = 1;
|
|
78
82
|
```
|
|
79
83
|
|
|
80
84
|
A couple variables example:
|
|
@@ -85,6 +89,8 @@ module.exports.report = () => 'any message here';
|
|
|
85
89
|
module.exports.replace = () => ({
|
|
86
90
|
'const __a = __b': 'const __b = __a',
|
|
87
91
|
});
|
|
92
|
+
|
|
93
|
+
// const hello = world; -> const world = hello;
|
|
88
94
|
```
|
|
89
95
|
|
|
90
96
|
#### Processing of node using functions
|
|
@@ -102,6 +108,8 @@ module.exports.replace = () => ({
|
|
|
102
108
|
return '';
|
|
103
109
|
},
|
|
104
110
|
});
|
|
111
|
+
|
|
112
|
+
// for (a of b) {}; alert(); -> alert();
|
|
105
113
|
```
|
|
106
114
|
|
|
107
115
|
Update node:
|
|
@@ -111,11 +119,13 @@ module.exports.report = () => 'any message here';
|
|
|
111
119
|
|
|
112
120
|
module.exports.replace = () => ({
|
|
113
121
|
'for (const __a of __array) __c': ({__a, __array, __c}, path) => {
|
|
114
|
-
// update __array elements
|
|
122
|
+
// update __array elements
|
|
115
123
|
path.node.right.elements = [];
|
|
116
124
|
return path;
|
|
117
125
|
},
|
|
118
126
|
});
|
|
127
|
+
|
|
128
|
+
// for (const a of [1, 2, 3]) {}; -> for (const a of []) {};
|
|
119
129
|
```
|
|
120
130
|
|
|
121
131
|
Update node using template variables:
|
|
@@ -129,6 +139,8 @@ module.exports.replace = () => ({
|
|
|
129
139
|
return 'for (const x of y) z';
|
|
130
140
|
},
|
|
131
141
|
});
|
|
142
|
+
|
|
143
|
+
// for (const item of array) {}; -> for (const x of y) z;
|
|
132
144
|
```
|
|
133
145
|
|
|
134
146
|
### Includer
|
|
@@ -224,8 +236,8 @@ const ast = parse('const m = "hi"; debugger');
|
|
|
224
236
|
const places = runPlugins({
|
|
225
237
|
ast,
|
|
226
238
|
shebang: false, // default
|
|
227
|
-
fix:
|
|
228
|
-
fixCount:
|
|
239
|
+
fix: false, // default
|
|
240
|
+
fixCount: 0, // default
|
|
229
241
|
plugins,
|
|
230
242
|
parser: 'babel', // default
|
|
231
243
|
});
|
|
@@ -234,9 +246,10 @@ const places = runPlugins({
|
|
|
234
246
|
## Stores
|
|
235
247
|
|
|
236
248
|
Stores is preferred way of keeping 🐊**Putout** data, `traverse` init function called only once, and any other way
|
|
237
|
-
of handling variables will most likely will lead to bugs. There is
|
|
249
|
+
of handling variables will most likely will lead to bugs. There is a couple store types:
|
|
238
250
|
|
|
239
251
|
- ✅`listStore`;
|
|
252
|
+
- ✅`pathStore`;
|
|
240
253
|
- ✅`store`;
|
|
241
254
|
- ✅`upstore`;
|
|
242
255
|
- ✅`uplist`;
|
|
@@ -275,6 +288,34 @@ module.exports.traverse = ({listStore}) => ({
|
|
|
275
288
|
});
|
|
276
289
|
```
|
|
277
290
|
|
|
291
|
+
### `pathStore`
|
|
292
|
+
|
|
293
|
+
When you want additional check that `path` not removed.
|
|
294
|
+
|
|
295
|
+
```js
|
|
296
|
+
debugger;
|
|
297
|
+
const hello = '';
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
Let's process it!
|
|
301
|
+
|
|
302
|
+
```js
|
|
303
|
+
module.exports.traverse = ({pathStore}) => ({
|
|
304
|
+
'debugger'(path) {
|
|
305
|
+
pathStore(path);
|
|
306
|
+
path.remove();
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
Program: {
|
|
310
|
+
exit() {
|
|
311
|
+
console.log(listStore());
|
|
312
|
+
// returns
|
|
313
|
+
[];
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
```
|
|
318
|
+
|
|
278
319
|
### `store`
|
|
279
320
|
|
|
280
321
|
When you need `key-value` use `store`:
|
|
@@ -349,14 +390,14 @@ When you need to update named arrays:
|
|
|
349
390
|
module.exports.traverse = ({uplist, push}) => ({
|
|
350
391
|
'const __object = __a.__b': (fullPath) => {
|
|
351
392
|
const {__a, __b} = getTemplateValues(fullPath, 'const __object = __a.__b');
|
|
352
|
-
const
|
|
353
|
-
const {uid} =
|
|
393
|
+
const initPath = fullPath.get('declarations.0.init');
|
|
394
|
+
const {uid} = initPath.scope;
|
|
354
395
|
|
|
355
396
|
if (isIdentifier(__a) || isCallExpression(__a)) {
|
|
356
397
|
const {code} = generate(__a);
|
|
357
398
|
const id = `${uid}-${code}`;
|
|
358
399
|
|
|
359
|
-
return uplist(id,
|
|
400
|
+
return uplist(id, initPath);
|
|
360
401
|
}
|
|
361
402
|
},
|
|
362
403
|
'Program': {
|
package/lib/merge-visitors.js
CHANGED
|
@@ -11,6 +11,7 @@ const {
|
|
|
11
11
|
mapStore,
|
|
12
12
|
upStore,
|
|
13
13
|
upListStore,
|
|
14
|
+
pathStore,
|
|
14
15
|
} = require('./store');
|
|
15
16
|
|
|
16
17
|
const shouldSkip = (a) => !a.parent;
|
|
@@ -40,6 +41,7 @@ module.exports = (pluginsToMerge, {fix, shebang, template}) => {
|
|
|
40
41
|
store,
|
|
41
42
|
upstore,
|
|
42
43
|
listStore,
|
|
44
|
+
pathStore,
|
|
43
45
|
uplist,
|
|
44
46
|
} = getStore(plugin, {
|
|
45
47
|
fix,
|
|
@@ -55,6 +57,7 @@ module.exports = (pluginsToMerge, {fix, shebang, template}) => {
|
|
|
55
57
|
push,
|
|
56
58
|
store,
|
|
57
59
|
listStore,
|
|
60
|
+
pathStore,
|
|
58
61
|
upstore,
|
|
59
62
|
uplist,
|
|
60
63
|
generate,
|
|
@@ -62,7 +65,7 @@ module.exports = (pluginsToMerge, {fix, shebang, template}) => {
|
|
|
62
65
|
});
|
|
63
66
|
|
|
64
67
|
if (!visitor)
|
|
65
|
-
throw Error(
|
|
68
|
+
throw Error(`☝️ Visitors cannot be empty in "${rule}"`);
|
|
66
69
|
|
|
67
70
|
assign(options, {
|
|
68
71
|
include: parse('include', plugin, options),
|
|
@@ -94,6 +97,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
|
|
|
94
97
|
const upstore = upStore();
|
|
95
98
|
const placesStore = listStore();
|
|
96
99
|
const uplist = upListStore();
|
|
100
|
+
const paths = pathStore();
|
|
97
101
|
|
|
98
102
|
const push = (path) => {
|
|
99
103
|
const position = getPosition(path, shebang);
|
|
@@ -117,6 +121,8 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
|
|
|
117
121
|
list.clear();
|
|
118
122
|
upstore.clear();
|
|
119
123
|
uplist.clear();
|
|
124
|
+
paths.clear();
|
|
125
|
+
|
|
120
126
|
return placesStore.clear();
|
|
121
127
|
};
|
|
122
128
|
|
|
@@ -127,6 +133,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
|
|
|
127
133
|
listStore: list,
|
|
128
134
|
upstore,
|
|
129
135
|
uplist,
|
|
136
|
+
pathStore: paths,
|
|
130
137
|
};
|
|
131
138
|
}
|
|
132
139
|
|
package/lib/replace/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const isString = (a) => typeof a === 'string';
|
|
4
|
-
|
|
5
3
|
const {template} = require('@putout/engine-parser');
|
|
4
|
+
|
|
6
5
|
const {
|
|
7
6
|
remove,
|
|
8
7
|
replaceWith,
|
|
@@ -14,9 +13,10 @@ const {
|
|
|
14
13
|
setValues,
|
|
15
14
|
} = require('@putout/compare');
|
|
16
15
|
const debug = require('debug')('putout:runner:replace');
|
|
17
|
-
|
|
18
16
|
const maybeArray = require('../maybe-array');
|
|
17
|
+
|
|
19
18
|
const watermark = require('./watermark');
|
|
19
|
+
const isString = (a) => typeof a === 'string';
|
|
20
20
|
|
|
21
21
|
const log = (from, path) => {
|
|
22
22
|
debug.enabled && debug(`${from} -> ${path}\n`);
|
package/lib/run-fix.js
CHANGED
|
@@ -4,7 +4,9 @@ const tryCatch = require('try-catch');
|
|
|
4
4
|
const debug = require('debug')('putout:runner:fix');
|
|
5
5
|
const {enabled} = debug;
|
|
6
6
|
const {stringify} = JSON;
|
|
7
|
+
|
|
7
8
|
const isFn = (a) => typeof a === 'function';
|
|
9
|
+
const getPath = (path) => path.path || path;
|
|
8
10
|
|
|
9
11
|
const tryToFix = (fix, {path, position, options}) => {
|
|
10
12
|
const [e] = tryCatch(fix, path, {options});
|
|
@@ -26,7 +28,9 @@ module.exports = (is, fix, {path, rule, position, options}) => {
|
|
|
26
28
|
if (!is)
|
|
27
29
|
return;
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
if (enabled)
|
|
32
|
+
debug(`fix: ${rule}`, position, getPath(path).toString());
|
|
33
|
+
|
|
30
34
|
validate('fix', fix);
|
|
31
35
|
|
|
32
36
|
tryToFix(fix, {
|
package/lib/store.js
CHANGED
|
@@ -7,26 +7,12 @@ const {
|
|
|
7
7
|
} = Object;
|
|
8
8
|
|
|
9
9
|
const toArray = (a) => Array.from(a);
|
|
10
|
+
const isNotRemoved = (a) => a.node;
|
|
11
|
+
const notRemoved = (a) => toArray(a).filter(isNotRemoved);
|
|
12
|
+
const id = (a) => a;
|
|
10
13
|
|
|
11
|
-
module.exports.listStore = (
|
|
12
|
-
|
|
13
|
-
if (!args.length)
|
|
14
|
-
return Array.from(list);
|
|
15
|
-
|
|
16
|
-
const [a] = args;
|
|
17
|
-
list.add(a);
|
|
18
|
-
|
|
19
|
-
return Array.from(list);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
fn.clear = () => {
|
|
23
|
-
const a = list;
|
|
24
|
-
list = new Set();
|
|
25
|
-
return Array.from(a);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
return fn;
|
|
29
|
-
};
|
|
14
|
+
module.exports.listStore = createListStore();
|
|
15
|
+
module.exports.pathStore = createListStore(notRemoved);
|
|
30
16
|
|
|
31
17
|
module.exports.mapStore = createStore({
|
|
32
18
|
get(map) {
|
|
@@ -81,3 +67,23 @@ function createStore({set, get}) {
|
|
|
81
67
|
};
|
|
82
68
|
}
|
|
83
69
|
|
|
70
|
+
function createListStore(returns = id) {
|
|
71
|
+
return (list = new Set()) => {
|
|
72
|
+
const fn = (...args) => {
|
|
73
|
+
if (!args.length)
|
|
74
|
+
return returns(toArray(list));
|
|
75
|
+
|
|
76
|
+
const [a] = args;
|
|
77
|
+
list.add(a);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
fn.clear = () => {
|
|
81
|
+
const a = list;
|
|
82
|
+
list = new Set();
|
|
83
|
+
|
|
84
|
+
return returns(toArray(a));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return fn;
|
|
88
|
+
};
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/engine-runner",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.0.1",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "run putout plugins",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-runner#readme",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
|
+
"commitType": "colon",
|
|
9
10
|
"release": false,
|
|
10
11
|
"tag": false,
|
|
11
12
|
"changelog": false,
|
|
@@ -44,10 +45,10 @@
|
|
|
44
45
|
"@babel/plugin-codemod-optional-catch-binding": "^7.7.4",
|
|
45
46
|
"c8": "^7.5.0",
|
|
46
47
|
"eslint": "^8.0.1",
|
|
47
|
-
"eslint-plugin-
|
|
48
|
-
"eslint-plugin-putout": "^
|
|
48
|
+
"eslint-plugin-n": "^15.2.4",
|
|
49
|
+
"eslint-plugin-putout": "^16.0.0",
|
|
49
50
|
"just-camel-case": "^4.0.2",
|
|
50
|
-
"lerna": "^
|
|
51
|
+
"lerna": "^5.0.0",
|
|
51
52
|
"madrun": "^9.0.0",
|
|
52
53
|
"mock-require": "^3.0.3",
|
|
53
54
|
"montag": "^1.0.0",
|