@putout/plugin-putout 8.1.0 → 8.4.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
@@ -3,7 +3,7 @@
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-putout.svg?style=flat&longCache=true
4
4
  [NPMURL]: https://npmjs.org/package/@putout/plugin-putout"npm"
5
5
 
6
- 🐊[`Putout`](https://github.com/coderaiser/putout) plugin helps with 🐊[`Putout`](https://github.com/coderaiser/putout) plugins development.
6
+ 🐊[`Putout`](https://github.com/coderaiser/putout) plugin helps with 🐊`Putout` plugins development.
7
7
 
8
8
  ## Install
9
9
 
@@ -19,6 +19,7 @@ npm i @putout/plugin-putout -D
19
19
  "putout/apply-processors-destructuring": "on",
20
20
  "putout/apply-async-formatter": "on",
21
21
  "putout/add-args": "on",
22
+ "putout/convert-putout-test-to-create-test": "on",
22
23
  "putout/convert-to-no-transform-code": "on",
23
24
  "putout/convert-replace-with": "on",
24
25
  "putout/convert-replace-with-multiple": "on",
@@ -32,7 +33,8 @@ npm i @putout/plugin-putout -D
32
33
  "putout/convert-add-argument-to-add-args": "on",
33
34
  "putout/shorten-imports": "on",
34
35
  "putout/check-replace-code": "on",
35
- "putout/declare": "on"
36
+ "putout/declare": "on",
37
+ "putout/move-require-on-top-level": "on"
36
38
  }
37
39
  }
38
40
  ```
@@ -75,6 +77,30 @@ test('formatter: codeframea', async ({format}) => {
75
77
  });
76
78
  ```
77
79
 
80
+ ## convert-putout-test-to-create-test"
81
+
82
+ Fixes results of [@putout/convert-commonjs-to-esm](https://github.com/coderaiser/putout/tree/master/packages/plugin-convert-commonjs-to-esm) work.
83
+
84
+ ### ❌ Incorrect code example
85
+
86
+ ```js
87
+ import putoutTest from '@putout/test';
88
+
89
+ const test = putoutTest(__dirname, {
90
+ 'remove-unused-variables': rmVars,
91
+ });
92
+ ```
93
+
94
+ ### ✅ Correct code Example
95
+
96
+ ```js
97
+ import createTest from '@putout/test';
98
+
99
+ const test = createTest(__dirname, {
100
+ 'remove-unused-variables': rmVars,
101
+ });
102
+ ```
103
+
78
104
  ## convert-to-no-transform-code
79
105
 
80
106
  ### ❌ Incorrect code example
@@ -304,7 +330,8 @@ module.exports.replace = () => ({
304
330
  });
305
331
  ```
306
332
 
307
- 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 possitives when running on code. This 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 code more stable and transform bugs.
333
+ 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 possitives when running on code.
334
+ 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.
308
335
 
309
336
  ## declare
310
337
 
@@ -369,6 +396,39 @@ module.exports = addArgs({
369
396
  });
370
397
  ```
371
398
 
399
+ ## move-require-on-top-level
400
+
401
+ ### ❌ Incorrect code example
402
+
403
+ ```js
404
+ const test = require('@putout/test')(__dirname, {
405
+ 'remove-debugger': require('..'),
406
+ });
407
+
408
+ test('remove debugger: report', (t) => {
409
+ t.transform('debugger', {
410
+ 'remove-debugger': require('..'),
411
+ });
412
+ t.end();
413
+ });
414
+ ```
415
+
416
+ ### ✅ Correct code Example
417
+
418
+ ```js
419
+ const removeDebugger = require('..');
420
+ const test = require('@putout/test')(__dirname, {
421
+ 'remove-debugger': removeDebugger,
422
+ });
423
+
424
+ test('remove debugger: report', (t) => {
425
+ const test = require('@putout/test')(__dirname, {
426
+ 'remove-debugger': removeDebugger,
427
+ });
428
+ t.end();
429
+ });
430
+ ```
431
+
372
432
  ## License
373
433
 
374
434
  MIT
@@ -5,11 +5,16 @@ const tryCatch = require('try-catch');
5
5
 
6
6
  const generateCode = require('./generate-code');
7
7
 
8
- const {operator} = putout;
8
+ const {operator, types} = putout;
9
+ const {
10
+ isMemberExpression,
11
+ isObjectExpression,
12
+ } = types;
9
13
 
10
14
  const {
11
15
  compare,
12
16
  extract,
17
+ getBindingPath,
13
18
  } = operator;
14
19
 
15
20
  const name = '__putout_plugin_check_replace_code';
@@ -48,8 +53,13 @@ module.exports.traverse = ({push}) => ({
48
53
  continue;
49
54
 
50
55
  const {node} = propertyPath;
51
- const key = extract(node.key);
56
+ const key = compute(propertyPath);
57
+
58
+ if (!key)
59
+ continue;
60
+
52
61
  const template = extract(node.value);
62
+
53
63
  const [generateError, keyCode] = generateCode(path, key);
54
64
 
55
65
  if (generateError) {
@@ -96,3 +106,34 @@ module.exports.traverse = ({push}) => ({
96
106
  },
97
107
  });
98
108
 
109
+ function compute(path) {
110
+ const {key, computed} = path.node;
111
+
112
+ if (!computed)
113
+ return extract(key);
114
+
115
+ if (!isMemberExpression(key))
116
+ return '';
117
+
118
+ const bindingPath = getBindingPath(path, extract(key.object));
119
+
120
+ if (!bindingPath)
121
+ return '';
122
+
123
+ const bindingNode = bindingPath.node;
124
+
125
+ if (!isObjectExpression(bindingNode.init))
126
+ return '';
127
+
128
+ const keyPropertyValue = extract(key.property);
129
+
130
+ for (const property of bindingNode.init.properties) {
131
+ const keyValue = extract(property.key);
132
+
133
+ if (keyValue === keyPropertyValue)
134
+ return extract(property.value);
135
+ }
136
+
137
+ return `'not found'`;
138
+ }
139
+
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Use 'createTest' instead of 'putoutTest'`;
4
+
5
+ module.exports.filter = ({scope}) => !scope.bindings.createTest;
6
+
7
+ module.exports.include = () => [
8
+ 'import putoutTest from "@putout/test"',
9
+ ];
10
+
11
+ module.exports.fix = (path) => {
12
+ path.scope.rename('putoutTest', 'createTest');
13
+ };
14
+
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ const getRule = (a) => ({
7
7
  module.exports.rules = {
8
8
  ...getRule('apply-processors-destructuring'),
9
9
  ...getRule('apply-async-formatter'),
10
+ ...getRule('convert-putout-test-to-create-test'),
10
11
  ...getRule('convert-to-no-transform-code'),
11
12
  ...getRule('convert-find-to-traverse'),
12
13
  ...getRule('convert-replace-with'),
@@ -25,5 +26,6 @@ module.exports.rules = {
25
26
  ...getRule('check-replace-code'),
26
27
  ...getRule('declare'),
27
28
  ...getRule('add-args'),
29
+ ...getRule('move-require-on-top-level'),
28
30
  };
29
31
 
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ const justCamelCase = require('just-camel-case');
4
+ const {
5
+ types,
6
+ template,
7
+ } = require('putout');
8
+
9
+ const TEST = `
10
+ const test = require('@putout/test')(__dirname, {
11
+ __a: __b
12
+ });
13
+ `;
14
+
15
+ const TRANSFORM = `
16
+ t.transform(__c, {
17
+ __a: __b
18
+ });
19
+ `;
20
+
21
+ const {
22
+ Identifier,
23
+ isIdentifier,
24
+ } = types;
25
+
26
+ module.exports.report = () => 'Move require on top level';
27
+
28
+ module.exports.match = () => ({
29
+ [TEST]: ({__b}) => !isIdentifier(__b),
30
+ [TRANSFORM]: ({__b}) => !isIdentifier(__b),
31
+ });
32
+
33
+ module.exports.replace = () => ({
34
+ [TEST]: (vars, path) => {
35
+ const name = declareRequire(vars, path);
36
+ const {__a} = vars;
37
+ const value = __a.value || __a.name;
38
+
39
+ return `
40
+ const test = require('@putout/test')(__dirname, {
41
+ '${value}': ${name},
42
+ });
43
+ `;
44
+ },
45
+ [TRANSFORM]: (vars, path) => {
46
+ const name = declareRequire(vars, path);
47
+ const {__a} = vars;
48
+ const value = __a.value || __a.name;
49
+
50
+ return `
51
+ t.transform(__c, {
52
+ '${value}': ${name},
53
+ });
54
+ `;
55
+ },
56
+ });
57
+
58
+ const buildRequire = template(`const NAME = REQUIRE`);
59
+
60
+ function declareRequire({__a, __b}, path) {
61
+ const shortName = (__a.value || __a.name).split('/').pop();
62
+ const name = justCamelCase(shortName);
63
+ const requireNode = buildRequire({
64
+ NAME: Identifier(name),
65
+ REQUIRE: __b,
66
+ });
67
+
68
+ if (path.scope.hasBinding(name))
69
+ return name;
70
+
71
+ const programPath = path.scope.getProgramParent().path;
72
+ programPath.node.body.unshift(requireNode);
73
+
74
+ return name;
75
+ }
76
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "8.1.0",
3
+ "version": "8.4.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "putout plugin helps with plugins development",
6
6
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-putout",
@@ -13,6 +13,7 @@
13
13
  "url": "git://github.com/coderaiser/putout.git"
14
14
  },
15
15
  "scripts": {
16
+ "wisdom": "madrun wisdom",
16
17
  "test": "madrun test",
17
18
  "watch:test": "madrun watch:test",
18
19
  "lint": "madrun lint",
@@ -24,6 +25,7 @@
24
25
  },
25
26
  "dependencies": {
26
27
  "fullstore": "^3.0.0",
28
+ "just-camel-case": "^6.0.1",
27
29
  "try-catch": "^3.0.0"
28
30
  },
29
31
  "keywords": [