@putout/plugin-putout 20.7.0 → 20.8.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/lib/check-replace-code/index.js +78 -66
- package/package.json +1 -1
|
@@ -38,79 +38,91 @@ module.exports.fix = ({mainPath}) => {
|
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
module.exports.traverse = ({push}) => ({
|
|
41
|
-
'module.exports.replace = () => __a': (
|
|
42
|
-
|
|
43
|
-
|
|
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 (
|
|
46
|
-
|
|
64
|
+
if (!template)
|
|
65
|
+
continue;
|
|
47
66
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
103
|
-
|
|
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
|
-
|
|
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