@atlaskit/codemod-utils 4.1.2 → 4.2.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/CHANGELOG.md +12 -0
- package/dist/cjs/index.js +29 -31
- package/dist/cjs/utils/index.js +185 -279
- package/dist/cjs/utils/support.js +113 -209
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/utils/index.js +12 -44
- package/dist/es2019/utils/support.js +26 -68
- package/dist/es2019/version.json +1 -1
- package/dist/esm/utils/index.js +12 -44
- package/dist/esm/utils/support.js +32 -77
- package/dist/esm/version.json +1 -1
- package/package.json +6 -3
- package/report.api.md +92 -41
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { addCommentToStartOfFile, addToImport, getDefaultSpecifier, getJSXAttributesByName, getNamedSpecifier, getSafeImportName, tryCreateImport } from './support';
|
|
2
|
-
|
|
3
2
|
const createRemoveFuncFor = (component, importName, prop, predicate = () => true, comment) => (j, source) => {
|
|
4
3
|
const specifier = getNamedSpecifier(j, source, component, importName);
|
|
5
|
-
|
|
6
4
|
if (!specifier) {
|
|
7
5
|
return;
|
|
8
6
|
}
|
|
9
|
-
|
|
10
7
|
source.findJSXElements(specifier).forEach(element => {
|
|
11
8
|
if (predicate(j, element) && comment) {
|
|
12
9
|
addCommentToStartOfFile({
|
|
@@ -21,40 +18,32 @@ const createRemoveFuncFor = (component, importName, prop, predicate = () => true
|
|
|
21
18
|
}
|
|
22
19
|
});
|
|
23
20
|
};
|
|
24
|
-
|
|
25
21
|
const createRenameFuncFor = (component, from, to) => (j, source) => {
|
|
26
22
|
const defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
27
|
-
|
|
28
23
|
if (!defaultSpecifier) {
|
|
29
24
|
return;
|
|
30
25
|
}
|
|
31
|
-
|
|
32
26
|
source.findJSXElements(defaultSpecifier).forEach(element => {
|
|
33
27
|
getJSXAttributesByName(j, element, from).forEach(attribute => {
|
|
34
28
|
j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(to), attribute.node.value));
|
|
35
29
|
});
|
|
36
30
|
});
|
|
37
31
|
};
|
|
38
|
-
|
|
39
32
|
const createConvertFuncFor = (component, from, to, predicate) => (j, source) => {
|
|
40
33
|
const defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
41
|
-
|
|
42
34
|
if (!defaultSpecifier) {
|
|
43
35
|
return;
|
|
44
36
|
}
|
|
45
|
-
|
|
46
37
|
source.findJSXElements(defaultSpecifier).forEach(element => {
|
|
47
38
|
getJSXAttributesByName(j, element, from).forEach(attribute => {
|
|
48
39
|
const shouldConvert = predicate && predicate(attribute.node.value) || false;
|
|
49
40
|
const node = j.jsxAttribute(j.jsxIdentifier(to));
|
|
50
|
-
|
|
51
41
|
if (shouldConvert) {
|
|
52
42
|
j(attribute).insertBefore(node);
|
|
53
43
|
}
|
|
54
44
|
});
|
|
55
45
|
});
|
|
56
46
|
};
|
|
57
|
-
|
|
58
47
|
const replaceImportStatementFor = (pkg, convertMap) => (j, root) => {
|
|
59
48
|
root.find(j.ImportDeclaration).filter(path => path.node.source.value === pkg).forEach(path => {
|
|
60
49
|
const defaultSpecifier = (path.value.specifiers || []).filter(specifier => specifier.type === 'ImportDefaultSpecifier');
|
|
@@ -65,7 +54,6 @@ const replaceImportStatementFor = (pkg, convertMap) => (j, root) => {
|
|
|
65
54
|
j(path).replaceWith(defaultDeclarations);
|
|
66
55
|
const otherDeclarations = otherSpecifier.map(s => {
|
|
67
56
|
const localName = s.local.name;
|
|
68
|
-
|
|
69
57
|
if (convertMap[localName]) {
|
|
70
58
|
return j.importDeclaration([j.importDefaultSpecifier(j.identifier(localName))], j.literal(convertMap[localName]));
|
|
71
59
|
} else {
|
|
@@ -75,26 +63,23 @@ const replaceImportStatementFor = (pkg, convertMap) => (j, root) => {
|
|
|
75
63
|
j(path).insertAfter(otherDeclarations);
|
|
76
64
|
});
|
|
77
65
|
};
|
|
78
|
-
|
|
79
66
|
const createRenameImportFor = (component, from, to) => (j, source) => {
|
|
80
67
|
source.find(j.ImportDeclaration).filter(path => path.node.source.value === component).forEach(path => {
|
|
81
68
|
j(path).replaceWith(j.importDeclaration(path.value.specifiers, j.literal(to)));
|
|
82
69
|
});
|
|
83
70
|
};
|
|
84
|
-
|
|
85
71
|
const createTransformer = (migrates, shouldApplyTransform) => (fileInfo, {
|
|
86
72
|
jscodeshift: j
|
|
87
73
|
}, options) => {
|
|
88
|
-
const source = j(fileInfo.source);
|
|
74
|
+
const source = j(fileInfo.source);
|
|
89
75
|
|
|
76
|
+
// If shouldApplyTransform not provided then perform old behaviour
|
|
90
77
|
if (!shouldApplyTransform || shouldApplyTransform(j, source)) {
|
|
91
78
|
migrates.forEach(tf => tf(j, source));
|
|
92
79
|
return source.toSource(options.printOptions);
|
|
93
80
|
}
|
|
94
|
-
|
|
95
81
|
return fileInfo.source;
|
|
96
82
|
};
|
|
97
|
-
|
|
98
83
|
const elevateComponentToNewEntryPoint = (pkg, toPkg, innerElementName) => (j, root) => {
|
|
99
84
|
const importDeclarations = root.find(j.ImportDeclaration).filter(path => path.node.source.value === pkg);
|
|
100
85
|
const defaultSpecifier = importDeclarations.find(j.ImportDefaultSpecifier).nodes();
|
|
@@ -114,14 +99,11 @@ const elevateComponentToNewEntryPoint = (pkg, toPkg, innerElementName) => (j, ro
|
|
|
114
99
|
j(path).insertBefore(newOtherSpecifiers);
|
|
115
100
|
});
|
|
116
101
|
};
|
|
117
|
-
|
|
118
102
|
const flattenCertainChildPropsAsProp = (component, propName, childProps) => (j, source) => {
|
|
119
103
|
const defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
120
|
-
|
|
121
104
|
if (!defaultSpecifier) {
|
|
122
105
|
return;
|
|
123
106
|
}
|
|
124
|
-
|
|
125
107
|
source.findJSXElements(defaultSpecifier).forEach(element => {
|
|
126
108
|
getJSXAttributesByName(j, element, propName).forEach(attribute => {
|
|
127
109
|
j(attribute).find(j.JSXExpressionContainer).find(j.ObjectExpression).forEach(objectExpression => {
|
|
@@ -129,7 +111,6 @@ const flattenCertainChildPropsAsProp = (component, propName, childProps) => (j,
|
|
|
129
111
|
childProps.forEach(childProp => {
|
|
130
112
|
if ((property.type === 'Property' || property.type === 'ObjectProperty') && property.key.type === 'Identifier' && property.key.name === childProp) {
|
|
131
113
|
var _element$node$opening;
|
|
132
|
-
|
|
133
114
|
(_element$node$opening = element.node.openingElement.attributes) === null || _element$node$opening === void 0 ? void 0 : _element$node$opening.push(j.jsxAttribute(j.jsxIdentifier(childProp), j.jsxExpressionContainer(property.value)));
|
|
134
115
|
}
|
|
135
116
|
});
|
|
@@ -138,7 +119,6 @@ const flattenCertainChildPropsAsProp = (component, propName, childProps) => (j,
|
|
|
138
119
|
});
|
|
139
120
|
});
|
|
140
121
|
};
|
|
141
|
-
|
|
142
122
|
const createRenameJSXFunc = (packagePath, from, to, fallback = undefined) => (j, source) => {
|
|
143
123
|
const namedSpecifier = getNamedSpecifier(j, source, packagePath, from);
|
|
144
124
|
const toName = fallback ? getSafeImportName({
|
|
@@ -151,37 +131,29 @@ const createRenameJSXFunc = (packagePath, from, to, fallback = undefined) => (j,
|
|
|
151
131
|
const existingAlias = source.find(j.ImportDeclaration).filter(path => path.node.source.value === packagePath).find(j.ImportSpecifier).nodes().map(specifier => {
|
|
152
132
|
if (from !== specifier.imported.name) {
|
|
153
133
|
return null;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
134
|
+
}
|
|
135
|
+
// If aliased: return the alias
|
|
157
136
|
if (specifier.local && from !== specifier.local.name) {
|
|
158
137
|
return specifier.local.name;
|
|
159
138
|
}
|
|
160
|
-
|
|
161
139
|
return null;
|
|
162
140
|
}).filter(Boolean)[0] || null;
|
|
163
141
|
source.find(j.ImportDeclaration).filter(path => path.node.source.value === packagePath).find(j.ImportSpecifier).filter(importSpecifier => {
|
|
164
142
|
const identifier = j(importSpecifier).find(j.Identifier).get();
|
|
165
|
-
|
|
166
143
|
if (from === identifier.value.name || existingAlias === identifier.value.name) {
|
|
167
144
|
return true;
|
|
168
145
|
}
|
|
169
|
-
|
|
170
146
|
return false;
|
|
171
147
|
}).replaceWith([j.importSpecifier(j.identifier(toName), existingAlias ? j.identifier(existingAlias) : null)], j.literal(packagePath));
|
|
172
148
|
};
|
|
173
|
-
|
|
174
149
|
const createRemoveFuncAddCommentFor = (component, prop, comment) => (j, source) => {
|
|
175
150
|
const defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
176
|
-
|
|
177
151
|
if (!defaultSpecifier) {
|
|
178
152
|
return;
|
|
179
153
|
}
|
|
180
|
-
|
|
181
154
|
source.findJSXElements(defaultSpecifier).forEach(element => {
|
|
182
155
|
getJSXAttributesByName(j, element, prop).forEach(attribute => {
|
|
183
156
|
j(attribute).remove();
|
|
184
|
-
|
|
185
157
|
if (comment) {
|
|
186
158
|
addCommentToStartOfFile({
|
|
187
159
|
j,
|
|
@@ -192,14 +164,12 @@ const createRemoveFuncAddCommentFor = (component, prop, comment) => (j, source)
|
|
|
192
164
|
});
|
|
193
165
|
});
|
|
194
166
|
};
|
|
195
|
-
|
|
196
167
|
const renameNamedImportWithAliasName = (component, from, to) => (j, source) => {
|
|
197
168
|
source.find(j.ImportDeclaration).filter(path => path.node.source.value === component).find(j.ImportSpecifier).filter(path => path.node.imported.name === from).forEach(path => {
|
|
198
169
|
const localName = path.node.local.name;
|
|
199
170
|
j(path).replaceWith(j.importSpecifier(j.identifier(to), j.identifier(localName)));
|
|
200
171
|
});
|
|
201
172
|
};
|
|
202
|
-
|
|
203
173
|
const changeImportEntryPoint = (oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) => (j, root) => {
|
|
204
174
|
root.find(j.ImportDeclaration, {
|
|
205
175
|
source: {
|
|
@@ -207,43 +177,41 @@ const changeImportEntryPoint = (oldPackageName, importToConvert, newPackageName,
|
|
|
207
177
|
}
|
|
208
178
|
}).forEach(path => {
|
|
209
179
|
var _currentImportSpecifi;
|
|
210
|
-
|
|
211
180
|
const currentImportSpecifier = (path.value.specifiers || []).find(specifier => {
|
|
212
181
|
if (specifier.type === 'ImportSpecifier') {
|
|
213
182
|
return specifier.imported.name === importToConvert;
|
|
214
183
|
}
|
|
215
|
-
|
|
216
184
|
return false;
|
|
217
185
|
});
|
|
218
|
-
|
|
219
186
|
if (!currentImportSpecifier) {
|
|
220
187
|
return;
|
|
221
188
|
}
|
|
222
|
-
|
|
223
189
|
const importedSpecifierName = currentImportSpecifier.imported.name;
|
|
224
190
|
const localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
|
|
225
|
-
const newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined);
|
|
191
|
+
const newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined);
|
|
226
192
|
|
|
227
|
-
|
|
193
|
+
// check if new import exists, if not create it
|
|
194
|
+
tryCreateImport(j, root, oldPackageName, newPackageName, shouldBeTypeImport);
|
|
228
195
|
|
|
196
|
+
// remove the old import specifier, but NOT the whole package
|
|
229
197
|
root.find(j.ImportDeclaration, {
|
|
230
198
|
source: {
|
|
231
199
|
value: oldPackageName
|
|
232
200
|
}
|
|
233
|
-
}).find(j.ImportSpecifier).filter(path => path.value.imported.name === importToConvert).remove();
|
|
201
|
+
}).find(j.ImportSpecifier).filter(path => path.value.imported.name === importToConvert).remove();
|
|
234
202
|
|
|
235
|
-
|
|
203
|
+
// adds import specifier to new import
|
|
204
|
+
addToImport(j, root, newIdentifier, newPackageName);
|
|
236
205
|
|
|
206
|
+
// if there are any imports with no specifiers, remove the whole import
|
|
237
207
|
root.find(j.ImportDeclaration, {
|
|
238
208
|
source: {
|
|
239
209
|
value: oldPackageName
|
|
240
210
|
}
|
|
241
211
|
}).filter(path => {
|
|
242
212
|
var _path$value$specifier;
|
|
243
|
-
|
|
244
213
|
return !((_path$value$specifier = path.value.specifiers) !== null && _path$value$specifier !== void 0 && _path$value$specifier.length);
|
|
245
214
|
}).remove();
|
|
246
215
|
});
|
|
247
216
|
};
|
|
248
|
-
|
|
249
217
|
export { createRenameFuncFor, createConvertFuncFor, createRenameImportFor, createRemoveFuncFor, replaceImportStatementFor, elevateComponentToNewEntryPoint, createTransformer, renameNamedImportWithAliasName, flattenCertainChildPropsAsProp, createRenameJSXFunc, createRemoveFuncAddCommentFor, changeImportEntryPoint };
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
function getNamedSpecifier(j, source, specifier, importName) {
|
|
2
2
|
const specifiers = source.find(j.ImportDeclaration).filter(path => path.node.source.value === specifier).find(j.ImportSpecifier).filter(path => path.node.imported.name === importName);
|
|
3
|
-
|
|
4
3
|
if (!specifiers.length) {
|
|
5
4
|
return null;
|
|
6
5
|
}
|
|
7
|
-
|
|
8
6
|
return specifiers.nodes()[0].local.name;
|
|
9
7
|
}
|
|
10
|
-
|
|
11
8
|
const getDynamicImportName = (j, source, importPath) => {
|
|
12
9
|
const dynamicImports = source.find(j.VariableDeclarator).filter(variableDeclaratorPath => {
|
|
13
10
|
return j(variableDeclaratorPath).find(j.CallExpression).filter(callExpressionPath => {
|
|
@@ -18,48 +15,38 @@ const getDynamicImportName = (j, source, importPath) => {
|
|
|
18
15
|
return !!(isCallExpressionCalleeImportType(callee) && isCallExpressionArgumentStringLiteralType(callExpressionArguments) && isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath));
|
|
19
16
|
}).length > 0;
|
|
20
17
|
});
|
|
21
|
-
|
|
22
18
|
if (!dynamicImports.length) {
|
|
23
19
|
return null;
|
|
24
20
|
}
|
|
25
|
-
|
|
26
21
|
const {
|
|
27
22
|
id
|
|
28
23
|
} = dynamicImports.nodes()[0];
|
|
29
|
-
|
|
30
24
|
if (id.type !== 'Identifier') {
|
|
31
25
|
return null;
|
|
32
26
|
}
|
|
33
|
-
|
|
34
27
|
return id.name;
|
|
35
28
|
};
|
|
36
|
-
|
|
37
29
|
const isCallExpressionCalleeImportType = callee => {
|
|
38
30
|
return callee && callee.type === 'Import';
|
|
39
31
|
};
|
|
40
|
-
|
|
41
32
|
const isCallExpressionArgumentStringLiteralType = callExpressionArguments => {
|
|
42
33
|
return callExpressionArguments && callExpressionArguments.length && callExpressionArguments[0].type === 'StringLiteral';
|
|
43
34
|
};
|
|
44
|
-
|
|
45
35
|
const isCallExpressionArgumentValueMatches = (callExpressionArgument, j, value) => {
|
|
46
36
|
return j(callExpressionArgument).some(path => path.node.value === value);
|
|
47
37
|
};
|
|
48
|
-
|
|
49
38
|
const addDynamicImport = (j, target, name, packageEndpoint) => {
|
|
50
39
|
const node = j.variableDeclaration('const', [j.variableDeclarator(j.identifier(name), j.callExpression(j.memberExpression(j.identifier('React'), j.identifier('lazy')), [j.arrowFunctionExpression([], j.callExpression(j.import(), [j.stringLiteral(packageEndpoint)]))]))]);
|
|
51
40
|
target.insertAfter(node);
|
|
52
41
|
addCommentBefore(j, j(node), 'We have added "React.lazy" here. Feel free to change it to "lazy" or other named import depending upon how you imported.');
|
|
53
|
-
};
|
|
54
|
-
|
|
42
|
+
};
|
|
55
43
|
|
|
44
|
+
// not replacing newlines (which \s does)
|
|
56
45
|
const spacesAndTabs = /[ \t]{2,}/g;
|
|
57
46
|
const lineStartWithSpaces = /^[ \t]*/gm;
|
|
58
|
-
|
|
59
47
|
function clean(value) {
|
|
60
48
|
return value.replace(spacesAndTabs, ' ').replace(lineStartWithSpaces, '').trim();
|
|
61
49
|
}
|
|
62
|
-
|
|
63
50
|
const addCommentToStartOfFile = ({
|
|
64
51
|
j,
|
|
65
52
|
base,
|
|
@@ -67,18 +54,17 @@ const addCommentToStartOfFile = ({
|
|
|
67
54
|
}) => {
|
|
68
55
|
addCommentBefore(j, base.find(j.Program), message);
|
|
69
56
|
};
|
|
70
|
-
|
|
71
57
|
function addCommentBefore(j, target, message, commentType = 'block', messagePrefix = 'TODO: (from codemod) ') {
|
|
72
58
|
const msg = clean(messagePrefix + message);
|
|
73
59
|
const content = commentType === 'block' ? ` ${msg} ` : ` ${msg}`;
|
|
74
60
|
target.forEach(path => {
|
|
75
61
|
path.value.comments = path.value.comments || [];
|
|
76
|
-
const exists = path.value.comments.find(comment => comment.value === content);
|
|
62
|
+
const exists = path.value.comments.find(comment => comment.value === content);
|
|
77
63
|
|
|
64
|
+
// avoiding duplicates of the same comment
|
|
78
65
|
if (exists) {
|
|
79
66
|
return;
|
|
80
67
|
}
|
|
81
|
-
|
|
82
68
|
if (commentType === 'block') {
|
|
83
69
|
path.value.comments.push(j.commentBlock(content));
|
|
84
70
|
} else {
|
|
@@ -86,50 +72,40 @@ function addCommentBefore(j, target, message, commentType = 'block', messagePref
|
|
|
86
72
|
}
|
|
87
73
|
});
|
|
88
74
|
}
|
|
89
|
-
|
|
90
75
|
const getDefaultSpecifier = (j, source, specifier) => {
|
|
91
76
|
const specifiers = source.find(j.ImportDeclaration).filter(path => path.node.source.value === specifier).find(j.ImportDefaultSpecifier);
|
|
92
|
-
|
|
93
77
|
if (!specifiers.length) {
|
|
94
78
|
return null;
|
|
95
79
|
}
|
|
96
|
-
|
|
97
80
|
return specifiers.nodes()[0].local.name;
|
|
98
|
-
};
|
|
99
|
-
|
|
81
|
+
};
|
|
100
82
|
|
|
83
|
+
// @ts-ignore
|
|
101
84
|
const getJSXAttributesByName = (j, element, attributeName) => {
|
|
102
85
|
return j(element).find(j.JSXOpeningElement).at(0).find(j.JSXAttribute).filter(attribute => {
|
|
103
86
|
const matches = j(attribute).find(j.JSXIdentifier).filter(identifier => identifier.value.name === attributeName);
|
|
104
87
|
return Boolean(matches.length);
|
|
105
88
|
});
|
|
106
89
|
};
|
|
107
|
-
|
|
108
90
|
const isEmpty = string => string && string.value !== '';
|
|
109
|
-
|
|
110
91
|
const hasImportDeclaration = (j, source, importPath) => {
|
|
111
92
|
const imports = source.find(j.ImportDeclaration).filter(path => path.node.source.value === importPath);
|
|
112
93
|
return Boolean(imports.length);
|
|
113
94
|
};
|
|
114
|
-
|
|
115
95
|
const hasImportDeclarationFromAnyPackageEntrypoint = (j, source, packageName) => {
|
|
116
96
|
const imports = source.find(j.ImportDeclaration).filter(path => {
|
|
117
97
|
var _path$node, _path$node$source;
|
|
118
|
-
|
|
119
98
|
return (// @ts-ignore
|
|
120
99
|
path === null || path === void 0 ? void 0 : (_path$node = path.node) === null || _path$node === void 0 ? void 0 : (_path$node$source = _path$node.source) === null || _path$node$source === void 0 ? void 0 : _path$node$source.value.toString().startsWith(packageName)
|
|
121
100
|
);
|
|
122
101
|
});
|
|
123
102
|
return Boolean(imports.length);
|
|
124
103
|
};
|
|
125
|
-
|
|
126
104
|
const debug = component => (j, source) => {
|
|
127
105
|
const defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
128
|
-
|
|
129
106
|
if (!defaultSpecifier) {
|
|
130
107
|
return;
|
|
131
108
|
}
|
|
132
|
-
|
|
133
109
|
source.findJSXElements(defaultSpecifier).forEach(element => {
|
|
134
110
|
console.log(element); //eslint-disable-line no-console
|
|
135
111
|
});
|
|
@@ -139,7 +115,6 @@ export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
139
115
|
if (placeholderStr === resolvedStr) {
|
|
140
116
|
return true;
|
|
141
117
|
}
|
|
142
|
-
|
|
143
118
|
let value = '';
|
|
144
119
|
let offset = 0;
|
|
145
120
|
const partsPlaceholder = placeholderStr.split(' ');
|
|
@@ -151,19 +126,19 @@ export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
151
126
|
const remainingWords = partsResolved.slice(i + offset);
|
|
152
127
|
const nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]);
|
|
153
128
|
const hasNextWord = nextWordIndex !== -1;
|
|
154
|
-
|
|
155
129
|
if (hasNextWord) {
|
|
156
130
|
offset += nextWordIndex - 1;
|
|
157
131
|
}
|
|
158
|
-
|
|
159
132
|
const insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' ');
|
|
160
|
-
value += `${insert} `;
|
|
133
|
+
value += `${insert} `;
|
|
134
|
+
// Regular words
|
|
161
135
|
} else {
|
|
162
136
|
value += `${p} `;
|
|
163
137
|
}
|
|
164
138
|
});
|
|
165
139
|
return value.trimRight() === resolvedStr;
|
|
166
140
|
}
|
|
141
|
+
|
|
167
142
|
/**
|
|
168
143
|
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
169
144
|
*
|
|
@@ -175,10 +150,9 @@ export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
175
150
|
*
|
|
176
151
|
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
177
152
|
*/
|
|
178
|
-
|
|
179
153
|
export function matchesStringWithFormatSpecifier(argValue, str) {
|
|
180
|
-
const value = String(argValue);
|
|
181
|
-
|
|
154
|
+
const value = String(argValue);
|
|
155
|
+
// Check whether value contains a printf format placeholder e.g. %s, %d etc
|
|
182
156
|
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) {
|
|
183
157
|
return placeholderStringMatches(value, str);
|
|
184
158
|
} else {
|
|
@@ -186,13 +160,11 @@ export function matchesStringWithFormatSpecifier(argValue, str) {
|
|
|
186
160
|
return false;
|
|
187
161
|
}
|
|
188
162
|
}
|
|
189
|
-
|
|
190
163
|
const checkForTemplateLiteralsWithPlaceholders = (quasis, str) => {
|
|
191
164
|
const templateStrs = quasis.map(quasi => quasi.value.raw.trim()).join('.*');
|
|
192
165
|
let regex = new RegExp(templateStrs);
|
|
193
166
|
return regex.test(str);
|
|
194
167
|
};
|
|
195
|
-
|
|
196
168
|
const callExpressionArgMatchesString = (arg, str) => {
|
|
197
169
|
switch (arg.type) {
|
|
198
170
|
case 'StringLiteral':
|
|
@@ -205,14 +177,12 @@ const callExpressionArgMatchesString = (arg, str) => {
|
|
|
205
177
|
return matchesStringWithFormatSpecifier(arg.value, str);
|
|
206
178
|
}
|
|
207
179
|
}
|
|
208
|
-
|
|
209
180
|
case 'TemplateLiteral':
|
|
210
181
|
{
|
|
211
182
|
// fuzzy match template literals, skipping expressions
|
|
212
183
|
const templateStrs = arg.quasis.map(quasi => {
|
|
213
184
|
return quasi.value.raw.trim();
|
|
214
185
|
}).join(' ');
|
|
215
|
-
|
|
216
186
|
if (str.trim() === templateStrs.trim()) {
|
|
217
187
|
return true;
|
|
218
188
|
} else {
|
|
@@ -220,50 +190,43 @@ const callExpressionArgMatchesString = (arg, str) => {
|
|
|
220
190
|
return checkForTemplateLiteralsWithPlaceholders(arg.quasis, str);
|
|
221
191
|
}
|
|
222
192
|
}
|
|
223
|
-
|
|
224
193
|
case 'BinaryExpression':
|
|
225
194
|
{
|
|
226
195
|
return false;
|
|
227
196
|
}
|
|
228
|
-
|
|
229
197
|
default:
|
|
230
198
|
{
|
|
231
199
|
return false;
|
|
232
200
|
}
|
|
233
201
|
}
|
|
234
202
|
};
|
|
235
|
-
|
|
236
203
|
const testMethodVariantEach = (path, testMethods) => {
|
|
237
204
|
var _path$value, _path$value$callee, _path$value$callee$ca, _path$value2, _path$value2$callee, _path$value2$callee$c, _path$value2$callee$c2, _path$value3, _path$value3$callee, _path$value3$callee$c, _path$value3$callee$c2;
|
|
238
|
-
|
|
239
205
|
const variants = new Set(['each']);
|
|
240
|
-
return (
|
|
241
|
-
|
|
242
|
-
|
|
206
|
+
return (
|
|
207
|
+
// @ts-ignore
|
|
208
|
+
((_path$value = path.value) === null || _path$value === void 0 ? void 0 : (_path$value$callee = _path$value.callee) === null || _path$value$callee === void 0 ? void 0 : (_path$value$callee$ca = _path$value$callee.callee) === null || _path$value$callee$ca === void 0 ? void 0 : _path$value$callee$ca.type) === 'MemberExpression' &&
|
|
209
|
+
// @ts-ignore
|
|
210
|
+
testMethods.has((_path$value2 = path.value) === null || _path$value2 === void 0 ? void 0 : (_path$value2$callee = _path$value2.callee) === null || _path$value2$callee === void 0 ? void 0 : (_path$value2$callee$c = _path$value2$callee.callee) === null || _path$value2$callee$c === void 0 ? void 0 : (_path$value2$callee$c2 = _path$value2$callee$c.object) === null || _path$value2$callee$c2 === void 0 ? void 0 : _path$value2$callee$c2.name) &&
|
|
211
|
+
// @ts-ignore
|
|
243
212
|
variants.has((_path$value3 = path.value) === null || _path$value3 === void 0 ? void 0 : (_path$value3$callee = _path$value3.callee) === null || _path$value3$callee === void 0 ? void 0 : (_path$value3$callee$c = _path$value3$callee.callee) === null || _path$value3$callee$c === void 0 ? void 0 : (_path$value3$callee$c2 = _path$value3$callee$c.property) === null || _path$value3$callee$c2 === void 0 ? void 0 : _path$value3$callee$c2.name)
|
|
244
213
|
);
|
|
245
214
|
};
|
|
246
|
-
|
|
247
215
|
const hasJSXAttributesByName = (j, element, attributeName) => getJSXAttributesByName(j, element, attributeName).length > 0;
|
|
248
|
-
|
|
249
216
|
const doesIdentifierExist = (j, base, name) => {
|
|
250
217
|
return base.find(j.Identifier).filter(identifer => identifer.value.name === name).length > 0;
|
|
251
218
|
};
|
|
252
|
-
|
|
253
219
|
function removeImport(j, base, packageName) {
|
|
254
220
|
base.find(j.ImportDeclaration).filter(path => path.node.source.value === packageName).remove();
|
|
255
221
|
}
|
|
256
|
-
|
|
257
222
|
function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
|
|
258
223
|
const matches = base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName);
|
|
259
224
|
const exists = matches.length > 0;
|
|
260
|
-
|
|
261
225
|
if (exists) {
|
|
262
226
|
if (shouldBeTypeImport) {
|
|
263
227
|
// if the matched import declarations are not type imports
|
|
264
228
|
// but should be, we update them accordingly
|
|
265
229
|
const isTypeImports = matches.every(path => path.value.importKind === 'type');
|
|
266
|
-
|
|
267
230
|
if (!isTypeImports) {
|
|
268
231
|
matches.filter(path => path.value.importKind !== 'type').forEach(path => {
|
|
269
232
|
path.value.importKind = 'type';
|
|
@@ -271,40 +234,37 @@ function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeIm
|
|
|
271
234
|
return;
|
|
272
235
|
}
|
|
273
236
|
}
|
|
274
|
-
|
|
275
237
|
return;
|
|
276
|
-
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// we dynamically build up importDeclaration args, so that if shouldBeTypeImport
|
|
277
241
|
// is falsy, it is never passed explicitly as an importKind parameter (avoiding
|
|
278
242
|
// runtime exceptions during transform runs when importKind is undefined)
|
|
279
|
-
|
|
280
|
-
|
|
281
243
|
const importDeclarationArgs = [[], j.literal(packageName)];
|
|
282
|
-
|
|
283
244
|
if (shouldBeTypeImport) {
|
|
284
245
|
importDeclarationArgs.push('type');
|
|
285
246
|
}
|
|
286
|
-
|
|
287
|
-
|
|
247
|
+
base.find(j.ImportDeclaration).filter(path => path.value.source.value === relativeToPackage)
|
|
248
|
+
// we only take the first matching path, to avoid duplicate inserts
|
|
288
249
|
// if the source contains the same import declaration more than once
|
|
289
250
|
.paths()[0].insertBefore(j.importDeclaration(...importDeclarationArgs));
|
|
290
251
|
}
|
|
291
|
-
|
|
292
252
|
function addToImport(j, base, importSpecifier, packageName) {
|
|
293
253
|
base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).at(0).replaceWith(declaration => {
|
|
294
|
-
return j.importDeclaration([
|
|
254
|
+
return j.importDeclaration([
|
|
255
|
+
// we are appending to the existing specifiers
|
|
295
256
|
// We are doing a filter hear because sometimes specifiers can be removed
|
|
296
257
|
// but they hand around in the declaration
|
|
297
258
|
...(declaration.value.specifiers || []).filter(item => item.type === 'ImportSpecifier' && item.imported != null), importSpecifier], j.literal(packageName), declaration.value.importKind);
|
|
298
259
|
});
|
|
299
260
|
}
|
|
300
|
-
|
|
301
261
|
const shiftDefaultImport = (j, base, defaultName, oldPackagePath, newPackagePath) => {
|
|
302
262
|
tryCreateImport(j, base, oldPackagePath, newPackagePath);
|
|
303
|
-
addToImport(j, base, j.importDefaultSpecifier(j.identifier(defaultName)), newPackagePath);
|
|
263
|
+
addToImport(j, base, j.importDefaultSpecifier(j.identifier(defaultName)), newPackagePath);
|
|
304
264
|
|
|
265
|
+
// removing old default specifier
|
|
305
266
|
base.find(j.ImportDeclaration).filter(path => path.node.source.value === oldPackagePath).remove();
|
|
306
267
|
};
|
|
307
|
-
|
|
308
268
|
function getSafeImportName({
|
|
309
269
|
j,
|
|
310
270
|
base,
|
|
@@ -315,9 +275,7 @@ function getSafeImportName({
|
|
|
315
275
|
if (currentDefaultSpecifierName === desiredName) {
|
|
316
276
|
return desiredName;
|
|
317
277
|
}
|
|
318
|
-
|
|
319
278
|
const isUsed = doesIdentifierExist(j, base, desiredName);
|
|
320
279
|
return isUsed ? fallbackName : desiredName;
|
|
321
280
|
}
|
|
322
|
-
|
|
323
281
|
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, hasImportDeclarationFromAnyPackageEntrypoint, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, getDynamicImportName, addDynamicImport };
|
package/dist/es2019/version.json
CHANGED