@putout/engine-runner 19.0.1 β†’ 19.1.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
@@ -19,6 +19,7 @@ There is a couple plugin types available in 🐊**Putout**:
19
19
  - βœ… [**Replacer**](#replacer)
20
20
  - βœ… [**Includer**](#includer)
21
21
  - βœ… [**Traverser**](#traverser)
22
+ - βœ… [**Scanner**](#filer)
22
23
  - βœ… [**Finder**](#finder)
23
24
 
24
25
  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).
@@ -39,7 +40,7 @@ Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/
39
40
 
40
41
  ### Replacer
41
42
 
42
- **Replacer** converts code in declarative way. Simplest possible form, no need to use `fix`. Just `from` and `to` parts
43
+ **Replacer** converts code in declarative way. Simplest possible form of 🐊**Putout Plugin**, no need to use `fix`. Just `from` and `to` parts
43
44
  according to [`template variables syntax`](https://github.com/coderaiser/putout/tree/master/packages/compare#supported-template-variables).
44
45
 
45
46
  Simplest replace example:
@@ -202,6 +203,27 @@ module.exports.traverse = ({push}) => ({
202
203
  });
203
204
  ```
204
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) => {
218
+ const [filePath] = findFile(rootPath, 'hello.txt');
219
+
220
+ if (filePath)
221
+ return null;
222
+
223
+ return rootPath;
224
+ };
225
+ ```
226
+
205
227
  ### Finder
206
228
 
207
229
  **Finder** gives you all the control over traversing, but it's the slowest format.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const getPath = (item) => item.path || item;
3
+ const getPath = (item) => item.path || item[0] || item;
4
4
 
5
5
  module.exports.getPath = getPath;
6
6
  module.exports.getPosition = (path, shebang) => {
package/lib/index.js CHANGED
@@ -10,12 +10,14 @@ const superFind = require('./super-find');
10
10
  const include = require('./include');
11
11
  const replace = require('./replace');
12
12
  const declare = require('./declare');
13
+ const scanner = require('./scanner');
14
+ const template = require('./template');
13
15
 
14
16
  const {getPath, getPosition} = require('./get-position');
15
17
 
16
18
  const isRemoved = (a) => a?.removed;
17
19
 
18
- module.exports.runPlugins = ({ast, shebang, fix, fixCount, plugins, template = require('./template')}) => {
20
+ module.exports.runPlugins = ({ast, shebang, fix, fixCount, plugins}) => {
19
21
  let places = [];
20
22
 
21
23
  const merge = once(mergeVisitors);
@@ -171,6 +173,11 @@ function splitPlugins(plugins) {
171
173
  pluginsTraverse.push(include(item));
172
174
  continue;
173
175
  }
176
+
177
+ if (plugin.scan) {
178
+ pluginsTraverse.push(scanner(item));
179
+ continue;
180
+ }
174
181
  }
175
182
 
176
183
  return {
@@ -102,9 +102,9 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
102
102
  const uplist = upListStore();
103
103
  const paths = pathStore();
104
104
 
105
- const push = (path) => {
105
+ const push = (path, pathOptions) => {
106
106
  const position = getPosition(path, shebang);
107
- const message = msg || plugin.report(path);
107
+ const message = msg || plugin.report(path, pathOptions);
108
108
 
109
109
  placesStore({
110
110
  message,
@@ -113,6 +113,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
113
113
 
114
114
  runFix(fix, plugin.fix, {
115
115
  path,
116
+ pathOptions,
116
117
  rule,
117
118
  position,
118
119
  options,
@@ -27,6 +27,7 @@ const PRINT_OPTIONS = {
27
27
  newline: '',
28
28
  indent: '',
29
29
  splitter: ' ',
30
+ endOfFile: '',
30
31
  },
31
32
  }],
32
33
  };
@@ -148,9 +149,8 @@ const getFilter = (match = stubMatch, options) => (path) => {
148
149
  for (const [from, matchProperty] of all) {
149
150
  const nodeFrom = template.ast(from);
150
151
 
151
- if (!compare(path.node, nodeFrom)) {
152
+ if (!compare(path.node, nodeFrom))
152
153
  continue;
153
- }
154
154
 
155
155
  const waysFrom = findVarsWays(nodeFrom);
156
156
  const {node} = path;
@@ -9,9 +9,7 @@ const hasWatermark = (watermark) => (path) => path.node?.[name]?.has(watermark);
9
9
 
10
10
  module.exports = (from, to, path) => {
11
11
  const {watermark, highWatermark} = create(from, to, path);
12
-
13
12
  const program = path.findParent(isProgram) || path;
14
-
15
13
  const options = {
16
14
  watermark,
17
15
  highWatermark,
@@ -41,9 +39,8 @@ function create(from, to, path) {
41
39
 
42
40
  module.exports.init = init;
43
41
  function init({path, program}) {
44
- if (path.node) {
42
+ if (path.node)
45
43
  path.node[name] = path.node[name] || new Set();
46
- }
47
44
 
48
45
  program.node[name] = program.node[name] || new Set();
49
46
  }
package/lib/run-fix.js CHANGED
@@ -8,10 +8,25 @@ const {stringify} = JSON;
8
8
  const isFn = (a) => typeof a === 'function';
9
9
  const getPath = (path) => path.path || path;
10
10
 
11
- const tryToFix = (fix, {path, position, options}) => {
12
- const [e] = tryCatch(fix, path, {
11
+ const chooseFixArgs = ({path, pathOptions, options}) => {
12
+ if (pathOptions)
13
+ return [
14
+ path,
15
+ pathOptions, {
16
+ options,
17
+ }];
18
+
19
+ return [path, {
13
20
  options,
14
- });
21
+ }];
22
+ };
23
+
24
+ const tryToFix = (fix, {path, pathOptions, position, options}) => {
25
+ const [e] = tryCatch(fix, ...chooseFixArgs({
26
+ path,
27
+ pathOptions,
28
+ options,
29
+ }));
15
30
 
16
31
  const {scope} = path.path || path;
17
32
 
@@ -20,16 +35,15 @@ const tryToFix = (fix, {path, position, options}) => {
20
35
  .getProgramParent()
21
36
  .crawl();
22
37
 
23
- if (!e) {
38
+ if (!e)
24
39
  return;
25
- }
26
40
 
27
41
  e.loc = e.loc || position;
28
42
 
29
43
  throw e;
30
44
  };
31
45
 
32
- module.exports = (is, fix, {path, rule, position, options}) => {
46
+ module.exports = (is, fix, {path, pathOptions, rule, position, options}) => {
33
47
  if (!is)
34
48
  return;
35
49
 
@@ -40,6 +54,7 @@ module.exports = (is, fix, {path, rule, position, options}) => {
40
54
 
41
55
  tryToFix(fix, {
42
56
  path,
57
+ pathOptions,
43
58
  position,
44
59
  options,
45
60
  });
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const {__filesystem} = require('@putout/operator-json');
4
+ const log = require('debug')('putout:runner:scanner');
5
+
6
+ module.exports = ({rule, plugin, msg, options}) => {
7
+ const {
8
+ scan,
9
+ report,
10
+ fix,
11
+ } = plugin;
12
+
13
+ const traverse = getTraverse({
14
+ scan,
15
+ rule,
16
+ });
17
+
18
+ return {
19
+ rule,
20
+ msg,
21
+ options,
22
+ plugin: {
23
+ report,
24
+ fix,
25
+ traverse,
26
+ },
27
+ };
28
+ };
29
+
30
+ const getTraverse = ({scan, rule}) => ({push, options}) => ({
31
+ [__filesystem](path) {
32
+ log(rule);
33
+
34
+ const rootPath = path.get('arguments.0');
35
+ scan(rootPath, {
36
+ push,
37
+ options,
38
+ });
39
+ },
40
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-runner",
3
- "version": "19.0.1",
3
+ "version": "19.1.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Run 🐊Putout plugins",
@@ -29,6 +29,7 @@
29
29
  "@putout/engine-parser": "^9.0.0",
30
30
  "@putout/operate": "^11.0.0",
31
31
  "@putout/operator-declare": "^8.0.0",
32
+ "@putout/operator-json": "^1.3.0",
32
33
  "debug": "^4.1.1",
33
34
  "jessy": "^3.0.0",
34
35
  "nessy": "^4.0.0",
@@ -41,11 +42,12 @@
41
42
  "putout-engine"
42
43
  ],
43
44
  "devDependencies": {
45
+ "@putout/operator-filesystem": "^2.9.2",
44
46
  "@putout/plugin-minify": "*",
45
47
  "c8": "^8.0.0",
46
48
  "eslint": "^8.0.1",
47
49
  "eslint-plugin-n": "^16.0.0",
48
- "eslint-plugin-putout": "^19.0.0",
50
+ "eslint-plugin-putout": "^21.0.0",
49
51
  "just-camel-case": "^4.0.2",
50
52
  "lerna": "^6.0.1",
51
53
  "madrun": "^9.0.0",