@putout/engine-runner 21.0.3 → 21.2.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 CHANGED
@@ -203,123 +203,7 @@ module.exports.traverse = ({push}) => ({
203
203
  });
204
204
  ```
205
205
 
206
- ### Scanner
207
-
208
- **Scanner** gives you all ability to work with files.
209
-
210
- ```js
211
- module.exports.report = () => 'Create file hello.txt';
212
-
213
- module.exports.fix = (rootPath) => {
214
- createFile(rootPath, 'hello.txt', 'hello world');
215
- };
216
-
217
- module.exports.scan = (rootPath, {push}) => {
218
- const [filePath] = findFile(rootPath, 'hello.txt');
219
-
220
- if (filePath)
221
- return null;
222
-
223
- push(rootPath);
224
- };
225
- ```
226
-
227
- You can also subscribe to `progress` events:
228
-
229
- - `start`;
230
- - `end`;
231
- - `push`;
232
-
233
- And one more: `file` if your plugin uses `progress` function:
234
-
235
- ```js
236
- export const report = () => 'Create file hello.txt';
237
-
238
- export const fix = (rootPath) => {
239
- createFile(rootPath, 'hello.txt', 'hello world');
240
- };
241
-
242
- export const scan = (rootPath, {progress}) => {
243
- const [filePath] = findFile(rootPath, 'hello.txt');
244
-
245
- if (filePath)
246
- return null;
247
-
248
- progress({
249
- i: 0,
250
- n: 1,
251
- });
252
-
253
- push(filePath);
254
- };
255
- ```
256
-
257
- Or better `trackFile`, which does the same, but also count `progress`:
258
-
259
- ```js
260
- export const report = () => 'Add file';
261
- export const fix = (file) => {
262
- writeFileContent(file, 'hello');
263
- };
264
-
265
- export const scan = (rootPath, {push, trackFile}) => {
266
- for (const file of trackFile(rootPath, 'hello.txt')) {
267
- push(file);
268
- }
269
- };
270
- ```
271
-
272
- ### Finder
273
-
274
- **Finder** 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/engine-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
-
312
- const places = runPlugins({
313
- ast,
314
- shebang: false, // default
315
- fix: false, // default
316
- fixCount: 0, // default
317
- plugins,
318
- parser: 'babel', // default
319
- });
320
- ```
321
-
322
- ## Stores
206
+ #### Stores
323
207
 
324
208
  Stores is preferred way of keeping 🐊**Putout** data, `traverse` init function called only once, and any other way
325
209
  of handling variables will most likely will lead to bugs. There is a couple store types:
@@ -332,7 +216,7 @@ of handling variables will most likely will lead to bugs. There is a couple stor
332
216
 
333
217
  Let's talk about each of them.
334
218
 
335
- ### `listStore`
219
+ ##### `listStore`
336
220
 
337
221
  To save things as a list without duplicates use `listStore`.
338
222
  Let's suppose you have such code:
@@ -369,7 +253,7 @@ module.exports.traverse = ({listStore}) => ({
369
253
  });
370
254
  ```
371
255
 
372
- ### `pathStore`
256
+ ##### `pathStore`
373
257
 
374
258
  When you want additional check that `path` not removed.
375
259
 
@@ -397,7 +281,7 @@ module.exports.traverse = ({pathStore}) => ({
397
281
  });
398
282
  ```
399
283
 
400
- ### `store`
284
+ ##### `store`
401
285
 
402
286
  When you need `key-value` use `store`:
403
287
 
@@ -426,7 +310,7 @@ module.exports.traverse = ({push, store}) => ({
426
310
  });
427
311
  ```
428
312
 
429
- ### `upstore`
313
+ ##### `upstore`
430
314
 
431
315
  When you need to update already saved values, use `upstore`:
432
316
 
@@ -463,7 +347,7 @@ module.exports.traverse = ({push, upstore}) => ({
463
347
  });
464
348
  ```
465
349
 
466
- ### `uplist`
350
+ ##### `uplist`
467
351
 
468
352
  When you need to update named arrays:
469
353
 
@@ -500,6 +384,122 @@ module.exports.traverse = ({uplist, push}) => ({
500
384
  });
501
385
  ```
502
386
 
387
+ ### Scanner
388
+
389
+ **Scanner** gives you all ability to work with files.
390
+
391
+ ```js
392
+ module.exports.report = () => 'Create file hello.txt';
393
+
394
+ module.exports.fix = (rootPath) => {
395
+ createFile(rootPath, 'hello.txt', 'hello world');
396
+ };
397
+
398
+ module.exports.scan = (rootPath, {push}) => {
399
+ const [filePath] = findFile(rootPath, 'hello.txt');
400
+
401
+ if (filePath)
402
+ return null;
403
+
404
+ push(rootPath);
405
+ };
406
+ ```
407
+
408
+ You can also subscribe to `progress` events:
409
+
410
+ - `start`;
411
+ - `end`;
412
+ - `push`;
413
+
414
+ And one more: `file` if your plugin uses `progress` function:
415
+
416
+ ```js
417
+ export const report = () => 'Create file hello.txt';
418
+
419
+ export const fix = (rootPath) => {
420
+ createFile(rootPath, 'hello.txt', 'hello world');
421
+ };
422
+
423
+ export const scan = (rootPath, {progress}) => {
424
+ const [filePath] = findFile(rootPath, 'hello.txt');
425
+
426
+ if (filePath)
427
+ return null;
428
+
429
+ progress({
430
+ i: 0,
431
+ n: 1,
432
+ });
433
+
434
+ push(filePath);
435
+ };
436
+ ```
437
+
438
+ Or better `trackFile`, which does the same, but also count `progress`:
439
+
440
+ ```js
441
+ export const report = () => 'Add file';
442
+ export const fix = (file) => {
443
+ writeFileContent(file, 'hello');
444
+ };
445
+
446
+ export const scan = (rootPath, {push, trackFile}) => {
447
+ for (const file of trackFile(rootPath, 'hello.txt')) {
448
+ push(file);
449
+ }
450
+ };
451
+ ```
452
+
453
+ ### Finder
454
+
455
+ **Finder** gives you all the control over traversing, but it's the slowest format.
456
+ Because `traversers` not merged in contrast with other plugin formats.
457
+
458
+ ```js
459
+ module.exports.report = () => 'debugger statement should not be used';
460
+
461
+ module.exports.fix = (path) => {
462
+ path.remove();
463
+ };
464
+
465
+ module.exports.find = (ast, {push, traverse}) => {
466
+ traverse(ast, {
467
+ 'debugger'(path) {
468
+ push(path);
469
+ },
470
+ });
471
+ };
472
+ ```
473
+
474
+ ## Example
475
+
476
+ ```js
477
+ const {runPlugins} = require('@putout/engine-runner');
478
+ const {parse} = require('@putout/engine-parser');
479
+
480
+ const plugins = [{
481
+ rule: 'remove-debugger',
482
+ msg: '', // optional
483
+ options: {}, // optional
484
+ plugin: {
485
+ include: () => ['debugger'],
486
+ fix: (path) => path.remove(),
487
+ report: () => `debugger should not be used`,
488
+ },
489
+ }];
490
+
491
+ const ast = parse('const m = "hi"; debugger');
492
+
493
+ const places = runPlugins({
494
+ ast,
495
+ shebang: false, // default
496
+ fix: false, // default
497
+ fixCount: 0, // default
498
+ plugins,
499
+ parser: 'babel', // default
500
+ });
501
+ ```
502
+
503
503
  ## Logs
504
504
 
505
505
  To see logs, use:
package/lib/include.js CHANGED
@@ -2,11 +2,10 @@
2
2
 
3
3
  const log = require('debug')('putout:runner:include');
4
4
  const maybeArray = require('./maybe-array');
5
+ const {validate} = require('./validate');
5
6
 
6
- const {stringify} = JSON;
7
7
  const stub = () => [];
8
8
  const good = () => true;
9
- const isFn = (a) => typeof a === 'function';
10
9
 
11
10
  module.exports = ({rule, plugin, msg, options}) => {
12
11
  const {
@@ -64,8 +63,3 @@ const getTraverse = (include, filter, rule) => ({push, options}) => {
64
63
 
65
64
  return result;
66
65
  };
67
-
68
- function validate(name, fn) {
69
- if (!isFn(fn))
70
- throw Error(`☝️ Looks like '${name}' is not a 'function' but '${typeof fn}' with value: '${stringify(fn)}'. More on using Includer: https://git.io/JqcMn`);
71
- }
@@ -6,6 +6,7 @@ const {generate} = require('@putout/engine-parser');
6
6
  const runFix = require('./run-fix');
7
7
  const {getPosition} = require('./get-position');
8
8
  const maybeArray = require('./maybe-array');
9
+ const {validate} = require('./validate');
9
10
 
10
11
  const {
11
12
  listStore,
@@ -23,8 +24,10 @@ const {assign} = Object;
23
24
  const parse = (name, plugin, options) => {
24
25
  const list = [];
25
26
 
26
- if (plugin[name])
27
+ if (plugin[name]) {
28
+ validate(name, plugin[name]);
27
29
  list.push(...maybeArray(plugin[name]()));
30
+ }
28
31
 
29
32
  if (options[name])
30
33
  list.push(...maybeArray(options[name]));
package/lib/progress.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {EventEmitter} = require('events');
3
+ const {EventEmitter} = require('node:events');
4
4
 
5
5
  module.exports.createProgress = () => {
6
6
  let pluginsCount = 0;
@@ -119,13 +119,15 @@ const fix = (from, to, path) => {
119
119
  const waysTo = findVarsWays(nodeTo);
120
120
  const newPath = replaceWith(path, nodeTo);
121
121
 
122
- validateTemplateValues(waysTo, waysFrom);
123
-
124
- setValues({
125
- waysTo,
126
- values,
127
- path: newPath,
128
- });
122
+ if (!nodeTo.__putout_replace_cooked) {
123
+ validateTemplateValues(waysTo, waysFrom);
124
+
125
+ setValues({
126
+ waysTo,
127
+ values,
128
+ path: newPath,
129
+ });
130
+ }
129
131
 
130
132
  mark.add();
131
133
  path
@@ -174,8 +176,10 @@ function parseTo(to, values, path) {
174
176
  if (!toStr)
175
177
  return null;
176
178
 
177
- if (isObj(toStr) && toStr.type)
179
+ if (isObj(toStr) && toStr.type) {
180
+ toStr.__putout_replace_cooked = true;
178
181
  return toStr;
182
+ }
179
183
 
180
184
  if (!isString(toStr))
181
185
  throw Error(`☝️ Looks like you passed 'replace' value with a wrong type. Allowed: 'string', 'node' and 'path'. Received: '${typeof toStr}' with value '${toStr}'.`);
package/lib/run-fix.js CHANGED
@@ -16,9 +16,11 @@ const chooseFixArgs = ({path, pathOptions, options}) => {
16
16
  options,
17
17
  }];
18
18
 
19
- return [path, {
20
- options,
21
- }];
19
+ return [
20
+ path, {
21
+ options,
22
+ },
23
+ ];
22
24
  };
23
25
 
24
26
  const tryToFix = (fix, {path, pathOptions, position, options}) => {
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const isFn = (a) => typeof a === 'function';
4
+ const {stringify} = JSON;
5
+
6
+ module.exports.validate = (name, fn) => {
7
+ if (!isFn(fn))
8
+ throw Error(`☝️ Looks like '${name}' is not a 'function' but '${typeof fn}' with value: '${stringify(fn)}'. More on using Includer: https://git.io/JqcMn`);
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-runner",
3
- "version": "21.0.3",
3
+ "version": "21.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Run 🐊Putout plugins",
@@ -50,11 +50,11 @@
50
50
  ],
51
51
  "devDependencies": {
52
52
  "@putout/plugin-minify": "*",
53
- "c8": "^9.0.0",
53
+ "c8": "^10.0.0",
54
54
  "eslint": "^9.0.0",
55
55
  "eslint-plugin-n": "^17.0.0",
56
- "eslint-plugin-putout": "^22.0.0",
57
- "just-camel-case": "^4.0.2",
56
+ "eslint-plugin-putout": "^23.0.0",
57
+ "just-camel-case": "^6.2.0",
58
58
  "lerna": "^6.0.1",
59
59
  "madrun": "^10.0.0",
60
60
  "mock-require": "^3.0.3",