@putout/plugin-putout 8.2.0 → 8.5.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
 
@@ -33,7 +33,8 @@ npm i @putout/plugin-putout -D
33
33
  "putout/convert-add-argument-to-add-args": "on",
34
34
  "putout/shorten-imports": "on",
35
35
  "putout/check-replace-code": "on",
36
- "putout/declare": "on"
36
+ "putout/declare": "on",
37
+ "putout/move-require-on-top-level": "on"
37
38
  }
38
39
  }
39
40
  ```
@@ -75,18 +76,29 @@ test('formatter: codeframea', async ({format}) => {
75
76
  await format(codeframe, 1);
76
77
  });
77
78
  ```
79
+
78
80
  ## convert-putout-test-to-create-test"
79
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
+
80
84
  ### ❌ Incorrect code example
81
85
 
82
86
  ```js
83
87
  import putoutTest from '@putout/test';
88
+
89
+ const test = putoutTest(__dirname, {
90
+ 'remove-unused-variables': rmVars,
91
+ });
84
92
  ```
85
93
 
86
94
  ### ✅ Correct code Example
87
95
 
88
96
  ```js
89
97
  import createTest from '@putout/test';
98
+
99
+ const test = createTest(__dirname, {
100
+ 'remove-unused-variables': rmVars,
101
+ });
90
102
  ```
91
103
 
92
104
  ## convert-to-no-transform-code
@@ -318,7 +330,8 @@ module.exports.replace = () => ({
318
330
  });
319
331
  ```
320
332
 
321
- 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.
322
335
 
323
336
  ## declare
324
337
 
@@ -383,6 +396,39 @@ module.exports = addArgs({
383
396
  });
384
397
  ```
385
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
+
386
432
  ## License
387
433
 
388
434
  MIT
@@ -10,6 +10,7 @@ const {operator} = putout;
10
10
  const {
11
11
  compare,
12
12
  extract,
13
+ compute,
13
14
  } = operator;
14
15
 
15
16
  const name = '__putout_plugin_check_replace_code';
@@ -28,7 +29,7 @@ module.exports.report = ({path, code, error}) => {
28
29
  if (error)
29
30
  return error.message;
30
31
 
31
- const key = extract(path.node.key);
32
+ const [, key] = parseKey(path);
32
33
  const value = extract(path.node.value);
33
34
 
34
35
  return `transform mismatch: "${key}" -> "${value}" !== "${code}"`;
@@ -48,7 +49,17 @@ module.exports.traverse = ({push}) => ({
48
49
  continue;
49
50
 
50
51
  const {node} = propertyPath;
51
- const key = extract(node.key);
52
+ const [parseError, key] = parseKey(propertyPath);
53
+
54
+ if (parseError) {
55
+ push({
56
+ error: parseError,
57
+ mainPath: path,
58
+ path: propertyPath,
59
+ });
60
+ return;
61
+ }
62
+
52
63
  const template = extract(node.value);
53
64
  const [generateError, keyCode] = generateCode(path, key);
54
65
 
@@ -96,3 +107,18 @@ module.exports.traverse = ({push}) => ({
96
107
  },
97
108
  });
98
109
 
110
+ function parseKey(propertyPath) {
111
+ const {node} = propertyPath;
112
+
113
+ if (!node.computed)
114
+ return [null, extract(node.key)];
115
+
116
+ const keyPath = propertyPath.get('key');
117
+ const [isComputed, key] = compute(keyPath);
118
+
119
+ if (!isComputed)
120
+ return [Error(`Replace key cannot be computed: '${keyPath.toString()}'`)];
121
+
122
+ return [null, key];
123
+ }
124
+
@@ -2,10 +2,13 @@
2
2
 
3
3
  module.exports.report = () => `Use 'createTest' instead of 'putoutTest'`;
4
4
 
5
- module.exports.replace = () => ({
6
- 'import putoutTest from "@putout/test"': (vars, path) => {
7
- path.scope.rename('putoutTest', 'createTest');
8
- return path;
9
- },
10
- });
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
+ };
11
14
 
package/lib/index.js CHANGED
@@ -26,5 +26,6 @@ module.exports.rules = {
26
26
  ...getRule('check-replace-code'),
27
27
  ...getRule('declare'),
28
28
  ...getRule('add-args'),
29
+ ...getRule('move-require-on-top-level'),
29
30
  };
30
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.2.0",
3
+ "version": "8.5.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": [