@putout/plugin-putout 12.7.0 → 13.0.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
@@ -23,6 +23,8 @@ npm i @putout/plugin-putout -D
23
23
  "putout/apply-declare": "on",
24
24
  "putout/add-args": "on",
25
25
  "putout/add-push": "on",
26
+ "putout/check-match": "on",
27
+ "putout/check-replace-code": "on",
26
28
  "putout/convert-putout-test-to-create-test": "on",
27
29
  "putout/convert-to-no-transform-code": "on",
28
30
  "putout/convert-number-to-numeric": "on",
@@ -43,7 +45,6 @@ npm i @putout/plugin-putout -D
43
45
  "putout/convert-report-to-function": "on",
44
46
  "putout/create-test": "on",
45
47
  "putout/shorten-imports": "on",
46
- "putout/check-replace-code": "on",
47
48
  "putout/declare": "on",
48
49
  "putout/includer": "on",
49
50
  "putout/move-require-on-top-level": "on",
@@ -502,8 +503,28 @@ module.exports.replace = () => ({
502
503
  });
503
504
  ```
504
505
 
505
- There is no `fix` for this rule, it used internally to be more confident about `test coverage`, because of declaration form, transforms cannon be checked by `nyc` and `c8`, and uncovered lines can find unfixable false positives when running on code.
506
- This is additional tests, if you forget to test some case (from a big list of rules that is supported) it will be checked with this `rule` and make transforms more stable.
506
+ ☝️ *There is no `fix` for this rule, it used internally to be more confident about `test coverage`, because of declaration form, transforms cannon be checked by `nyc` and `c8`, and uncovered lines can find unfixable false positives when running on code.
507
+ This is additional tests, if you forget to test some case (from a big list of rules that is supported) it will be checked with this `rule` and make transforms more stable.*
508
+
509
+ ## check-match
510
+
511
+ Checks that [Replacer](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#replacer) `match()` keys exists in `replace`.
512
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c245bee6b129614b2f68c2e89fc3b101/d9f935ccd7f41ff166111cd62277f3244b1d526e).
513
+
514
+ ### ❌ Example of incorrect code
515
+
516
+ ```js
517
+ module.exports.match = () => ({
518
+ '__a = __b': (vars, path) => {},
519
+ });
520
+
521
+ module.exports.replace = () => ({
522
+ '__a = __': '__a',
523
+ });
524
+ ```
525
+
526
+ ☝️ *There is no `fix` for this rule, it used internally to be more confident about `test coverage`, because of declaration form, transforms cannon be checked by `nyc` and `c8`, and uncovered lines can find unfixable false positives when running on code.
527
+ This is additional tests, if you forget to test some case (from a big list of rules that is supported) it will be checked with this `rule` and make transforms more stable.*
507
528
 
508
529
  ## declare
509
530
 
@@ -520,10 +541,7 @@ isIdentifier(a);
520
541
  ### ✅ Example of correct code
521
542
 
522
543
  ```js
523
- const {
524
- operator,
525
- types,
526
- } = require('putout');
544
+ const {operator, types} = require('putout');
527
545
 
528
546
  const {compare} = operator;
529
547
  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,55 @@
1
+ 'use strict';
2
+
3
+ const {operator, types} = require('putout');
4
+
5
+ const {
6
+ getTemplateValues,
7
+ traverse,
8
+ } = operator;
9
+
10
+ const {isStringLiteral} = types;
11
+
12
+ const PATTERN_MATCH = 'module.exports.match = () => __object';
13
+ const PATTERN_REPLACE = 'module.exports.replace = () => __object';
14
+
15
+ module.exports.report = () => `☝️ Looks like 'match()' template absent in 'replace()'`;
16
+
17
+ module.exports.replace = () => ({
18
+ [PATTERN_MATCH]: PATTERN_MATCH,
19
+ });
20
+
21
+ module.exports.match = () => ({
22
+ [PATTERN_MATCH]: ({__object}, path) => {
23
+ const namesMatch = [];
24
+
25
+ for (const prop of __object.properties) {
26
+ if (!isStringLiteral(prop.key))
27
+ continue;
28
+
29
+ namesMatch.push(prop.key.value);
30
+ }
31
+
32
+ const namesReplace = [];
33
+
34
+ traverse(path.parentPath.parentPath, {
35
+ [PATTERN_REPLACE]: (path) => {
36
+ const {__object} = getTemplateValues(path, PATTERN_REPLACE);
37
+
38
+ for (const prop of __object.properties) {
39
+ if (!isStringLiteral(prop.key))
40
+ continue;
41
+
42
+ namesReplace.push(prop.key.value);
43
+ }
44
+ },
45
+ });
46
+
47
+ for (const name of namesMatch) {
48
+ if (!namesReplace.includes(name)) {
49
+ return true;
50
+ }
51
+ }
52
+
53
+ return false;
54
+ },
55
+ });
@@ -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;
@@ -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([
package/lib/index.js CHANGED
@@ -10,6 +10,8 @@ module.exports.rules = {
10
10
  ...getRule('apply-create-test'),
11
11
  ...getRule('apply-remove'),
12
12
  ...getRule('apply-declare'),
13
+ ...getRule('check-replace-code'),
14
+ ...getRule('check-match'),
13
15
  ...getRule('convert-putout-test-to-create-test'),
14
16
  ...getRule('convert-to-no-transform-code'),
15
17
  ...getRule('convert-find-to-traverse'),
@@ -33,7 +35,6 @@ module.exports.rules = {
33
35
  ...getRule('rename-operate-to-operator'),
34
36
  ...getRule('replace-operate-with-operator'),
35
37
  ...getRule('shorten-imports'),
36
- ...getRule('check-replace-code'),
37
38
  ...getRule('declare'),
38
39
  ...getRule('add-args'),
39
40
  ...getRule('add-push'),
@@ -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.7.0",
3
+ "version": "13.0.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
47
  "nodemon": "^2.0.1"
48
48
  },
49
49
  "peerDependencies": {
50
- "putout": ">=29"
50
+ "putout": ">=30"
51
51
  },
52
52
  "license": "MIT",
53
53
  "engines": {