@putout/plugin-putout 29.36.1 → 29.38.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
@@ -1771,6 +1771,43 @@ export const scan = (root, {push, progress, crawlFile}) => {
1771
1771
 
1772
1772
  ### ❌ Example of incorrect code
1773
1773
 
1774
+ ```js
1775
+ export const traverse = ({push}) => ({
1776
+ AssignmentExpression(path) {
1777
+ const {left} = path.node;
1778
+
1779
+ if (!isIdentifier(left))
1780
+ return;
1781
+
1782
+ if (left.name === 'exports')
1783
+ return;
1784
+
1785
+ push(path);
1786
+ },
1787
+ });
1788
+ ```
1789
+
1790
+ ### ✅ Example of correct code
1791
+
1792
+ ```js
1793
+ export const include = () => [
1794
+ 'AssignmentExpression',
1795
+ ];
1796
+ export const filter = (path) => {
1797
+ const {left} = path.node;
1798
+
1799
+ if (!isIdentifier(left))
1800
+ return false;
1801
+
1802
+ if (left.name === 'exports')
1803
+ return false;
1804
+
1805
+ return true;
1806
+ };
1807
+ ```
1808
+
1809
+ ### ❌ Example of incorrect code
1810
+
1774
1811
  ```js
1775
1812
  module.exports.match = () => ({
1776
1813
  '__a = __b': (vars, path) => {},
@@ -20,31 +20,29 @@ export const fix = (path) => {
20
20
  path.node.name = newName;
21
21
  };
22
22
 
23
- export const traverse = ({push}) => ({
24
- ReferencedIdentifier(path) {
25
- const {name} = path.node;
26
- const [first, second] = name;
27
-
28
- const bindingPath = getBindingPath(path, name);
29
-
30
- if (!isVariableDeclarator(bindingPath))
31
- return;
32
-
33
- if (!isIdentifier(bindingPath.node.init, {name: 'types'}))
34
- return;
35
-
36
- if (!/[A-Z]/.test(first))
37
- return;
38
-
39
- if (!/[a-z]/.test(second) && !/^(TS|JSX)/.test(name))
40
- return;
41
-
42
- if (!types[name])
43
- return;
44
-
45
- push(path);
46
- },
47
- });
23
+ export const include = () => [
24
+ 'ReferencedIdentifier',
25
+ ];
26
+ export const filter = (path) => {
27
+ const {name} = path.node;
28
+ const [first, second] = name;
29
+
30
+ const bindingPath = getBindingPath(path, name);
31
+
32
+ if (!isVariableDeclarator(bindingPath))
33
+ return false;
34
+
35
+ if (!isIdentifier(bindingPath.node.init, {name: 'types'}))
36
+ return false;
37
+
38
+ if (!/[A-Z]/.test(first))
39
+ return false;
40
+
41
+ if (!/[a-z]/.test(second) && !/^(TS|JSX)/.test(name))
42
+ return false;
43
+
44
+ return types[name];
45
+ };
48
46
 
49
47
  const getNewName = (name) => {
50
48
  if (name.startsWith('TS')) {
@@ -15,6 +15,7 @@ const {
15
15
  __json,
16
16
  __yaml,
17
17
  __toml,
18
+ compareAny,
18
19
  } = operator;
19
20
 
20
21
  const name = '__putout_plugin_check_replace_code';
@@ -174,11 +175,16 @@ function parseKey(propertyPath) {
174
175
  return [null, key];
175
176
  }
176
177
 
178
+ const MATCHERS = [
179
+ 'module.exports.match = __a',
180
+ 'export const match = __a',
181
+ ];
182
+
177
183
  function hasMatch(path) {
178
184
  const {body} = path.scope.getProgramParent().path.node;
179
185
 
180
186
  for (const current of body) {
181
- if (compare(current, 'module.exports.match = __a'))
187
+ if (compareAny(current, MATCHERS))
182
188
  return true;
183
189
  }
184
190
 
@@ -18,18 +18,15 @@ const isBabelTypes = (path) => path.get('arguments.0').isStringLiteral({
18
18
  value: '@babel/types',
19
19
  });
20
20
 
21
- export const traverse = ({push}) => ({
22
- CallExpression(path) {
23
- if (!isRequire(path))
24
- return;
25
-
26
- if (!isBabelTypes(path))
27
- return;
28
-
29
- push(path);
30
- },
31
- });
32
-
21
+ export const include = () => [
22
+ 'CallExpression',
23
+ ];
24
+ export const filter = (path) => {
25
+ if (!isRequire(path))
26
+ return false;
27
+
28
+ return isBabelTypes(path);
29
+ };
33
30
  export const fix = (path) => {
34
31
  replaceWith(path, astRequire);
35
32
  };
@@ -4,52 +4,147 @@ import {
4
4
  operator,
5
5
  } from 'putout';
6
6
 
7
- const {stringLiteral} = types;
8
- const {compare, remove} = operator;
7
+ const {
8
+ isIdentifier,
9
+ stringLiteral,
10
+ booleanLiteral,
11
+ returnStatement,
12
+ } = types;
9
13
 
10
- const isPush = (path) => path.get('value').isIdentifier({
14
+ const {
15
+ compare,
16
+ remove,
17
+ insertAfter,
18
+ createTypeChecker,
19
+ } = operator;
20
+
21
+ const isPush = ({node}) => isIdentifier(node.value, {
11
22
  name: 'push',
12
23
  });
13
24
 
25
+ const isPushCall = (node) => compare(node, 'push(path)');
26
+ const isLastNotPush = ({node}) => !isPushCall(node.body.body.at(-1));
27
+
28
+ const isBlock = createTypeChecker([
29
+ ['-: node.body -> !BlockStatement'],
30
+ ['-: node.body.body.length', '!=', 1],
31
+ ['+: node.body.body.0', isPushCall],
32
+ ]);
33
+
34
+ const isSingleMethod = createTypeChecker([
35
+ ['-: -> !ObjectExpression'],
36
+ ['-: node.properties.length', '!=', 1],
37
+ ['+: node.properties.0 -> ObjectMethod'],
38
+ ]);
39
+
40
+ const isGuarded = createTypeChecker([
41
+ ['-: node.body.body.length', '<', 2],
42
+ ['-', isLastNotPush],
43
+ ['-', hasReturnValue],
44
+ ['-', hasSeveralPushes],
45
+ ['+: -> +'],
46
+ ]);
47
+
48
+ const isInclude = (path) => isPush(path) || isBlock(path);
49
+
14
50
  export const report = () => 'Includer should be used instead of Traverser';
15
51
 
16
52
  export const match = () => ({
17
53
  'module.exports.traverse = __a': (vars, path) => {
18
- const __aPath = path.get('right.body');
54
+ const objectPath = path.get('right.body');
19
55
 
20
- if (__aPath.isBlockStatement())
56
+ if (!objectPath.isObjectExpression())
21
57
  return false;
22
58
 
23
- const properties = __aPath.get('properties');
59
+ return objectPath.get('properties').every(isInclude);
60
+ },
61
+ 'export const traverse = ({push}) => __a': (vars, path) => {
62
+ const objectPath = getObjectPath(path);
24
63
 
25
- for (const propertyPath of properties) {
26
- if (!isPush(propertyPath) && !isBlock(propertyPath))
27
- return false;
28
- }
64
+ if (!isSingleMethod(objectPath))
65
+ return false;
29
66
 
30
- return true;
67
+ return isGuarded(objectPath.get('properties.0'));
31
68
  },
32
69
  });
33
70
 
34
71
  export const replace = () => ({
35
72
  'module.exports.traverse = __a': (vars, path) => {
36
73
  const node = template.ast.fresh('module.exports.include = () => []');
37
- const __aPath = path.get('right.body');
38
- const properties = __aPath.get('properties');
74
+ const properties = path.get('right.body.properties');
39
75
 
40
- for (const propertyPath of properties) {
41
- const name = getName(propertyPath);
42
-
43
- if (isPush(propertyPath) || isBlock(propertyPath)) {
44
- node.right.body.elements.push(stringLiteral(name));
45
- remove(propertyPath);
46
- }
76
+ for (const propertyPath of properties.filter(isInclude)) {
77
+ node.right.body.elements.push(stringLiteral(getName(propertyPath)));
78
+ remove(propertyPath);
47
79
  }
48
80
 
49
81
  return node;
50
82
  },
83
+ 'export const traverse = ({push}) => __a': (vars, path) => {
84
+ const methodPath = getObjectPath(path).get('properties.0');
85
+ const bodyPath = methodPath.get('body');
86
+
87
+ bodyPath.traverse({
88
+ Function: skip,
89
+ ReturnStatement: returnFalse,
90
+ });
91
+
92
+ bodyPath.node.body.splice(-1, 1, returnStatement(booleanLiteral(true)));
93
+
94
+ const include = template.ast.fresh('export const include = () => []');
95
+ const filter = template.ast.fresh('export const filter = (path) => {}');
96
+ const [filterDeclarator] = filter.declaration.declarations;
97
+
98
+ include.declaration.declarations[0].init.body.elements.push(stringLiteral(getName(methodPath)));
99
+
100
+ filterDeclarator.init.params = methodPath.node.params;
101
+ filterDeclarator.init.body = bodyPath.node;
102
+
103
+ insertAfter(path, filter);
104
+
105
+ return include;
106
+ },
51
107
  });
52
108
 
109
+ function getObjectPath(path) {
110
+ return path.get('declaration.declarations.0.init.body');
111
+ }
112
+
113
+ function hasReturnValue(path) {
114
+ let result = false;
115
+
116
+ path.traverse({
117
+ Function: skip,
118
+ ReturnStatement({node}) {
119
+ if (node.argument)
120
+ result = true;
121
+ },
122
+ });
123
+
124
+ return result;
125
+ }
126
+
127
+ function hasSeveralPushes(path) {
128
+ let count = 0;
129
+
130
+ path.traverse({
131
+ Identifier({node}) {
132
+ if (node.name === 'push')
133
+ ++count;
134
+ },
135
+ });
136
+
137
+ return count > 1;
138
+ }
139
+
140
+ function skip(path) {
141
+ path.skip();
142
+ }
143
+
144
+ function returnFalse(path) {
145
+ path.node.argument = booleanLiteral(false);
146
+ }
147
+
53
148
  function getName(propertyPath) {
54
149
  const keyPath = propertyPath.get('key');
55
150
  const {node} = keyPath;
@@ -59,14 +154,3 @@ function getName(propertyPath) {
59
154
 
60
155
  return node.value;
61
156
  }
62
-
63
- function isBlock(path) {
64
- const bodyPath = path.get('body');
65
-
66
- if (!bodyPath.isBlockStatement() || bodyPath.node.body.length !== 1)
67
- return false;
68
-
69
- const [node] = bodyPath.node.body;
70
-
71
- return compare(node, 'push(path)');
72
- }
@@ -13,23 +13,23 @@ export const fix = (path) => {
13
13
  remove(path);
14
14
  };
15
15
 
16
- export const traverse = ({push}) => ({
17
- ObjectProperty(path) {
18
- const {value} = path.node;
19
-
20
- if (value.value !== 'putout')
21
- return;
22
-
23
- const {parentPath} = path.parentPath;
24
-
25
- if (!isCallExpression(parentPath))
26
- return;
27
-
28
- const name = 'createTest';
29
-
30
- if (!isIdentifier(parentPath.node.callee, {name}))
31
- return;
32
-
33
- push(path);
34
- },
35
- });
16
+ export const include = () => [
17
+ 'ObjectProperty',
18
+ ];
19
+ export const filter = (path) => {
20
+ const {value} = path.node;
21
+
22
+ if (value.value !== 'putout')
23
+ return false;
24
+
25
+ const {parentPath} = path.parentPath;
26
+
27
+ if (!isCallExpression(parentPath))
28
+ return false;
29
+
30
+ const name = 'createTest';
31
+
32
+ return isIdentifier(parentPath.node.callee, {
33
+ name,
34
+ });
35
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "29.36.1",
3
+ "version": "29.38.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin helps with plugins development",