@putout/plugin-putout 20.7.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
+ }
@@ -38,79 +38,91 @@ module.exports.fix = ({mainPath}) => {
38
38
  };
39
39
 
40
40
  module.exports.traverse = ({push}) => ({
41
- 'module.exports.replace = () => __a': (path) => {
42
- if (get(path))
43
- return;
41
+ 'module.exports.replace = () => __a': createTraverseReplacer(push),
42
+ 'export const replace = () => __a': createTraverseReplacer(push),
43
+ });
44
+
45
+ function getProperties(path) {
46
+ const props = `body.properties`;
47
+
48
+ if (path.isExportNamedDeclaration())
49
+ return path.get(`declaration.declarations.0.init.${props}`);
50
+
51
+ return path.get(`right.${props}`);
52
+ }
53
+
54
+ const createTraverseReplacer = (push) => (path) => {
55
+ if (get(path))
56
+ return;
57
+
58
+ if (hasMatch(path))
59
+ return;
60
+
61
+ for (const propertyPath of getProperties(path)) {
62
+ const template = extractValue(propertyPath);
44
63
 
45
- if (hasMatch(path))
46
- return;
64
+ if (!template)
65
+ continue;
47
66
 
48
- for (const propertyPath of path.get('right.body.properties')) {
49
- const template = extractValue(propertyPath);
50
-
51
- if (!template)
52
- continue;
53
-
54
- const [parseError, key] = parseKey(propertyPath);
55
-
56
- if (parseError) {
57
- push({
58
- error: parseError,
59
- mainPath: path,
60
- path: propertyPath,
61
- });
62
-
63
- return;
64
- }
65
-
66
- const [generateError, keyCode] = generateCode(path, key);
67
-
68
- if (generateError) {
69
- push({
70
- error: generateError,
71
- mainPath: path,
72
- path: propertyPath,
73
- });
74
-
75
- return;
76
- }
77
-
78
- const [transformError, result] = tryCatch(putout, keyCode, {
79
- printer: 'putout',
80
- fix: true,
81
- isTS: true,
82
- plugins: [
83
- ['evaluate', {
84
- report: noop,
85
- replace: () => ({
86
- [key]: template,
87
- }),
88
- }],
89
- ],
67
+ const [parseError, key] = parseKey(propertyPath);
68
+
69
+ if (parseError) {
70
+ push({
71
+ error: parseError,
72
+ mainPath: path,
73
+ path: propertyPath,
90
74
  });
91
75
 
92
- if (transformError) {
93
- push({
94
- error: transformError,
95
- mainPath: path,
96
- path: propertyPath,
97
- });
98
-
99
- return;
100
- }
76
+ return;
77
+ }
78
+
79
+ const [generateError, keyCode] = generateCode(path, key);
80
+
81
+ if (generateError) {
82
+ push({
83
+ error: generateError,
84
+ mainPath: path,
85
+ path: propertyPath,
86
+ });
101
87
 
102
- const code = result.code.slice(0, -1);
103
- const [error, is] = tryCatch(compare, rmSemi(code), template);
88
+ return;
89
+ }
90
+
91
+ const [transformError, result] = tryCatch(putout, keyCode, {
92
+ printer: 'putout',
93
+ fix: true,
94
+ isTS: true,
95
+ plugins: [
96
+ ['evaluate', {
97
+ report: noop,
98
+ replace: () => ({
99
+ [key]: template,
100
+ }),
101
+ }],
102
+ ],
103
+ });
104
+
105
+ if (transformError) {
106
+ push({
107
+ error: transformError,
108
+ mainPath: path,
109
+ path: propertyPath,
110
+ });
104
111
 
105
- if (error || !is)
106
- push({
107
- code,
108
- mainPath: path,
109
- path: propertyPath,
110
- });
112
+ return;
111
113
  }
112
- },
113
- });
114
+
115
+ const code = result.code.slice(0, -1);
116
+ const [error, is] = tryCatch(compare, rmSemi(code), template);
117
+
118
+ if (error || !is)
119
+ push({
120
+ code,
121
+ mainPath: path,
122
+ path: propertyPath,
123
+ });
124
+ }
125
+ };
114
126
 
115
127
  function parseKey(propertyPath) {
116
128
  const keyPath = propertyPath.get('key');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "20.7.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
- }