@putout/plugin-putout 8.2.1 → 8.5.1

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
@@ -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
  ```
@@ -395,6 +396,39 @@ module.exports = addArgs({
395
396
  });
396
397
  ```
397
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
+
398
432
  ## License
399
433
 
400
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,13 @@ module.exports.traverse = ({push}) => ({
96
107
  },
97
108
  });
98
109
 
110
+ function parseKey(propertyPath) {
111
+ const keyPath = propertyPath.get('key');
112
+ const [isComputed, key] = compute(keyPath);
113
+
114
+ if (!isComputed)
115
+ return [Error(`Replace key cannot be computed: '${keyPath.toString()}'`)];
116
+
117
+ return [null, key];
118
+ }
119
+
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.1",
3
+ "version": "8.5.1",
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",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "fullstore": "^3.0.0",
28
+ "just-camel-case": "^6.0.1",
28
29
  "try-catch": "^3.0.0"
29
30
  },
30
31
  "keywords": [