@putout/engine-runner 12.4.1 → 12.5.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 +77 -57
- package/lib/index.js +3 -7
- package/lib/replace/index.js +13 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,6 +13,15 @@ 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
|
+
They goes from simplest to hardest. Let's start from `Replacer`.
|
|
24
|
+
|
|
16
25
|
### Replacer
|
|
17
26
|
|
|
18
27
|
`Replacer` converts code in declarative way. Simplest possible form, no need to use `fix`. Just `from` and `to` parts
|
|
@@ -171,14 +180,70 @@ module.exports.traverse = ({push}) => ({
|
|
|
171
180
|
});
|
|
172
181
|
```
|
|
173
182
|
|
|
174
|
-
|
|
183
|
+
### Finder
|
|
175
184
|
|
|
176
|
-
|
|
177
|
-
|
|
185
|
+
`Find plugins` gives you all the control over traversing, but it's the slowest format.
|
|
186
|
+
Because `traversers` not merged in contrast with other plugin formats.
|
|
178
187
|
|
|
179
|
-
|
|
188
|
+
```js
|
|
189
|
+
module.exports.report = () => 'debugger statement should not be used';
|
|
180
190
|
|
|
181
|
-
|
|
191
|
+
module.exports.fix = (path) => {
|
|
192
|
+
path.remove();
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
module.exports.find = (ast, {push, traverse}) => {
|
|
196
|
+
traverse(ast, {
|
|
197
|
+
'debugger'(path) {
|
|
198
|
+
push(path);
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Example
|
|
205
|
+
|
|
206
|
+
```js
|
|
207
|
+
const {runPlugins} = require('@putout/engine-runner');
|
|
208
|
+
const {parse} = require('@putout/engin-parser');
|
|
209
|
+
|
|
210
|
+
const plugins = [{
|
|
211
|
+
rule: "remove-debugger",
|
|
212
|
+
msg: "", // optional
|
|
213
|
+
options: {}, // optional
|
|
214
|
+
plugin: {
|
|
215
|
+
include: () => ['debugger'],
|
|
216
|
+
fix: (path) => path.remove(),
|
|
217
|
+
report: () => `debugger should not be used`,
|
|
218
|
+
},
|
|
219
|
+
}];
|
|
220
|
+
|
|
221
|
+
const ast = parse('const m = "hi"; debugger');
|
|
222
|
+
const places = runPlugins({
|
|
223
|
+
ast,
|
|
224
|
+
shebang: false, // default
|
|
225
|
+
fix: true, // default
|
|
226
|
+
fixCount: 1, // default
|
|
227
|
+
plugins,
|
|
228
|
+
parser: 'babel', // default
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Stores
|
|
233
|
+
|
|
234
|
+
Stores is preferred way of keeping 🐊`Putout` data, `traverse` init function called only once, and any other way
|
|
235
|
+
of handling variables will most likely will lead to bugs. There is 3 store types:
|
|
236
|
+
|
|
237
|
+
- ✅`listStore`;
|
|
238
|
+
- ✅`store`;
|
|
239
|
+
- ✅`upstore`;
|
|
240
|
+
|
|
241
|
+
Let's talk about each of them.
|
|
242
|
+
|
|
243
|
+
### `listStore`
|
|
244
|
+
|
|
245
|
+
To save things as a list use `listStore`.
|
|
246
|
+
Let's suppose you have such code:
|
|
182
247
|
|
|
183
248
|
```js
|
|
184
249
|
debugger;
|
|
@@ -187,6 +252,8 @@ debugger;
|
|
|
187
252
|
const world = '';
|
|
188
253
|
```
|
|
189
254
|
|
|
255
|
+
Let's process it!
|
|
256
|
+
|
|
190
257
|
```js
|
|
191
258
|
module.exports.traverse = ({listStore}) => ({
|
|
192
259
|
'debugger'(path) {
|
|
@@ -205,7 +272,9 @@ module.exports.traverse = ({listStore}) => ({
|
|
|
205
272
|
});
|
|
206
273
|
```
|
|
207
274
|
|
|
208
|
-
|
|
275
|
+
### `store`
|
|
276
|
+
|
|
277
|
+
When you need `key-value` use `store`:
|
|
209
278
|
|
|
210
279
|
```js
|
|
211
280
|
module.exports.traverse = ({push, store}) => ({
|
|
@@ -232,9 +301,9 @@ module.exports.traverse = ({push, store}) => ({
|
|
|
232
301
|
});
|
|
233
302
|
```
|
|
234
303
|
|
|
235
|
-
|
|
304
|
+
### `upstore`
|
|
236
305
|
|
|
237
|
-
When you need to update already saved values, use `upstore
|
|
306
|
+
When you need to update already saved values, use `upstore`:
|
|
238
307
|
|
|
239
308
|
```js
|
|
240
309
|
module.exports.traverse = ({push, store}) => ({
|
|
@@ -269,55 +338,6 @@ module.exports.traverse = ({push, store}) => ({
|
|
|
269
338
|
});
|
|
270
339
|
```
|
|
271
340
|
|
|
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
341
|
## Logs
|
|
322
342
|
|
|
323
343
|
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,
|
|
@@ -151,9 +146,19 @@ function parseTo(to, values, path) {
|
|
|
151
146
|
if (!toStr)
|
|
152
147
|
return null;
|
|
153
148
|
|
|
154
|
-
if (isObj(toStr))
|
|
149
|
+
if (isObj(toStr) && toStr.type)
|
|
155
150
|
return toStr;
|
|
156
151
|
|
|
152
|
+
if (!isString(toStr))
|
|
153
|
+
throw Error(`☝️ Looks like you passed 'replace' value with a wrong type. Allowed: 'string', 'node' and 'path'. Received: '${typeof toStr}' with value '${toStr}'.`);
|
|
154
|
+
|
|
157
155
|
return template.ast.fresh(toStr);
|
|
158
156
|
}
|
|
159
157
|
|
|
158
|
+
const validateTemplateValues = (a, b) => {
|
|
159
|
+
for (const key of keys(a)) {
|
|
160
|
+
if (!b[key])
|
|
161
|
+
throw Error(`☝️ Looks like template values not linked: ${stringify(keys(b))} -> ${stringify(keys(a))}`);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|