@putout/plugin-putout 14.2.0 → 14.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 +29 -3
- package/lib/apply-namaspace-specifier/index.js +33 -15
- package/lib/convert-get-rule-to-require/index.js +78 -0
- package/lib/declare/index.js +5 -0
- package/lib/index.js +80 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,6 +46,7 @@ npm i @putout/plugin-putout -D
|
|
|
46
46
|
"putout/convert-dirname-to-url": "on",
|
|
47
47
|
"putout/convert-url-to-dirname": "on",
|
|
48
48
|
"putout/convert-report-to-function": "on",
|
|
49
|
+
"putout/convert-get-rule-to-require": "on",
|
|
49
50
|
"putout/create-test": "on",
|
|
50
51
|
"putout/shorten-imports": "on",
|
|
51
52
|
"putout/declare": "on",
|
|
@@ -711,7 +712,7 @@ const test = createTest(import.meta.url, {
|
|
|
711
712
|
});
|
|
712
713
|
```
|
|
713
714
|
|
|
714
|
-
## convert-url-to-dirname
|
|
715
|
+
## convert-url-to-dirname
|
|
715
716
|
|
|
716
717
|
### ❌ Example of incorrect code
|
|
717
718
|
|
|
@@ -749,6 +750,32 @@ module.exports.report = `'report' should be a 'function'`;
|
|
|
749
750
|
module.exports.report = () => `'report' should be a 'function'`;
|
|
750
751
|
```
|
|
751
752
|
|
|
753
|
+
## convert-get-rule-to-require
|
|
754
|
+
|
|
755
|
+
- ✅ import [Nested plugins](https://github.com/coderaiser/putout/tree/master/packages/engine-loader#nested-plugin) in [**Deno** and **Browser**](https://github.com/putoutjs/bundle/);
|
|
756
|
+
- ✅ easier bundle with rollup without [`dynamicRequireTargets`](https://github.com/rollup/plugins/tree/master/packages/commonjs/#dynamicrequiretargets);
|
|
757
|
+
- ✅ easier to migrate to **ESM**;
|
|
758
|
+
|
|
759
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/38336fcc5b5ae6e441697e098067319c/dd98578c9554b7bd5dceee0499118f7d8216e1da).
|
|
760
|
+
|
|
761
|
+
### ❌ Example of incorrect code
|
|
762
|
+
|
|
763
|
+
```js
|
|
764
|
+
module.exports.rules = {
|
|
765
|
+
...getRule('remove-unused-variables'),
|
|
766
|
+
};
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
### ✅ Example of correct code
|
|
770
|
+
|
|
771
|
+
```js
|
|
772
|
+
const removeUnusedVariables = require('./remove-unused-variables');
|
|
773
|
+
|
|
774
|
+
module.exports.rules = {
|
|
775
|
+
'remove-unused-variables': removeUnusedVariables,
|
|
776
|
+
};
|
|
777
|
+
```
|
|
778
|
+
|
|
752
779
|
## move-require-on-top-level
|
|
753
780
|
|
|
754
781
|
### ❌ Example of incorrect code
|
|
@@ -776,10 +803,9 @@ const test = require('@putout/test')(__dirname, {
|
|
|
776
803
|
});
|
|
777
804
|
|
|
778
805
|
test('remove debugger: report', (t) => {
|
|
779
|
-
|
|
806
|
+
t.transform('debugger', {
|
|
780
807
|
'remove-debugger': removeDebugger,
|
|
781
808
|
});
|
|
782
|
-
|
|
783
809
|
t.end();
|
|
784
810
|
});
|
|
785
811
|
```
|
|
@@ -6,7 +6,31 @@ module.exports.fix = ({first}) => {
|
|
|
6
6
|
first.node.type = 'ImportNamespaceSpecifier';
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
module.exports.traverse = ({push}) => ({
|
|
9
|
+
module.exports.traverse = ({push, listStore}) => ({
|
|
10
|
+
'export const rules = __object': listStore,
|
|
11
|
+
...createImportVisitor({
|
|
12
|
+
push,
|
|
13
|
+
names: [
|
|
14
|
+
'./index.js',
|
|
15
|
+
'../lib/index.js',
|
|
16
|
+
],
|
|
17
|
+
}),
|
|
18
|
+
'Program': {
|
|
19
|
+
exit(path) {
|
|
20
|
+
const rules = listStore();
|
|
21
|
+
|
|
22
|
+
if (!rules.length)
|
|
23
|
+
return;
|
|
24
|
+
|
|
25
|
+
path.traverse(createImportVisitor({
|
|
26
|
+
push,
|
|
27
|
+
names: ['any'],
|
|
28
|
+
}));
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const createImportVisitor = ({push, names}) => ({
|
|
10
34
|
ImportDeclaration(path) {
|
|
11
35
|
const {value} = path.node.source;
|
|
12
36
|
const first = path.get('specifiers.0');
|
|
@@ -17,20 +41,14 @@ module.exports.traverse = ({push}) => ({
|
|
|
17
41
|
if (first.isImportNamespaceSpecifier())
|
|
18
42
|
return;
|
|
19
43
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (value === '../lib/index.js') {
|
|
29
|
-
push({
|
|
30
|
-
path,
|
|
31
|
-
first,
|
|
32
|
-
});
|
|
33
|
-
return;
|
|
44
|
+
for (const name of names) {
|
|
45
|
+
if (value === name || name === 'any') {
|
|
46
|
+
push({
|
|
47
|
+
path,
|
|
48
|
+
first,
|
|
49
|
+
});
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
34
52
|
}
|
|
35
53
|
},
|
|
36
54
|
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const camel = require('just-camel-case');
|
|
4
|
+
const {
|
|
5
|
+
types,
|
|
6
|
+
operator,
|
|
7
|
+
template,
|
|
8
|
+
} = require('putout');
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
replaceWith,
|
|
12
|
+
insertBefore,
|
|
13
|
+
compare,
|
|
14
|
+
} = operator;
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
ObjectProperty,
|
|
18
|
+
Identifier,
|
|
19
|
+
StringLiteral,
|
|
20
|
+
} = types;
|
|
21
|
+
|
|
22
|
+
module.exports.report = () => `Use top-level 'require()' instead of '...getRule()'`;
|
|
23
|
+
|
|
24
|
+
const REQUIRE = `const __a = require(__b)`;
|
|
25
|
+
const createRequire = template(REQUIRE, {
|
|
26
|
+
placeholderPattern: /__/,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
module.exports.match = () => ({
|
|
30
|
+
'getRule(__a)': ({__a}, path) => {
|
|
31
|
+
const name = __a.value;
|
|
32
|
+
|
|
33
|
+
if (!path.parentPath.isSpreadElement())
|
|
34
|
+
return false;
|
|
35
|
+
|
|
36
|
+
return !path.scope.getAllBindings()[name];
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
module.exports.replace = () => ({
|
|
41
|
+
'getRule(__a)': ({__a}, path) => {
|
|
42
|
+
const name = camel(__a.value);
|
|
43
|
+
const id = Identifier(name);
|
|
44
|
+
const node = ObjectProperty(__a, id);
|
|
45
|
+
|
|
46
|
+
replaceWith(path.parentPath, node);
|
|
47
|
+
|
|
48
|
+
const programPath = path.scope.getProgramParent().path;
|
|
49
|
+
const body = programPath.get('body');
|
|
50
|
+
const [first] = body;
|
|
51
|
+
|
|
52
|
+
const nodeRequire = createRequire({
|
|
53
|
+
__a: id,
|
|
54
|
+
__b: StringLiteral(`./${__a.value}`),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (compare(first, REQUIRE)) {
|
|
58
|
+
const latest = getLatest(body.slice(1));
|
|
59
|
+
insertBefore(latest, nodeRequire);
|
|
60
|
+
|
|
61
|
+
return path;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
programPath.node.body.unshift(nodeRequire);
|
|
65
|
+
|
|
66
|
+
return path;
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
function getLatest(body) {
|
|
71
|
+
let path;
|
|
72
|
+
|
|
73
|
+
for (path of body)
|
|
74
|
+
if (!compare(path, REQUIRE))
|
|
75
|
+
break;
|
|
76
|
+
|
|
77
|
+
return path;
|
|
78
|
+
}
|
package/lib/declare/index.js
CHANGED
|
@@ -3,9 +3,14 @@
|
|
|
3
3
|
const types = require('./types');
|
|
4
4
|
const operator = require('./operator');
|
|
5
5
|
|
|
6
|
+
const getRule = (a) => ({
|
|
7
|
+
[a]: require(`./${a}`),
|
|
8
|
+
});
|
|
9
|
+
|
|
6
10
|
module.exports.declare = () => ({
|
|
7
11
|
template: `import {template} from 'putout'`,
|
|
8
12
|
createTest: `import {createTest} from '@putout/test'`,
|
|
9
13
|
...operator,
|
|
10
14
|
...types,
|
|
15
|
+
getRule: `const getRule = ${getRule.toString()};`,
|
|
11
16
|
});
|
package/lib/index.js
CHANGED
|
@@ -1,47 +1,85 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const applyProcessorsDestructuring = require('./apply-processors-destructuring');
|
|
4
|
+
const applyAsyncFormatter = require('./apply-async-formatter');
|
|
5
|
+
const applyCreateTest = require('./apply-create-test');
|
|
6
|
+
const applyRemove = require('./apply-remove');
|
|
7
|
+
const applyInsertBefore = require('./apply-insert-before');
|
|
8
|
+
const applyInsertAfter = require('./apply-insert-after');
|
|
9
|
+
const applyDeclare = require('./apply-declare');
|
|
10
|
+
const checkReplaceCode = require('./check-replace-code');
|
|
11
|
+
const checkMatch = require('./check-match');
|
|
12
|
+
const convertPutoutTestToCreateTest = require('./convert-putout-test-to-create-test');
|
|
13
|
+
const convertToNoTransformCode = require('./convert-to-no-transform-code');
|
|
14
|
+
const convertFindToTraverse = require('./convert-find-to-traverse');
|
|
15
|
+
const convertDestructuringToIdentifier = require('./convert-destructuring-to-identifier');
|
|
16
|
+
const convertNumberToNumeric = require('./convert-number-to-numeric');
|
|
17
|
+
const convertReplaceWith = require('./convert-replace-with');
|
|
18
|
+
const convertReplaceWithMultiple = require('./convert-replace-with-multiple');
|
|
19
|
+
const convertReplaceToFunction = require('./convert-replace-to-function');
|
|
20
|
+
const convertMatchToFunction = require('./convert-match-to-function');
|
|
21
|
+
const convertBabelTypes = require('./convert-babel-types');
|
|
22
|
+
const convertNodeToPathInGetTemplateValues = require('./convert-node-to-path-in-get-template-values');
|
|
23
|
+
const convertTraverseToInclude = require('./convert-traverse-to-include');
|
|
24
|
+
const convertTraverseToReplace = require('./convert-traverse-to-replace');
|
|
25
|
+
const convertProcessToFind = require('./convert-process-to-find');
|
|
26
|
+
const convertMethodToProperty = require('./convert-method-to-property');
|
|
27
|
+
const convertAddArgumentToAddArgs = require('./convert-add-argument-to-add-args');
|
|
28
|
+
const convertDirnameToUrl = require('./convert-dirname-to-url');
|
|
29
|
+
const convertUrlToDirname = require('./convert-url-to-dirname');
|
|
30
|
+
const convertReportToFunction = require('./convert-report-to-function');
|
|
31
|
+
const replaceTestMessage = require('./replace-test-message');
|
|
32
|
+
const renameOperateToOperator = require('./rename-operate-to-operator');
|
|
33
|
+
const replaceOperateWithOperator = require('./replace-operate-with-operator');
|
|
34
|
+
const shortenImports = require('./shorten-imports');
|
|
35
|
+
const declare = require('./declare');
|
|
36
|
+
const addArgs = require('./add-args');
|
|
37
|
+
const addPush = require('./add-push');
|
|
38
|
+
const moveRequireOnTopLevel = require('./move-require-on-top-level');
|
|
39
|
+
const includer = require('./includer');
|
|
40
|
+
const createTest = require('./create-test');
|
|
41
|
+
const applyNamaspaceSpecifier = require('./apply-namaspace-specifier');
|
|
42
|
+
const convertGetRuleToRequire = require('./convert-get-rule-to-require');
|
|
6
43
|
|
|
7
44
|
module.exports.rules = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
'apply-processors-destructuring': applyProcessorsDestructuring,
|
|
46
|
+
'apply-async-formatter': applyAsyncFormatter,
|
|
47
|
+
'apply-create-test': applyCreateTest,
|
|
48
|
+
'apply-remove': applyRemove,
|
|
49
|
+
'apply-insert-before': applyInsertBefore,
|
|
50
|
+
'apply-insert-after': applyInsertAfter,
|
|
51
|
+
'apply-declare': applyDeclare,
|
|
52
|
+
'check-replace-code': checkReplaceCode,
|
|
53
|
+
'check-match': checkMatch,
|
|
54
|
+
'convert-putout-test-to-create-test': convertPutoutTestToCreateTest,
|
|
55
|
+
'convert-to-no-transform-code': convertToNoTransformCode,
|
|
56
|
+
'convert-find-to-traverse': convertFindToTraverse,
|
|
57
|
+
'convert-destructuring-to-identifier': convertDestructuringToIdentifier,
|
|
58
|
+
'convert-number-to-numeric': convertNumberToNumeric,
|
|
59
|
+
'convert-replace-with': convertReplaceWith,
|
|
60
|
+
'convert-replace-with-multiple': convertReplaceWithMultiple,
|
|
61
|
+
'convert-replace-to-function': convertReplaceToFunction,
|
|
62
|
+
'convert-match-to-function': convertMatchToFunction,
|
|
63
|
+
'convert-babel-types': convertBabelTypes,
|
|
64
|
+
'convert-node-to-path-in-get-template-values': convertNodeToPathInGetTemplateValues,
|
|
65
|
+
'convert-traverse-to-include': convertTraverseToInclude,
|
|
66
|
+
'convert-traverse-to-replace': convertTraverseToReplace,
|
|
67
|
+
'convert-process-to-find': convertProcessToFind,
|
|
68
|
+
'convert-method-to-property': convertMethodToProperty,
|
|
69
|
+
'convert-add-argument-to-add-args': convertAddArgumentToAddArgs,
|
|
70
|
+
'convert-dirname-to-url': convertDirnameToUrl,
|
|
71
|
+
'convert-url-to-dirname': convertUrlToDirname,
|
|
72
|
+
'convert-report-to-function': convertReportToFunction,
|
|
73
|
+
'replace-test-message': replaceTestMessage,
|
|
74
|
+
'rename-operate-to-operator': renameOperateToOperator,
|
|
75
|
+
'replace-operate-with-operator': replaceOperateWithOperator,
|
|
76
|
+
'shorten-imports': shortenImports,
|
|
77
|
+
declare,
|
|
78
|
+
'add-args': addArgs,
|
|
79
|
+
'add-push': addPush,
|
|
80
|
+
'move-require-on-top-level': moveRequireOnTopLevel,
|
|
81
|
+
includer,
|
|
82
|
+
'create-test': createTest,
|
|
83
|
+
'apply-namaspace-specifier': applyNamaspaceSpecifier,
|
|
84
|
+
'convert-get-rule-to-require': convertGetRuleToRequire,
|
|
47
85
|
};
|
package/package.json
CHANGED