@putout/plugin-putout 20.8.0 → 20.9.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.
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ const {parse, print, transform} = require('putout');
4
+ const tryCatch = require('try-catch');
5
+ const pluginGenerate = require('./plugin-generate');
6
+
7
+ module.exports = (rootPath, source) => {
8
+ const [parseError, ast] = tryCatch(parse, source, {
9
+ isTS: true,
10
+ });
11
+
12
+ if (parseError)
13
+ return [parseError];
14
+
15
+ const getVar = createVarStore(rootPath);
16
+
17
+ transform(ast, source, {
18
+ rules: {
19
+ 'generate': ['on', {
20
+ getVar,
21
+ }],
22
+ },
23
+ plugins: [
24
+ ['generate', pluginGenerate],
25
+ ]
26
+ });
27
+
28
+
29
+ const code = print(ast);
30
+
31
+ return [null, code];
32
+ };
33
+
34
+ function createVarStore(path) {
35
+ const store = {};
36
+
37
+ return (name) => {
38
+ if (store[name])
39
+ return store[name];
40
+
41
+ store[name] = path.scope.generateUid();
42
+
43
+ return store[name];
44
+ };
45
+ }
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+
3
+ const {operator, types} = require('putout');
4
+ const {
5
+ ObjectPattern,
6
+ BlockStatement,
7
+ Identifier,
8
+ ArrayPattern,
9
+ ObjectExpression,
10
+ } = types;
11
+ const {replaceWith} = operator;
12
+ const noop = () => {};
13
+
14
+ module.exports.report = noop;
15
+
16
+ module.exports.include = () => [
17
+ 'Identifier',
18
+ 'StringLiteral',
19
+ ];
20
+
21
+ module.exports.fix = (path, {options}) => {
22
+ const {getVar} = options;
23
+ const {node} = path;
24
+
25
+ const {value, name} = node;
26
+
27
+ if (path.isStringLiteral() && /^__[a-z]$/.test(value)) {
28
+ path.node.value = getVar(name);
29
+ return;
30
+ }
31
+
32
+ if (/^__identifier__[a-z]$/.test(name)) {
33
+ path.node.name = getVar(name);
34
+ return;
35
+ }
36
+
37
+ if (/^__[a-z]$/.test(name)) {
38
+ path.node.name = getVar(name);
39
+ return;
40
+ }
41
+
42
+ if (/__args/.test(name)) {
43
+ path.node.name = getVar(name);
44
+ return;
45
+ }
46
+
47
+ if (name === '__array') {
48
+ if (path.parentPath.isCallExpression())
49
+ return replaceWith(path, ArrayPattern([]));
50
+
51
+ if (path.parentPath.isFunction())
52
+ return replaceWith(path, ArrayPattern([]));
53
+ }
54
+
55
+ if (name === '__object')
56
+ return objectify(path);
57
+
58
+ if (name === '__body') {
59
+ if (path.parentPath.isClassProperty()) {
60
+ const key = Identifier(getVar());
61
+
62
+ replaceWith(path, key);
63
+
64
+ return;
65
+ }
66
+
67
+ replaceWith(path, BlockStatement([]));
68
+ }
69
+ };
70
+
71
+ function objectify(path) {
72
+ const {parentPath} = path;
73
+ const isAssign = parentPath.isAssignmentExpression();
74
+ const isVar = parentPath.isVariableDeclarator();
75
+
76
+ if (path.parentPath.isExportDeclaration())
77
+ return replaceWith(path, ObjectExpression([]));
78
+
79
+ if (path.parentPath.isCallExpression())
80
+ return replaceWith(path, ObjectExpression([]));
81
+
82
+ if (isAssign && parentPath.get('right') === path)
83
+ return replaceWith(path, ObjectExpression([]));
84
+
85
+ if (isVar && parentPath.get('id') === path)
86
+ return replaceWith(path, ObjectPattern([]));
87
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "20.8.0",
3
+ "version": "20.9.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",
@@ -1,120 +0,0 @@
1
- 'use strict';
2
-
3
- const putout = require('putout');
4
- const tryCatch = require('try-catch');
5
-
6
- const noop = () => {};
7
- const {types, operator} = putout;
8
-
9
- const {
10
- ArrayPattern,
11
- BlockStatement,
12
- ObjectExpression,
13
- ObjectPattern,
14
- Identifier,
15
- } = types;
16
-
17
- const {replaceWith} = operator;
18
-
19
- module.exports = (rootPath, key) => {
20
- const getVar = createVarStore(rootPath);
21
-
22
- const [transformError, result] = tryCatch(putout, key, {
23
- printer: 'putout',
24
- fix: true,
25
- isTS: true,
26
- plugins: [
27
- ['generate', {
28
- report: noop,
29
- include: () => [
30
- 'Identifier',
31
- 'StringLiteral',
32
- ],
33
- fix: (path) => {
34
- const {node} = path;
35
-
36
- const {value, name} = node;
37
-
38
- if (path.isStringLiteral() && /^__[a-z]$/.test(value)) {
39
- path.node.value = getVar(name);
40
- return;
41
- }
42
-
43
- if (/^__identifier__[a-z]$/.test(name)) {
44
- path.node.name = getVar(name);
45
- return;
46
- }
47
-
48
- if (/^__[a-z]$/.test(name)) {
49
- path.node.name = getVar(name);
50
- return;
51
- }
52
-
53
- if (/__args/.test(name)) {
54
- path.node.name = getVar(name);
55
- return;
56
- }
57
-
58
- if (name === '__array') {
59
- if (path.parentPath.isCallExpression())
60
- return replaceWith(path, ArrayPattern([]));
61
-
62
- if (path.parentPath.isFunction())
63
- return replaceWith(path, ArrayPattern([]));
64
- }
65
-
66
- if (name === '__object')
67
- return objectify(path);
68
-
69
- if (name === '__body') {
70
- if (path.parentPath.isClassProperty()) {
71
- const key = Identifier(getVar());
72
-
73
- replaceWith(path, key);
74
-
75
- return;
76
- }
77
-
78
- replaceWith(path, BlockStatement([]));
79
- }
80
- },
81
- }],
82
- ],
83
- });
84
-
85
- return [
86
- transformError,
87
- result?.code,
88
- ];
89
- };
90
-
91
- function createVarStore(path) {
92
- const store = {};
93
-
94
- return (name) => {
95
- if (store[name])
96
- return store[name];
97
-
98
- store[name] = path.scope.generateUid();
99
-
100
- return store[name];
101
- };
102
- }
103
-
104
- function objectify(path) {
105
- const {parentPath} = path;
106
- const isAssign = parentPath.isAssignmentExpression();
107
- const isVar = parentPath.isVariableDeclarator();
108
-
109
- if (path.parentPath.isExportDeclaration())
110
- return replaceWith(path, ObjectExpression([]));
111
-
112
- if (path.parentPath.isCallExpression())
113
- return replaceWith(path, ObjectExpression([]));
114
-
115
- if (isAssign && parentPath.get('right') === path)
116
- return replaceWith(path, ObjectExpression([]));
117
-
118
- if (isVar && parentPath.get('id') === path)
119
- return replaceWith(path, ObjectPattern([]));
120
- }