@putout/engine-runner 13.0.1 β 13.2.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 +47 -9
- package/lib/include.js +6 -2
- package/lib/merge-visitors.js +6 -0
- package/lib/run-fix.js +8 -0
- package/lib/store.js +20 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-runner.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/engine-runner"npm"
|
|
5
5
|
|
|
6
|
-
Run π[
|
|
6
|
+
Run π[**Putout**](https://github.com/coderaiser/putout) plugins.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
@@ -13,20 +13,20 @@ npm i @putout/engine-runner
|
|
|
13
13
|
|
|
14
14
|
## Supported Plugin Types
|
|
15
15
|
|
|
16
|
-
There is a couple plugin types supported by
|
|
16
|
+
There is a couple plugin types supported by π**Putout**:
|
|
17
17
|
|
|
18
18
|
- β
[`Replacer`](#replacer)
|
|
19
19
|
- β
[`Includer`](#includer)
|
|
20
20
|
- β
[`Traverser`](#traverser)
|
|
21
21
|
- β
[`Finder`](#finder)
|
|
22
22
|
|
|
23
|
-
All of them supports subset of
|
|
23
|
+
All of them supports subset of **JavaScript** π¦[**PutoutScript**](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript) described in [`@putout/compare`](https://github.com/coderaiser/putout/tree/master/packages/compare#readme).
|
|
24
24
|
|
|
25
|
-
They goes from simplest to hardest. Let's start from
|
|
25
|
+
They goes from simplest to hardest. Let's start from **Replacer**.
|
|
26
26
|
|
|
27
27
|
### Replacer
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
**Replacer** converts code in declarative way. Simplest possible form, no need to use `fix`. Just `from` and `to` parts
|
|
30
30
|
according to [`template variables syntax`](https://github.com/coderaiser/putout/tree/master/packages/compare#supported-template-variables).
|
|
31
31
|
|
|
32
32
|
Simplest replace example:
|
|
@@ -63,7 +63,7 @@ Simplest remove example:
|
|
|
63
63
|
module.exports.report = () => 'debugger should not be used';
|
|
64
64
|
|
|
65
65
|
module.exports.replace = () => ({
|
|
66
|
-
|
|
66
|
+
debugger: '',
|
|
67
67
|
});
|
|
68
68
|
```
|
|
69
69
|
|
|
@@ -210,8 +210,8 @@ const {runPlugins} = require('@putout/engine-runner');
|
|
|
210
210
|
const {parse} = require('@putout/engin-parser');
|
|
211
211
|
|
|
212
212
|
const plugins = [{
|
|
213
|
-
rule:
|
|
214
|
-
msg:
|
|
213
|
+
rule: 'remove-debugger',
|
|
214
|
+
msg: '', // optional
|
|
215
215
|
options: {}, // optional
|
|
216
216
|
plugin: {
|
|
217
217
|
include: () => ['debugger'],
|
|
@@ -233,12 +233,13 @@ const places = runPlugins({
|
|
|
233
233
|
|
|
234
234
|
## Stores
|
|
235
235
|
|
|
236
|
-
Stores is preferred way of keeping
|
|
236
|
+
Stores is preferred way of keeping π**Putout** data, `traverse` init function called only once, and any other way
|
|
237
237
|
of handling variables will most likely will lead to bugs. There is 3 store types:
|
|
238
238
|
|
|
239
239
|
- β
`listStore`;
|
|
240
240
|
- β
`store`;
|
|
241
241
|
- β
`upstore`;
|
|
242
|
+
- β
`uplist`;
|
|
242
243
|
|
|
243
244
|
Let's talk about each of them.
|
|
244
245
|
|
|
@@ -340,6 +341,43 @@ module.exports.traverse = ({push, store}) => ({
|
|
|
340
341
|
});
|
|
341
342
|
```
|
|
342
343
|
|
|
344
|
+
### `uplist`
|
|
345
|
+
|
|
346
|
+
When you need to update named arrays:
|
|
347
|
+
|
|
348
|
+
```js
|
|
349
|
+
module.exports.traverse = ({uplist, push}) => ({
|
|
350
|
+
'const __object = __a.__b': (fullPath) => {
|
|
351
|
+
const {__a, __b} = getTemplateValues(fullPath, 'const __object = __a.__b');
|
|
352
|
+
const path = fullPath.get('declarations.0.init');
|
|
353
|
+
const {uid} = path.scope;
|
|
354
|
+
|
|
355
|
+
if (isIdentifier(__a) || isCallExpression(__a)) {
|
|
356
|
+
const {code} = generate(__a);
|
|
357
|
+
const id = `${uid}-${code}`;
|
|
358
|
+
|
|
359
|
+
return uplist(id, path);
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
'Program': {
|
|
363
|
+
exit: () => {
|
|
364
|
+
for (const items of uplist()) {
|
|
365
|
+
if (items.length < 2)
|
|
366
|
+
continue;
|
|
367
|
+
|
|
368
|
+
const index = items.length - 1;
|
|
369
|
+
const path = items[index];
|
|
370
|
+
|
|
371
|
+
push({
|
|
372
|
+
path,
|
|
373
|
+
items,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
```
|
|
380
|
+
|
|
343
381
|
## Logs
|
|
344
382
|
|
|
345
383
|
To see logs, use:
|
package/lib/include.js
CHANGED
|
@@ -17,8 +17,8 @@ module.exports = ({rule, plugin, msg, options}) => {
|
|
|
17
17
|
filter = good,
|
|
18
18
|
} = plugin;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
validate('include', include);
|
|
21
|
+
validate('report', report);
|
|
22
22
|
|
|
23
23
|
const traverse = getTraverse(include(), filter, rule);
|
|
24
24
|
|
|
@@ -64,3 +64,7 @@ const getTraverse = (include, filter, rule) => ({push, options}) => {
|
|
|
64
64
|
return result;
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
function validate(name, fn) {
|
|
68
|
+
if (!isFn(fn))
|
|
69
|
+
throw Error(`βοΈ Looks like '${name}' is not a 'function' but '${typeof fn}' with value: '${stringify(fn)}'. More on using Includer: https://git.io/JqcMn`);
|
|
70
|
+
}
|
package/lib/merge-visitors.js
CHANGED
|
@@ -10,6 +10,7 @@ const {
|
|
|
10
10
|
listStore,
|
|
11
11
|
mapStore,
|
|
12
12
|
upStore,
|
|
13
|
+
upListStore,
|
|
13
14
|
} = require('./store');
|
|
14
15
|
|
|
15
16
|
const shouldSkip = (a) => !a.parent;
|
|
@@ -39,6 +40,7 @@ module.exports = (pluginsToMerge, {fix, shebang, template}) => {
|
|
|
39
40
|
store,
|
|
40
41
|
upstore,
|
|
41
42
|
listStore,
|
|
43
|
+
uplist,
|
|
42
44
|
} = getStore(plugin, {
|
|
43
45
|
fix,
|
|
44
46
|
rule,
|
|
@@ -54,6 +56,7 @@ module.exports = (pluginsToMerge, {fix, shebang, template}) => {
|
|
|
54
56
|
store,
|
|
55
57
|
listStore,
|
|
56
58
|
upstore,
|
|
59
|
+
uplist,
|
|
57
60
|
generate,
|
|
58
61
|
options,
|
|
59
62
|
});
|
|
@@ -90,6 +93,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
|
|
|
90
93
|
const list = listStore();
|
|
91
94
|
const upstore = upStore();
|
|
92
95
|
const placesStore = listStore();
|
|
96
|
+
const uplist = upListStore();
|
|
93
97
|
|
|
94
98
|
const push = (path) => {
|
|
95
99
|
const position = getPosition(path, shebang);
|
|
@@ -112,6 +116,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
|
|
|
112
116
|
store.clear();
|
|
113
117
|
list.clear();
|
|
114
118
|
upstore.clear();
|
|
119
|
+
uplist.clear();
|
|
115
120
|
return placesStore.clear();
|
|
116
121
|
};
|
|
117
122
|
|
|
@@ -121,6 +126,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
|
|
|
121
126
|
store,
|
|
122
127
|
listStore: list,
|
|
123
128
|
upstore,
|
|
129
|
+
uplist,
|
|
124
130
|
};
|
|
125
131
|
}
|
|
126
132
|
|
package/lib/run-fix.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
const tryCatch = require('try-catch');
|
|
4
4
|
const debug = require('debug')('putout:runner:fix');
|
|
5
5
|
const {enabled} = debug;
|
|
6
|
+
const {stringify} = JSON;
|
|
7
|
+
const isFn = (a) => typeof a === 'function';
|
|
6
8
|
|
|
7
9
|
const tryToFix = (fix, {path, position, options}) => {
|
|
8
10
|
const [e] = tryCatch(fix, path, {options});
|
|
@@ -25,6 +27,7 @@ module.exports = (is, fix, {path, rule, position, options}) => {
|
|
|
25
27
|
return;
|
|
26
28
|
|
|
27
29
|
enabled && debug(`fix: ${rule}`, position, path.toString());
|
|
30
|
+
validate('fix', fix);
|
|
28
31
|
|
|
29
32
|
tryToFix(fix, {
|
|
30
33
|
path,
|
|
@@ -33,3 +36,8 @@ module.exports = (is, fix, {path, rule, position, options}) => {
|
|
|
33
36
|
});
|
|
34
37
|
};
|
|
35
38
|
|
|
39
|
+
function validate(name, fn) {
|
|
40
|
+
if (!isFn(fn))
|
|
41
|
+
throw Error(`βοΈ Looks like '${name}' is not a 'function' but '${typeof fn}' with value: '${stringify(fn)}'. More on writing πPutout Plugins: https://git.io/JqcMn`);
|
|
42
|
+
}
|
|
43
|
+
|
package/lib/store.js
CHANGED
|
@@ -6,6 +6,8 @@ const {
|
|
|
6
6
|
assign,
|
|
7
7
|
} = Object;
|
|
8
8
|
|
|
9
|
+
const toArray = (a) => Array.from(a);
|
|
10
|
+
|
|
9
11
|
module.exports.listStore = (list = []) => {
|
|
10
12
|
const fn = (...args) => {
|
|
11
13
|
if (!args.length)
|
|
@@ -27,23 +29,39 @@ module.exports.listStore = (list = []) => {
|
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
module.exports.mapStore = createStore({
|
|
32
|
+
get(map) {
|
|
33
|
+
return values(map);
|
|
34
|
+
},
|
|
30
35
|
set(map, name, data) {
|
|
31
36
|
map[name] = data;
|
|
32
37
|
},
|
|
33
38
|
});
|
|
34
39
|
|
|
35
40
|
module.exports.upStore = createStore({
|
|
41
|
+
get(map) {
|
|
42
|
+
return values(map);
|
|
43
|
+
},
|
|
36
44
|
set(map, name, data) {
|
|
37
45
|
map[name] = map[name] || {};
|
|
38
46
|
assign(map[name], data);
|
|
39
47
|
},
|
|
40
48
|
});
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
module.exports.upListStore = createStore({
|
|
51
|
+
get(map) {
|
|
52
|
+
return values(map).map(toArray);
|
|
53
|
+
},
|
|
54
|
+
set(map, name, data) {
|
|
55
|
+
map[name] = map[name] || new Set();
|
|
56
|
+
map[name].add(data);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function createStore({set, get}) {
|
|
43
61
|
return (map = {}) => {
|
|
44
62
|
const fn = (...args) => {
|
|
45
63
|
if (!args.length)
|
|
46
|
-
return
|
|
64
|
+
return get(map);
|
|
47
65
|
|
|
48
66
|
const [name, data] = args;
|
|
49
67
|
|