@putout/plugin-putout 23.6.0 → 23.8.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
@@ -37,6 +37,7 @@ npm i @putout/plugin-putout -D
37
37
  - ✅ [apply-parens](#apply-parens);
38
38
  - ✅ [apply-short-processors](#apply-short-processors);
39
39
  - ✅ [check-match](#check-match);
40
+ - ✅ [check-declare](#check-declare);
40
41
  - ✅ [check-replace-code](#check-replace-code);
41
42
  - ✅ [convert-add-argument-to-add-args](#convert-add-argument-to-add-test-args);
42
43
  - ✅ [convert-babel-types](#convert-babel-types);
@@ -105,6 +106,7 @@ npm i @putout/plugin-putout -D
105
106
  "putout/apply-for-of-to-track-file": "on",
106
107
  "putout/apply-fixture-name-to-message": "on",
107
108
  "putout/check-match": "on",
109
+ "putout/check-declare": "on",
108
110
  "putout/check-replace-code": ["on", {
109
111
  "once": true
110
112
  }],
@@ -162,7 +164,6 @@ path.node = Identifier('x');
162
164
  path.node = identifier('x');
163
165
  ```
164
166
 
165
-
166
167
  ## apply-processors-destructuring
167
168
 
168
169
  ### ❌ Example of incorrect code
@@ -864,6 +865,23 @@ module.exports.match = () => ({
864
865
  });
865
866
  ```
866
867
 
868
+ ## check-declare
869
+
870
+ Checks that [Declarator](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#declarator) transform is possible.
871
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9fccb187bb8933afff0dc0db57b3cea8/25cb812978fadb4efb5f0cb44058a61f70f8b78e):
872
+
873
+ ### ❌ Example of incorrect code
874
+
875
+ ```js
876
+ module.exports.declare = () => ({
877
+ isNumber: 'const isNumber = () => {}',
878
+ isString: 'const isNumber = () => {}',
879
+ });
880
+ ```
881
+
882
+ ☝️ *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.
883
+ 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.*
884
+
867
885
  ## check-replace-code
868
886
 
869
887
  Checks that [Replacer](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#replacer) transform is possible.
@@ -7,13 +7,14 @@ const {
7
7
  identifier,
8
8
  } = types;
9
9
 
10
- const {traverse} = operator;
10
+ const {traverse, getBinding} = operator;
11
11
 
12
12
  const defaultNames = [
13
13
  'push',
14
14
  'store',
15
15
  'listStore',
16
16
  'pathStore',
17
+ 'options',
17
18
  ];
18
19
 
19
20
  module.exports.report = ({name}) => `Add '${name}' argument to 'traverse'`;
@@ -73,22 +74,23 @@ const checkArgs = (names, push) => (path) => {
73
74
  if (isArgExists(name, fn))
74
75
  return;
75
76
 
77
+ if (getBinding(path, name))
78
+ return;
79
+
76
80
  push({
77
81
  path,
78
82
  fn,
79
83
  name,
80
84
  });
81
85
  },
82
- [`__a(__args)`]: (currentPath) => {
83
- const {callee} = currentPath.node;
86
+ [`__a(__args)`]: (path) => {
87
+ const {callee} = path.node;
84
88
  const {name} = callee;
85
89
 
86
90
  if (!names.includes(name))
87
91
  return;
88
92
 
89
- const bindings = currentPath.scope.getAllBindings();
90
-
91
- if (bindings[name])
93
+ if (getBinding(path, name))
92
94
  return;
93
95
 
94
96
  push({
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ const tryCatch = require('try-catch');
4
+ const putout = require('putout');
5
+
6
+ const {types, operator} = putout;
7
+
8
+ const noop = () => {};
9
+ const {getTemplateValues} = operator;
10
+
11
+ const DECLARE_ESM = 'export const declare = () => __object';
12
+ const DECLARE_COMMONJS = 'module.exports.declare = () => __object';
13
+ const COMMONJS = 'module.exports = __object';
14
+
15
+ const {
16
+ isTemplateLiteral,
17
+ isStringLiteral,
18
+ } = types;
19
+
20
+ module.exports.report = ({message}) => message;
21
+
22
+ module.exports.fix = noop;
23
+ module.exports.traverse = ({push}) => ({
24
+ [DECLARE_ESM]: createCheck(push, DECLARE_ESM),
25
+ [DECLARE_COMMONJS]: createCheck(push, DECLARE_COMMONJS),
26
+ [COMMONJS]: createCheck(push, COMMONJS),
27
+ });
28
+
29
+ const createCheck = (push, template) => (path) => {
30
+ const {__object} = getTemplateValues(path, template);
31
+
32
+ const lines = [];
33
+
34
+ for (const {value} of __object.properties) {
35
+ let current = '';
36
+
37
+ if (isStringLiteral(value)) {
38
+ current = value.value;
39
+ } else if (isTemplateLiteral(value)) {
40
+ if (value.quasis.length > 1)
41
+ continue;
42
+
43
+ current = value.quasis[0].value.cooked;
44
+ }
45
+
46
+ if (/^(const|import)/.test(current))
47
+ lines.push(current);
48
+ }
49
+
50
+ const source = lines.join(';\n');
51
+ const [error] = tryCatch(putout.parse, source);
52
+
53
+ if (!error)
54
+ return;
55
+
56
+ const {message} = error;
57
+
58
+ push({
59
+ path,
60
+ message,
61
+ });
62
+ };
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ const applyRemove = require('./apply-remove');
7
7
  const applyInsertBefore = require('./apply-insert-before');
8
8
  const applyInsertAfter = require('./apply-insert-after');
9
9
  const applyDeclare = require('./apply-declare');
10
+ const checkDeclare = require('./check-declare');
10
11
  const checkReplaceCode = require('./check-replace-code');
11
12
  const checkMatch = require('./check-match');
12
13
  const convertPutoutTestToCreateTest = require('./convert-putout-test-to-create-test');
@@ -71,6 +72,7 @@ module.exports.rules = {
71
72
  'apply-insert-before': applyInsertBefore,
72
73
  'apply-insert-after': applyInsertAfter,
73
74
  'apply-declare': applyDeclare,
75
+ 'check-declare': checkDeclare,
74
76
  'check-replace-code': checkReplaceCode,
75
77
  'check-match': checkMatch,
76
78
  'convert-putout-test-to-create-test': convertPutoutTestToCreateTest,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "23.6.0",
3
+ "version": "23.8.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",