@putout/engine-runner 23.4.1 → 23.6.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.
@@ -84,10 +84,7 @@ module.exports = (pluginsToMerge, {fix, shebang, template}) => {
84
84
 
85
85
  const entries = Object.entries(pushed);
86
86
 
87
- const visitor = {
88
- //shouldSkip,
89
- ...merge(mergeItems),
90
- };
87
+ const visitor = merge(mergeItems);
91
88
 
92
89
  return {
93
90
  entries,
@@ -16,10 +16,13 @@ module.exports = (parentPath) => {
16
16
 
17
17
  return path.join('.');
18
18
  };
19
+
19
20
  function findKey(path, parent) {
20
21
  const {node} = path;
22
+ let key;
23
+ let value;
21
24
 
22
- for (const [key, value] of entries(parent)) {
25
+ for ([key, value] of entries(parent)) {
23
26
  if (isArray(value)) {
24
27
  const index = value.indexOf(node);
25
28
 
@@ -30,6 +33,8 @@ function findKey(path, parent) {
30
33
  }
31
34
 
32
35
  if (value === node)
33
- return key;
36
+ break;
34
37
  }
38
+
39
+ return key;
35
40
  }
@@ -39,27 +39,31 @@ const log = (from, path) => {
39
39
  };
40
40
 
41
41
  const {keys, entries} = Object;
42
-
43
42
  const {stringify} = JSON;
44
43
 
45
44
  const stub = () => [];
46
45
  const stubMatch = () => ({});
47
46
  const packKeys = (a) => () => keys(a);
48
- const isObj = (a) => typeof a === 'object';
47
+ const isObject = (a) => typeof a === 'object';
49
48
 
50
49
  module.exports = ({rule, plugin, msg, options}) => {
50
+ const maybeMatch = plugin.match || stubMatch;
51
+ const match = maybeMatch({
52
+ options,
53
+ });
54
+
51
55
  const {
52
56
  report,
53
57
  exclude = stub,
54
58
  replace,
55
- filter = getFilter(plugin.match, options),
59
+ filter = getFilter(match),
56
60
  } = plugin;
57
61
 
58
62
  const replaceItems = replace({
59
63
  options,
60
64
  });
61
65
 
62
- const fix = getFix(replaceItems);
66
+ const fix = getFix(replaceItems, match);
63
67
  const include = packKeys(replaceItems);
64
68
 
65
69
  return {
@@ -96,9 +100,6 @@ const fix = (from, to, path) => {
96
100
  if (mark.has())
97
101
  return;
98
102
 
99
- if (!compare(path, nodeFrom, {findUp: false}))
100
- return;
101
-
102
103
  const waysFrom = findVarsWays(nodeFrom);
103
104
  const {node} = path;
104
105
 
@@ -138,45 +139,55 @@ const fix = (from, to, path) => {
138
139
  log(from, newPath);
139
140
  };
140
141
 
141
- const getFix = (items) => (path) => {
142
- for (const [from, to] of entries(items))
143
- fix(from, to, path);
142
+ const getFix = (items, match) => (path) => {
143
+ for (const [from, to] of entries(items)) {
144
+ const nodeFrom = template.ast(from);
145
+
146
+ if (compare(path, nodeFrom, {findUp: false})) {
147
+ const matchFn = match[from];
148
+
149
+ if (!matchFn || runMatch(path, nodeFrom, matchFn))
150
+ fix(from, to, path);
151
+ }
152
+ }
144
153
  };
145
154
 
146
- const getFilter = (match = stubMatch, options) => (path) => {
147
- const all = entries(match({
148
- options,
149
- }));
155
+ const getFilter = (match) => (path) => {
156
+ const all = entries(match);
150
157
 
151
158
  for (const [from, matchProperty] of all) {
152
159
  const nodeFrom = template.ast(from);
153
160
 
154
- if (!compare(path.node, nodeFrom))
161
+ if (!compare(path.node, nodeFrom, {findUp: false}))
155
162
  continue;
156
163
 
157
- const waysFrom = findVarsWays(nodeFrom);
158
- const {node} = path;
159
-
160
- const values = getValues({
161
- waysFrom,
162
- node,
163
- });
164
-
165
- validateMatchProperty(matchProperty);
166
-
167
- return matchProperty(values, path);
164
+ return runMatch(path, nodeFrom, matchProperty);
168
165
  }
169
166
 
170
167
  return true;
171
168
  };
172
169
 
170
+ function runMatch(path, nodeFrom, matchProperty) {
171
+ const waysFrom = findVarsWays(nodeFrom);
172
+ const {node} = path;
173
+
174
+ const values = getValues({
175
+ waysFrom,
176
+ node,
177
+ });
178
+
179
+ validateMatchProperty(matchProperty);
180
+
181
+ return matchProperty(values, path);
182
+ }
183
+
173
184
  function parseTo(to, values, path) {
174
185
  const toStr = isFn(to) ? to(values, path) : to;
175
186
 
176
187
  if (!toStr)
177
188
  return null;
178
189
 
179
- if (isObj(toStr) && toStr.type) {
190
+ if (isObject(toStr) && toStr.type) {
180
191
  toStr.__putout_replace_cooked = true;
181
192
  return toStr;
182
193
  }
@@ -11,7 +11,7 @@ const hasWatermark = (watermark) => (path) => path.node?.[name]?.has(watermark);
11
11
 
12
12
  module.exports = (from, to, path) => {
13
13
  const {watermark, highWatermark} = create(from, to, path);
14
- const program = path.findParent(isProgram) || path;
14
+ const program = path.findParent(isProgram);
15
15
  const options = {
16
16
  watermark,
17
17
  highWatermark,
package/lib/run-fix.js CHANGED
@@ -14,7 +14,8 @@ const chooseFixArgs = ({path, pathOptions, options}) => {
14
14
  path,
15
15
  pathOptions, {
16
16
  options,
17
- }];
17
+ },
18
+ ];
18
19
 
19
20
  return [
20
21
  path, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-runner",
3
- "version": "23.4.1",
3
+ "version": "23.6.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Run 🐊Putout plugins",