@putout/plugin-putout 12.8.0 → 13.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,8 +19,9 @@ npm i @putout/plugin-putout -D
19
19
  "putout/apply-create-test": "on",
20
20
  "putout/apply-processors-destructuring": "on",
21
21
  "putout/apply-async-formatter": "on",
22
- "putout/apply-remove": "on",
23
22
  "putout/apply-declare": "on",
23
+ "putout/apply-remove": "on",
24
+ "putout/apply-insert-after": "on",
24
25
  "putout/add-args": "on",
25
26
  "putout/add-push": "on",
26
27
  "putout/check-match": "on",
@@ -96,6 +97,31 @@ export const fix = (path) => {
96
97
  };
97
98
  ```
98
99
 
100
+ ## apply-insert-after
101
+
102
+ Better to use [`insertAfter(a, b)`](https://github.com/coderaiser/putout/tree/master/packages/operate#insert-after) method of `operator`.
103
+ It helps to avoid duplication of comments.
104
+
105
+ ### ❌ Example of incorrect code
106
+
107
+ ```js
108
+ export const fix = (path) => {
109
+ path.insertAfter(path.get('init'));
110
+ };
111
+ ```
112
+
113
+ ### ✅ Example of correct code
114
+
115
+ ```js
116
+ import {operator} from 'putout';
117
+
118
+ const {insertAfter} = operator;
119
+
120
+ export const fix = (path) => {
121
+ insertAfter(path, path.get('init'));
122
+ };
123
+ ```
124
+
99
125
  ## apply-declare
100
126
 
101
127
  Better to use [`Declareator`](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#declarator) instead of `operator.declare()`.
@@ -218,7 +244,7 @@ isNumericLiteral(node);
218
244
 
219
245
  ## convert-putout-test-to-create-test
220
246
 
221
- Fixes results of [@putout/convert-commonjs-to-esm](https://github.com/coderaiser/putout/tree/master/packages/plugin-convert-commonjs-to-esm) work.
247
+ Fixes results of [@putout/convert-commonjs-to-esm](https://github.com/coderaiser/putout/tree/master/packages/plugin-convert-commonjs-to-esm#readme) work.
222
248
 
223
249
  ### ❌ Example of incorrect code
224
250
 
@@ -541,10 +567,7 @@ isIdentifier(a);
541
567
  ### ✅ Example of correct code
542
568
 
543
569
  ```js
544
- const {
545
- operator,
546
- types,
547
- } = require('putout');
570
+ const {operator, types} = require('putout');
548
571
 
549
572
  const {compare} = operator;
550
573
  const {isIdentifier} = types;
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
5
  const {traverse} = operator;
9
6
 
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- operator,
5
- types,
6
- } = require('putout');
3
+ const {operator, types} = require('putout');
7
4
 
8
5
  const computed = true;
9
6
  const shorthand = true;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Use 'insertAfter(a, b)' instead of 'a.insertAfter(b)'`;
4
+
5
+ module.exports.replace = () => ({
6
+ '__a.insertAfter(__b)': 'insertAfter(__a, __b)',
7
+ });
@@ -3,5 +3,5 @@
3
3
  module.exports.report = () => `Use 'remove(path)' instead of 'path.remove()'`;
4
4
 
5
5
  module.exports.replace = () => ({
6
- 'path.remove()': 'remove(path)',
6
+ '__a.remove()': 'remove(__a)',
7
7
  });
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- operator,
5
- types,
6
- } = require('putout');
3
+ const {operator, types} = require('putout');
7
4
 
8
5
  const {
9
6
  getTemplateValues,
@@ -4,11 +4,7 @@ const putout = require('putout');
4
4
  const tryCatch = require('try-catch');
5
5
  const noop = () => {};
6
6
 
7
- const {
8
- types,
9
- operator,
10
- } = putout;
11
-
7
+ const {types, operator} = putout;
12
8
  const {replaceWith} = operator;
13
9
 
14
10
  const {
@@ -22,6 +18,7 @@ module.exports = (rootPath, key) => {
22
18
  const getVar = createVarStore(rootPath);
23
19
 
24
20
  const [transformError, result] = tryCatch(putout, key, {
21
+ printer: 'putout',
25
22
  fix: true,
26
23
  isTS: true,
27
24
  plugins: [
@@ -34,10 +31,7 @@ module.exports = (rootPath, key) => {
34
31
  fix: (path) => {
35
32
  const {node} = path;
36
33
 
37
- const {
38
- value,
39
- name,
40
- } = node;
34
+ const {value, name} = node;
41
35
 
42
36
  if (path.isStringLiteral() && /^__[a-z]$/.test(value)) {
43
37
  path.node.value = getVar(name);
@@ -75,6 +75,7 @@ module.exports.traverse = ({push}) => ({
75
75
  }
76
76
 
77
77
  const [transformError, result] = tryCatch(putout, keyCode, {
78
+ printer: 'putout',
78
79
  fix: true,
79
80
  isTS: true,
80
81
  plugins: [
@@ -97,7 +98,7 @@ module.exports.traverse = ({push}) => ({
97
98
  return;
98
99
  }
99
100
 
100
- const {code} = result;
101
+ const code = result.code.slice(0, -1);
101
102
  const [error, is] = tryCatch(compare, rmSemi(code), template);
102
103
 
103
104
  if (error || !is)
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- operator,
5
- template,
6
- } = require('putout');
3
+ const {operator, template} = require('putout');
7
4
 
8
5
  const {replaceWith} = operator;
9
6
 
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
5
  const {replaceWith} = operator;
9
6
 
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
5
  const {replaceWith} = operator;
9
6
  const {ObjectProperty} = types;
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
5
  const {
9
6
  compare,
@@ -8,15 +8,12 @@ const {
8
8
 
9
9
  const fullstore = require('fullstore');
10
10
 
11
- const {
12
- Identifier,
13
- ObjectProperty,
14
- } = types;
11
+ const {Identifier, ObjectProperty} = types;
12
+ const {replaceWith, insertAfter} = operator;
15
13
 
16
- const {
17
- replaceWith,
18
- insertAfter,
19
- } = operator;
14
+ const isRecast = (program) => program.get('body.0.expression').isStringLiteral({
15
+ value: 'use strict',
16
+ });
20
17
 
21
18
  module.exports.report = () => {
22
19
  return `"operator.replaceWith" should be called instead of "path.replaceWith"`;
@@ -24,9 +21,7 @@ module.exports.report = () => {
24
21
 
25
22
  module.exports.fix = ({path, calleePath, property, object, program, isInserted}) => {
26
23
  replaceWith(calleePath, property);
27
-
28
- const strictModePath = program.get('body.0');
29
- const {bindings} = strictModePath.scope;
24
+ const {bindings} = program.scope;
30
25
 
31
26
  path.node.arguments.unshift(object);
32
27
 
@@ -39,10 +34,17 @@ module.exports.fix = ({path, calleePath, property, object, program, isInserted})
39
34
  `);
40
35
 
41
36
  const {types} = bindings;
42
- const pathToInsertAfter = types ? types.path.parentPath : strictModePath;
37
+ const first = program.get('body.0');
38
+ const pathToInsert = types ? types.path.parentPath : first;
39
+
40
+ if (isRecast(program))
41
+ insertAfter(pathToInsert, replaceWithAST);
42
+ else if (types)
43
+ insertAfter(pathToInsert, replaceWithAST);
44
+ else
45
+ pathToInsert.insertBefore(replaceWithAST);
43
46
 
44
47
  isInserted(true);
45
- insertAfter(pathToInsertAfter, replaceWithAST);
46
48
 
47
49
  return;
48
50
  }
@@ -75,15 +77,12 @@ module.exports.traverse = ({push}) => {
75
77
  if (!calleePath.isMemberExpression())
76
78
  return;
77
79
 
78
- const {
79
- object,
80
- property,
81
- } = calleePath.node;
80
+ const {object, property} = calleePath.node;
82
81
 
83
82
  if (property.name !== 'replaceWith')
84
83
  return;
85
84
 
86
- const program = path.findParent((path) => path.isProgram());
85
+ const program = path.scope.getProgramParent().path;
87
86
 
88
87
  push({
89
88
  isInserted,
@@ -6,15 +6,13 @@ const {
6
6
  types,
7
7
  } = require('putout');
8
8
 
9
- const {
10
- insertAfter,
11
- replaceWith,
12
- } = operator;
9
+ const {insertAfter, replaceWith} = operator;
10
+ const {Identifier, ObjectProperty} = types;
13
11
 
14
- const {
15
- Identifier,
16
- ObjectProperty,
17
- } = types;
12
+ const isRecast = (program) => program.get('body.0').get('expression')
13
+ .isStringLiteral({
14
+ value: 'use strict',
15
+ });
18
16
 
19
17
  module.exports.report = () => {
20
18
  return `"operate.replaceWithMultiple" should be called instead of "path.replaceWithMultiple"`;
@@ -25,8 +23,8 @@ const replaceWithAST = template.ast(`
25
23
  `);
26
24
 
27
25
  module.exports.fix = ({path, calleePath, property, object, program}) => {
28
- const strictModePath = program.get('body.0');
29
- const {bindings} = strictModePath.scope;
26
+ const first = program.get('body.0');
27
+ const {bindings} = program.scope;
30
28
 
31
29
  replaceWith(calleePath, property);
32
30
  path.node.arguments.unshift(object);
@@ -34,8 +32,12 @@ module.exports.fix = ({path, calleePath, property, object, program}) => {
34
32
  if (bindings.replaceWithMultiple)
35
33
  return;
36
34
 
37
- if (!bindings.replaceWith && !bindings.insertAfter)
38
- return insertAfter(strictModePath, replaceWithAST);
35
+ if (!bindings.replaceWith && !bindings.insertAfter) {
36
+ if (isRecast(program))
37
+ return insertAfter(first, replaceWithAST);
38
+
39
+ return first.insertBefore(replaceWithAST);
40
+ }
39
41
 
40
42
  const id = Identifier('replaceWithMultiple');
41
43
  const varPath = getVarPath(bindings);
@@ -44,10 +46,7 @@ module.exports.fix = ({path, calleePath, property, object, program}) => {
44
46
  };
45
47
 
46
48
  function getVarPath(bindings) {
47
- const {
48
- replaceWith,
49
- insertAfter,
50
- } = bindings;
49
+ const {replaceWith, insertAfter} = bindings;
51
50
 
52
51
  if (replaceWith)
53
52
  return replaceWith.path;
@@ -62,15 +61,12 @@ module.exports.traverse = ({push}) => ({
62
61
  if (!calleePath.isMemberExpression())
63
62
  return;
64
63
 
65
- const {
66
- object,
67
- property,
68
- } = calleePath.node;
64
+ const {object, property} = calleePath.node;
69
65
 
70
66
  if (property.name !== 'replaceWithMultiple')
71
67
  return;
72
68
 
73
- const program = path.findParent((path) => path.isProgram());
69
+ const program = path.scope.getProgramParent().path;
74
70
 
75
71
  push({
76
72
  path,
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- isIdentifier,
5
- Identifier,
6
- } = require('putout').types;
3
+ const {isIdentifier, Identifier} = require('putout').types;
7
4
 
8
5
  module.exports.report = () => {
9
6
  return `"noTransformCode" should be called instead of using same arguments twice in "transformCode"`;
@@ -16,10 +13,7 @@ module.exports.traverse = ({push}) => ({
16
13
  if (!calleePath.isMemberExpression())
17
14
  return;
18
15
 
19
- const {
20
- object,
21
- property,
22
- } = calleePath.node;
16
+ const {object, property} = calleePath.node;
23
17
 
24
18
  if (object.name !== 't' || property.name !== 'transformCode')
25
19
  return;
@@ -7,7 +7,7 @@ const {
7
7
  } = require('putout');
8
8
 
9
9
  const {StringLiteral} = types;
10
- const {compare} = operator;
10
+ const {compare, remove} = operator;
11
11
 
12
12
  const isPush = (path) => path.get('value').isIdentifier({
13
13
  name: 'push',
@@ -42,7 +42,7 @@ module.exports.replace = () => ({
42
42
 
43
43
  if (isPush(propertyPath) || isBlock(propertyPath)) {
44
44
  node.right.body.elements.push(StringLiteral(name));
45
- propertyPath.remove();
45
+ remove(propertyPath);
46
46
  }
47
47
  }
48
48
 
@@ -1,10 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {operator} = require('putout');
4
- const {
5
- contains,
6
- traverse,
7
- } = operator;
4
+ const {contains, traverse} = operator;
8
5
 
9
6
  module.exports.report = () => 'Replacer should be used instead of Traverser (https://git.io/JqcMn)';
10
7
 
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- operator,
5
- types,
6
- } = require('putout');
3
+ const {operator, types} = require('putout');
7
4
 
8
5
  const {
9
6
  StringLiteral,
@@ -14,10 +11,7 @@ const {
14
11
  isIdentifier,
15
12
  } = types;
16
13
 
17
- const {
18
- replaceWith,
19
- getProperty,
20
- } = operator;
14
+ const {replaceWith, getProperty} = operator;
21
15
 
22
16
  module.exports.report = () => `Apply modifications to 'createTest()' options`;
23
17
 
@@ -63,10 +57,7 @@ const maybeLiteral = (a) => {
63
57
  };
64
58
 
65
59
  function convert(objectPath) {
66
- const {
67
- key,
68
- value,
69
- } = objectPath.node.properties[0];
60
+ const {key, value} = objectPath.node.properties[0];
70
61
 
71
62
  replaceWith(objectPath, ObjectExpression([
72
63
  ObjectProperty(Identifier('plugins'), ArrayExpression([
@@ -16,6 +16,7 @@ module.exports = {
16
16
  isSimpleRegExp: `const {isSimpleRegExp} = operator`,
17
17
  getTemplateValues: `const {getTemplateValues} = operator`,
18
18
  addArgs: `const {addArgs} = operator`,
19
+ insertAfter: 'const {insertAfter} = operator',
19
20
  replaceWith: `const {replaceWith} = operator`,
20
21
  replaceWithMultiple: `const {replaceWithMultiple} = operator`,
21
22
  remove: 'const {remove} = operator',
package/lib/index.js CHANGED
@@ -9,6 +9,7 @@ module.exports.rules = {
9
9
  ...getRule('apply-async-formatter'),
10
10
  ...getRule('apply-create-test'),
11
11
  ...getRule('apply-remove'),
12
+ ...getRule('apply-insert-after'),
12
13
  ...getRule('apply-declare'),
13
14
  ...getRule('check-replace-code'),
14
15
  ...getRule('check-match'),
@@ -2,10 +2,7 @@
2
2
 
3
3
  const justCamelCase = require('just-camel-case');
4
4
 
5
- const {
6
- types,
7
- template,
8
- } = require('putout');
5
+ const {types, template} = require('putout');
9
6
 
10
7
  const TEST = `
11
8
  const test = require('@putout/test')(__dirname, {
@@ -5,9 +5,7 @@ const {rename} = operator;
5
5
 
6
6
  module.exports.report = () => '"operator" should be used instead of "operate"';
7
7
 
8
- module.exports.include = () => [
9
- 'Program',
10
- ];
8
+ module.exports.include = () => ['Program'];
11
9
 
12
10
  module.exports.filter = (path) => {
13
11
  const noOperator = !path.scope.bindings.operator;
@@ -59,16 +59,12 @@ function isCorrect({path, incorrect}) {
59
59
  const calleePath = path.findParent(isCallExpression);
60
60
 
61
61
  if (!calleePath)
62
- return [
63
- CORRECT,
64
- ];
62
+ return [CORRECT];
65
63
 
66
64
  const messagePath = calleePath.get('arguments.0');
67
65
 
68
66
  if (!messagePath.isStringLiteral())
69
- return [
70
- CORRECT,
71
- ];
67
+ return [CORRECT];
72
68
 
73
69
  const {value} = messagePath.node;
74
70
  const is = !incorrect.test(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "12.8.0",
3
+ "version": "13.1.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin helps with plugins development",
@@ -36,18 +36,18 @@
36
36
  "putout"
37
37
  ],
38
38
  "devDependencies": {
39
- "@putout/test": "^6.0.0",
40
- "c8": "^7.5.0",
39
+ "@putout/test": "^7.0.0",
40
+ "c8": "^8.0.0",
41
41
  "eslint": "^8.0.1",
42
42
  "eslint-plugin-n": "^16.0.0",
43
- "eslint-plugin-putout": "^17.0.0",
43
+ "eslint-plugin-putout": "^18.0.0",
44
44
  "lerna": "^6.0.1",
45
45
  "madrun": "^9.0.0",
46
46
  "montag": "^1.2.1",
47
- "nodemon": "^2.0.1"
47
+ "nodemon": "^3.0.1"
48
48
  },
49
49
  "peerDependencies": {
50
- "putout": ">=29"
50
+ "putout": ">=30"
51
51
  },
52
52
  "license": "MIT",
53
53
  "engines": {