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