@putout/engine-runner 12.4.1 → 13.0.0
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 +80 -58
- package/lib/index.js +3 -7
- package/lib/replace/index.js +22 -10
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -13,6 +13,17 @@ npm i @putout/engine-runner
|
|
|
13
13
|
|
|
14
14
|
## Supported Plugin Types
|
|
15
15
|
|
|
16
|
+
There is a couple plugin types supported by 🐊`Putout`:
|
|
17
|
+
|
|
18
|
+
- ✅[`Replacer`](#replacer)
|
|
19
|
+
- ✅[`Includer`](#includer)
|
|
20
|
+
- ✅[`Traverser`](#traverser)
|
|
21
|
+
- ✅[`Finder`](#finder)
|
|
22
|
+
|
|
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
|
+
|
|
25
|
+
They goes from simplest to hardest. Let's start from `Replacer`.
|
|
26
|
+
|
|
16
27
|
### Replacer
|
|
17
28
|
|
|
18
29
|
`Replacer` converts code in declarative way. Simplest possible form, no need to use `fix`. Just `from` and `to` parts
|
|
@@ -151,7 +162,7 @@ module.exports.filter = (path) => {
|
|
|
151
162
|
const __ = 'hello';
|
|
152
163
|
```
|
|
153
164
|
|
|
154
|
-
Where `__` can be any node. All this possible with help of [@putout/compare](https://github.com/coderaiser/putout/tree/master/packages/compare). Templates format supported in `traverse` and `find` plugins as well.
|
|
165
|
+
Where `__` can be any node. All this possible with help of [@putout/compare](https://github.com/coderaiser/putout/tree/master/packages/compare#readme). Templates format supported in `traverse` and `find` plugins as well.
|
|
155
166
|
|
|
156
167
|
### Traverser
|
|
157
168
|
|
|
@@ -171,14 +182,70 @@ module.exports.traverse = ({push}) => ({
|
|
|
171
182
|
});
|
|
172
183
|
```
|
|
173
184
|
|
|
174
|
-
|
|
185
|
+
### Finder
|
|
186
|
+
|
|
187
|
+
`Find plugins` gives you all the control over traversing, but it's the slowest format.
|
|
188
|
+
Because `traversers` not merged in contrast with other plugin formats.
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
module.exports.report = () => 'debugger statement should not be used';
|
|
192
|
+
|
|
193
|
+
module.exports.fix = (path) => {
|
|
194
|
+
path.remove();
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
module.exports.find = (ast, {push, traverse}) => {
|
|
198
|
+
traverse(ast, {
|
|
199
|
+
'debugger'(path) {
|
|
200
|
+
push(path);
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Example
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
const {runPlugins} = require('@putout/engine-runner');
|
|
210
|
+
const {parse} = require('@putout/engin-parser');
|
|
211
|
+
|
|
212
|
+
const plugins = [{
|
|
213
|
+
rule: "remove-debugger",
|
|
214
|
+
msg: "", // optional
|
|
215
|
+
options: {}, // optional
|
|
216
|
+
plugin: {
|
|
217
|
+
include: () => ['debugger'],
|
|
218
|
+
fix: (path) => path.remove(),
|
|
219
|
+
report: () => `debugger should not be used`,
|
|
220
|
+
},
|
|
221
|
+
}];
|
|
222
|
+
|
|
223
|
+
const ast = parse('const m = "hi"; debugger');
|
|
224
|
+
const places = runPlugins({
|
|
225
|
+
ast,
|
|
226
|
+
shebang: false, // default
|
|
227
|
+
fix: true, // default
|
|
228
|
+
fixCount: 1, // default
|
|
229
|
+
plugins,
|
|
230
|
+
parser: 'babel', // default
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Stores
|
|
175
235
|
|
|
176
|
-
|
|
177
|
-
of handling variables will most likely will lead to bugs.
|
|
236
|
+
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 3 store types:
|
|
178
238
|
|
|
179
|
-
|
|
239
|
+
- ✅`listStore`;
|
|
240
|
+
- ✅`store`;
|
|
241
|
+
- ✅`upstore`;
|
|
180
242
|
|
|
181
|
-
|
|
243
|
+
Let's talk about each of them.
|
|
244
|
+
|
|
245
|
+
### `listStore`
|
|
246
|
+
|
|
247
|
+
To save things as a list use `listStore`.
|
|
248
|
+
Let's suppose you have such code:
|
|
182
249
|
|
|
183
250
|
```js
|
|
184
251
|
debugger;
|
|
@@ -187,6 +254,8 @@ debugger;
|
|
|
187
254
|
const world = '';
|
|
188
255
|
```
|
|
189
256
|
|
|
257
|
+
Let's process it!
|
|
258
|
+
|
|
190
259
|
```js
|
|
191
260
|
module.exports.traverse = ({listStore}) => ({
|
|
192
261
|
'debugger'(path) {
|
|
@@ -205,7 +274,9 @@ module.exports.traverse = ({listStore}) => ({
|
|
|
205
274
|
});
|
|
206
275
|
```
|
|
207
276
|
|
|
208
|
-
|
|
277
|
+
### `store`
|
|
278
|
+
|
|
279
|
+
When you need `key-value` use `store`:
|
|
209
280
|
|
|
210
281
|
```js
|
|
211
282
|
module.exports.traverse = ({push, store}) => ({
|
|
@@ -232,9 +303,9 @@ module.exports.traverse = ({push, store}) => ({
|
|
|
232
303
|
});
|
|
233
304
|
```
|
|
234
305
|
|
|
235
|
-
|
|
306
|
+
### `upstore`
|
|
236
307
|
|
|
237
|
-
When you need to update already saved values, use `upstore
|
|
308
|
+
When you need to update already saved values, use `upstore`:
|
|
238
309
|
|
|
239
310
|
```js
|
|
240
311
|
module.exports.traverse = ({push, store}) => ({
|
|
@@ -269,55 +340,6 @@ module.exports.traverse = ({push, store}) => ({
|
|
|
269
340
|
});
|
|
270
341
|
```
|
|
271
342
|
|
|
272
|
-
### Finder
|
|
273
|
-
|
|
274
|
-
`Find plugins` gives you all the control over traversing, but it's the slowest format.
|
|
275
|
-
Because `traversers` not merged in contrast with other plugin formats.
|
|
276
|
-
|
|
277
|
-
```js
|
|
278
|
-
module.exports.report = () => 'debugger statement should not be used';
|
|
279
|
-
|
|
280
|
-
module.exports.fix = (path) => {
|
|
281
|
-
path.remove();
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
module.exports.find = (ast, {push, traverse}) => {
|
|
285
|
-
traverse(ast, {
|
|
286
|
-
'debugger'(path) {
|
|
287
|
-
push(path);
|
|
288
|
-
},
|
|
289
|
-
});
|
|
290
|
-
};
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
## Example
|
|
294
|
-
|
|
295
|
-
```js
|
|
296
|
-
const {runPlugins} = require('@putout/engine-runner');
|
|
297
|
-
const {parse} = require('@putout/engin-parser');
|
|
298
|
-
|
|
299
|
-
const plugins = [{
|
|
300
|
-
rule: "remove-debugger",
|
|
301
|
-
msg: "", // optional
|
|
302
|
-
options: {}, // optional
|
|
303
|
-
plugin: {
|
|
304
|
-
include: () => ['debugger'],
|
|
305
|
-
fix: (path) => path.remove(),
|
|
306
|
-
report: () => `debugger should not be used`,
|
|
307
|
-
},
|
|
308
|
-
}];
|
|
309
|
-
|
|
310
|
-
const ast = parse('const m = "hi"; debugger');
|
|
311
|
-
const places = runPlugins({
|
|
312
|
-
ast,
|
|
313
|
-
shebang: false, // default
|
|
314
|
-
fix: true, // default
|
|
315
|
-
fixCount: 1, // default
|
|
316
|
-
plugins,
|
|
317
|
-
parser: 'babel', // default
|
|
318
|
-
});
|
|
319
|
-
```
|
|
320
|
-
|
|
321
343
|
## Logs
|
|
322
344
|
|
|
323
345
|
To see logs, use:
|
package/lib/index.js
CHANGED
|
@@ -24,9 +24,7 @@ module.exports.runPlugins = ({ast, shebang, fix, fixCount, plugins, template = r
|
|
|
24
24
|
const {
|
|
25
25
|
pluginsFind,
|
|
26
26
|
pluginsTraverse,
|
|
27
|
-
} = splitPlugins(plugins
|
|
28
|
-
template,
|
|
29
|
-
});
|
|
27
|
+
} = splitPlugins(plugins);
|
|
30
28
|
|
|
31
29
|
for (let i = 0; i < fixCount; i++) {
|
|
32
30
|
places = run({
|
|
@@ -130,7 +128,7 @@ function runWithoutMerge({ast, fix, shebang, template, pluginsFind}) {
|
|
|
130
128
|
return places;
|
|
131
129
|
}
|
|
132
130
|
|
|
133
|
-
function splitPlugins(plugins
|
|
131
|
+
function splitPlugins(plugins) {
|
|
134
132
|
const pluginsFind = [];
|
|
135
133
|
const pluginsTraverse = [];
|
|
136
134
|
|
|
@@ -148,9 +146,7 @@ function splitPlugins(plugins, {template}) {
|
|
|
148
146
|
}
|
|
149
147
|
|
|
150
148
|
if (plugin.replace) {
|
|
151
|
-
pluginsTraverse.push(include(replace(item
|
|
152
|
-
template,
|
|
153
|
-
})));
|
|
149
|
+
pluginsTraverse.push(include(replace(item)));
|
|
154
150
|
continue;
|
|
155
151
|
}
|
|
156
152
|
|
package/lib/replace/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const isString = (a) => typeof a === 'string';
|
|
4
|
+
|
|
3
5
|
const {template} = require('@putout/engine-parser');
|
|
4
6
|
const {
|
|
5
7
|
remove,
|
|
@@ -28,13 +30,6 @@ const stubMatch = () => ({});
|
|
|
28
30
|
const packKeys = (a) => () => keys(a);
|
|
29
31
|
const isObj = (a) => typeof a === 'object';
|
|
30
32
|
|
|
31
|
-
const validateTemplateValues = (a, b) => {
|
|
32
|
-
for (const key of keys(a)) {
|
|
33
|
-
if (!b[key])
|
|
34
|
-
throw Error(`☝️ Looks like template values not linked: ${stringify(keys(b))} -> ${stringify(keys(a))}`);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
33
|
module.exports = ({rule, plugin, msg, options}) => {
|
|
39
34
|
const {
|
|
40
35
|
report,
|
|
@@ -124,7 +119,7 @@ const getFix = (items) => (path) => {
|
|
|
124
119
|
const getFilter = (match = stubMatch, options) => (path) => {
|
|
125
120
|
const all = entries(match({options}));
|
|
126
121
|
|
|
127
|
-
for (const [from,
|
|
122
|
+
for (const [from, matchProperty] of all) {
|
|
128
123
|
const nodeFrom = template.ast(from);
|
|
129
124
|
|
|
130
125
|
if (!compare(path.node, nodeFrom)) {
|
|
@@ -139,7 +134,9 @@ const getFilter = (match = stubMatch, options) => (path) => {
|
|
|
139
134
|
node,
|
|
140
135
|
});
|
|
141
136
|
|
|
142
|
-
|
|
137
|
+
validateMatchProperty(matchProperty);
|
|
138
|
+
|
|
139
|
+
return matchProperty(values, path);
|
|
143
140
|
}
|
|
144
141
|
|
|
145
142
|
return true;
|
|
@@ -151,9 +148,24 @@ function parseTo(to, values, path) {
|
|
|
151
148
|
if (!toStr)
|
|
152
149
|
return null;
|
|
153
150
|
|
|
154
|
-
if (isObj(toStr))
|
|
151
|
+
if (isObj(toStr) && toStr.type)
|
|
155
152
|
return toStr;
|
|
156
153
|
|
|
154
|
+
if (!isString(toStr))
|
|
155
|
+
throw Error(`☝️ Looks like you passed 'replace' value with a wrong type. Allowed: 'string', 'node' and 'path'. Received: '${typeof toStr}' with value '${toStr}'.`);
|
|
156
|
+
|
|
157
157
|
return template.ast.fresh(toStr);
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
function validateMatchProperty(match) {
|
|
161
|
+
if (!isFn(match))
|
|
162
|
+
throw Error(`☝️ Looks like 'match' property value is not a 'function', but '${typeof match}' with value '${match}'.`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const validateTemplateValues = (a, b) => {
|
|
166
|
+
for (const key of keys(a)) {
|
|
167
|
+
if (!b[key])
|
|
168
|
+
throw Error(`☝️ Looks like template values not linked: ${stringify(keys(b))} -> ${stringify(keys(a))}`);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/engine-runner",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "run putout plugins",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@babel/traverse": "^7.12.7",
|
|
28
28
|
"@babel/types": "^7.12.7",
|
|
29
29
|
"@putout/compare": "^8.0.0",
|
|
30
|
-
"@putout/engine-parser": "^
|
|
30
|
+
"@putout/engine-parser": "^5.0.0",
|
|
31
31
|
"@putout/operate": "^7.0.0",
|
|
32
32
|
"debug": "^4.1.1",
|
|
33
33
|
"jessy": "^3.0.0",
|
|
@@ -46,19 +46,19 @@
|
|
|
46
46
|
"c8": "^7.5.0",
|
|
47
47
|
"eslint": "^8.0.1",
|
|
48
48
|
"eslint-plugin-node": "^11.0.0",
|
|
49
|
-
"eslint-plugin-putout": "^
|
|
49
|
+
"eslint-plugin-putout": "^14.0.0",
|
|
50
50
|
"just-camel-case": "^4.0.2",
|
|
51
51
|
"lerna": "^4.0.0",
|
|
52
|
-
"madrun": "^
|
|
52
|
+
"madrun": "^9.0.0",
|
|
53
53
|
"mock-require": "^3.0.3",
|
|
54
54
|
"montag": "^1.0.0",
|
|
55
55
|
"nodemon": "^2.0.1",
|
|
56
56
|
"putout": "*",
|
|
57
|
-
"supertape": "^
|
|
57
|
+
"supertape": "^7.0.0"
|
|
58
58
|
},
|
|
59
59
|
"license": "MIT",
|
|
60
60
|
"engines": {
|
|
61
|
-
"node": ">=
|
|
61
|
+
"node": ">=16"
|
|
62
62
|
},
|
|
63
63
|
"publishConfig": {
|
|
64
64
|
"access": "public"
|