@atlaskit/checkbox 13.4.0 → 13.5.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 +868 -861
- package/README.md +2 -1
- package/__perf__/checkbox.tsx +54 -72
- package/__perf__/default.tsx +7 -7
- package/codemods/12.0.0-lite-mode.tsx +12 -16
- package/codemods/__tests__/12.0.0-lite-mode.tsx +105 -105
- package/codemods/migrations/remove-imports.tsx +3 -3
- package/codemods/migrations/remove-props.tsx +9 -13
- package/codemods/migrations/rename-import.tsx +10 -10
- package/codemods/migrations/rename-input-ref-to-ref.tsx +4 -4
- package/codemods/utils.tsx +360 -400
- package/dist/cjs/checkbox.js +20 -1
- package/dist/cjs/internal/checkbox-icon.js +2 -0
- package/dist/cjs/internal/label-text.js +4 -0
- package/dist/cjs/internal/label.js +5 -0
- package/dist/cjs/internal/required-indicator.js +6 -2
- package/dist/es2019/checkbox.js +21 -1
- package/dist/es2019/internal/checkbox-icon.js +2 -0
- package/dist/es2019/internal/label-text.js +4 -0
- package/dist/es2019/internal/label.js +5 -0
- package/dist/es2019/internal/required-indicator.js +6 -2
- package/dist/esm/checkbox.js +21 -1
- package/dist/esm/internal/checkbox-icon.js +2 -0
- package/dist/esm/internal/label-text.js +4 -0
- package/dist/esm/internal/label.js +5 -0
- package/dist/esm/internal/required-indicator.js +6 -2
- package/dist/types/internal/label-text.d.ts +3 -0
- package/dist/types/internal/label.d.ts +4 -1
- package/dist/types/internal/required-indicator.d.ts +3 -0
- package/dist/types-ts4.5/internal/label-text.d.ts +3 -0
- package/dist/types-ts4.5/internal/label.d.ts +4 -1
- package/dist/types-ts4.5/internal/required-indicator.d.ts +3 -0
- package/extract-react-types/checkbox-props.tsx +1 -1
- package/package.json +93 -95
- package/report.api.md +41 -46
package/codemods/utils.tsx
CHANGED
|
@@ -1,116 +1,105 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
API,
|
|
3
|
+
ASTPath,
|
|
4
|
+
default as core,
|
|
5
|
+
FileInfo,
|
|
6
|
+
ImportDeclaration,
|
|
7
|
+
ImportDefaultSpecifier,
|
|
8
|
+
ImportSpecifier,
|
|
9
|
+
JSXAttribute,
|
|
10
|
+
Options,
|
|
11
|
+
Program,
|
|
12
|
+
VariableDeclaration,
|
|
13
13
|
} from 'jscodeshift';
|
|
14
14
|
import { type Collection } from 'jscodeshift/src/Collection';
|
|
15
15
|
|
|
16
16
|
export type Nullable<T> = T | null;
|
|
17
17
|
|
|
18
18
|
export function getNamedSpecifier(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
j: core.JSCodeshift,
|
|
20
|
+
source: any,
|
|
21
|
+
specifier: string,
|
|
22
|
+
importName: string,
|
|
23
23
|
) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
if (!specifiers.length) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
return specifiers.nodes()[0]!.local!.name;
|
|
24
|
+
const specifiers = source
|
|
25
|
+
.find(j.ImportDeclaration)
|
|
26
|
+
.filter((path: ASTPath<ImportDeclaration>) => path.node.source.value === specifier)
|
|
27
|
+
.find(j.ImportSpecifier)
|
|
28
|
+
.filter((path: ASTPath<ImportSpecifier>) => path.node.imported.name === importName);
|
|
29
|
+
|
|
30
|
+
if (!specifiers.length) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return specifiers.nodes()[0]!.local!.name;
|
|
40
34
|
}
|
|
41
35
|
|
|
42
36
|
export function getJSXAttributesByName(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
j: core.JSCodeshift,
|
|
38
|
+
element: ASTPath<any>,
|
|
39
|
+
attributeName: string,
|
|
46
40
|
): Collection<JSXAttribute> {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
41
|
+
return j(element)
|
|
42
|
+
.find(j.JSXOpeningElement)
|
|
43
|
+
.find(j.JSXAttribute)
|
|
44
|
+
.filter((attribute) => {
|
|
45
|
+
const matches = j(attribute)
|
|
46
|
+
.find(j.JSXIdentifier)
|
|
47
|
+
.filter((identifier) => identifier.value.name === attributeName);
|
|
48
|
+
return Boolean(matches.length);
|
|
49
|
+
});
|
|
56
50
|
}
|
|
57
51
|
|
|
58
|
-
export function hasImportDeclaration(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
typeof path.node.source.value === 'string' &&
|
|
68
|
-
path.node.source.value.startsWith(importPath),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
return Boolean(imports.length);
|
|
52
|
+
export function hasImportDeclaration(j: core.JSCodeshift, source: any, importPath: string) {
|
|
53
|
+
const imports = source
|
|
54
|
+
.find(j.ImportDeclaration)
|
|
55
|
+
.filter(
|
|
56
|
+
(path: ASTPath<ImportDeclaration>) =>
|
|
57
|
+
typeof path.node.source.value === 'string' && path.node.source.value.startsWith(importPath),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return Boolean(imports.length);
|
|
72
61
|
}
|
|
73
62
|
|
|
74
63
|
export function findIdentifierAndReplaceAttribute(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
64
|
+
j: core.JSCodeshift,
|
|
65
|
+
source: ReturnType<typeof j>,
|
|
66
|
+
identifierName: string,
|
|
67
|
+
searchAttr: string,
|
|
68
|
+
replaceWithAttr: string,
|
|
80
69
|
) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
70
|
+
source
|
|
71
|
+
.find(j.JSXElement)
|
|
72
|
+
.find(j.JSXOpeningElement)
|
|
73
|
+
.filter((path) => {
|
|
74
|
+
return !!j(path.node)
|
|
75
|
+
.find(j.JSXIdentifier)
|
|
76
|
+
.filter((identifier) => identifier.value.name === identifierName);
|
|
77
|
+
})
|
|
78
|
+
.forEach((element) => {
|
|
79
|
+
j(element)
|
|
80
|
+
.find(j.JSXAttribute)
|
|
81
|
+
.find(j.JSXIdentifier)
|
|
82
|
+
.filter((attr) => attr.node.name === searchAttr)
|
|
83
|
+
.forEach((attribute) => {
|
|
84
|
+
j(attribute).replaceWith(j.jsxIdentifier(replaceWithAttr));
|
|
85
|
+
});
|
|
86
|
+
});
|
|
98
87
|
}
|
|
99
88
|
|
|
100
89
|
export function hasVariableAssignment(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
90
|
+
j: core.JSCodeshift,
|
|
91
|
+
source: ReturnType<typeof j>,
|
|
92
|
+
identifierName: string,
|
|
104
93
|
): Collection<VariableDeclaration> | boolean {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
94
|
+
const occurance = source.find(j.VariableDeclaration).filter((path) => {
|
|
95
|
+
return !!j(path.node)
|
|
96
|
+
.find(j.VariableDeclarator)
|
|
97
|
+
.find(j.Identifier)
|
|
98
|
+
.filter((identifier) => {
|
|
99
|
+
return identifier.node.name === identifierName;
|
|
100
|
+
}).length;
|
|
101
|
+
});
|
|
102
|
+
return !!occurance.length ? occurance : false;
|
|
114
103
|
}
|
|
115
104
|
|
|
116
105
|
// not replacing newlines (which \s does)
|
|
@@ -118,349 +107,320 @@ const spacesAndTabs: RegExp = /[ \t]{2,}/g;
|
|
|
118
107
|
const lineStartWithSpaces: RegExp = /^[ \t]*/gm;
|
|
119
108
|
|
|
120
109
|
function clean(value: string): string {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
110
|
+
return (
|
|
111
|
+
value
|
|
112
|
+
.replace(spacesAndTabs, ' ')
|
|
113
|
+
.replace(lineStartWithSpaces, '')
|
|
114
|
+
// using .trim() to clear the any newlines before the first text and after last text
|
|
115
|
+
.trim()
|
|
116
|
+
);
|
|
128
117
|
}
|
|
129
118
|
|
|
130
119
|
export function addCommentToStartOfFile({
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
120
|
+
j,
|
|
121
|
+
base,
|
|
122
|
+
message,
|
|
134
123
|
}: {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
124
|
+
j: core.JSCodeshift;
|
|
125
|
+
base: Collection<Node>;
|
|
126
|
+
message: string;
|
|
138
127
|
}) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
128
|
+
addCommentBefore({
|
|
129
|
+
j,
|
|
130
|
+
target: base.find(j.Program),
|
|
131
|
+
message,
|
|
132
|
+
});
|
|
144
133
|
}
|
|
145
134
|
|
|
146
135
|
export function addCommentBefore({
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
136
|
+
j,
|
|
137
|
+
target,
|
|
138
|
+
message,
|
|
150
139
|
}: {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
140
|
+
j: core.JSCodeshift;
|
|
141
|
+
target: Collection<Program> | Collection<ImportDeclaration>;
|
|
142
|
+
message: string;
|
|
154
143
|
}) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
144
|
+
const content: string = ` TODO: (from codemod) ${clean(message)} `;
|
|
145
|
+
target.forEach((path: ASTPath<Program | ImportDeclaration>) => {
|
|
146
|
+
path.value.comments = path.value.comments || [];
|
|
158
147
|
|
|
159
|
-
|
|
160
|
-
(comment) => comment.value === content,
|
|
161
|
-
);
|
|
148
|
+
const exists = path.value.comments.find((comment) => comment.value === content);
|
|
162
149
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
150
|
+
// avoiding duplicates of the same comment
|
|
151
|
+
if (exists) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
167
154
|
|
|
168
|
-
|
|
169
|
-
|
|
155
|
+
path.value.comments.push(j.commentBlock(content));
|
|
156
|
+
});
|
|
170
157
|
}
|
|
171
158
|
|
|
172
159
|
export function tryCreateImport({
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
160
|
+
j,
|
|
161
|
+
base,
|
|
162
|
+
relativeToPackage,
|
|
163
|
+
packageName,
|
|
177
164
|
}: {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
165
|
+
j: core.JSCodeshift;
|
|
166
|
+
base: Collection<any>;
|
|
167
|
+
relativeToPackage: string;
|
|
168
|
+
packageName: string;
|
|
182
169
|
}) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
.insertBefore(j.importDeclaration([], j.literal(packageName)));
|
|
170
|
+
const exists: boolean =
|
|
171
|
+
base.find(j.ImportDeclaration).filter((path) => path.value.source.value === packageName)
|
|
172
|
+
.length > 0;
|
|
173
|
+
|
|
174
|
+
if (exists) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
base
|
|
179
|
+
.find(j.ImportDeclaration)
|
|
180
|
+
.filter((path) => path.value.source.value === relativeToPackage)
|
|
181
|
+
.insertBefore(j.importDeclaration([], j.literal(packageName)));
|
|
196
182
|
}
|
|
197
183
|
|
|
198
184
|
export function addToImport({
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
185
|
+
j,
|
|
186
|
+
base,
|
|
187
|
+
importSpecifier,
|
|
188
|
+
packageName,
|
|
203
189
|
}: {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
190
|
+
j: core.JSCodeshift;
|
|
191
|
+
base: Collection<any>;
|
|
192
|
+
importSpecifier: ImportSpecifier | ImportDefaultSpecifier;
|
|
193
|
+
packageName: string;
|
|
208
194
|
}) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
195
|
+
base
|
|
196
|
+
.find(j.ImportDeclaration)
|
|
197
|
+
.filter((path) => path.value.source.value === packageName)
|
|
198
|
+
.replaceWith((declaration) => {
|
|
199
|
+
return j.importDeclaration(
|
|
200
|
+
[
|
|
201
|
+
// we are appending to the existing specifiers
|
|
202
|
+
// We are doing a filter hear because sometimes specifiers can be removed
|
|
203
|
+
// but they hand around in the declaration
|
|
204
|
+
...(declaration.value.specifiers || []).filter(
|
|
205
|
+
(item) => item.type === 'ImportSpecifier' && item.imported != null,
|
|
206
|
+
),
|
|
207
|
+
importSpecifier,
|
|
208
|
+
],
|
|
209
|
+
j.literal(packageName),
|
|
210
|
+
);
|
|
211
|
+
});
|
|
226
212
|
}
|
|
227
213
|
|
|
228
214
|
export const createRenameFuncFor =
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
ids.node.name,
|
|
258
|
-
from,
|
|
259
|
-
to,
|
|
260
|
-
);
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
};
|
|
215
|
+
(component: string, importName: string, from: string, to: string) =>
|
|
216
|
+
(j: core.JSCodeshift, source: Collection<Node>) => {
|
|
217
|
+
const specifier = getNamedSpecifier(j, source, component, importName);
|
|
218
|
+
|
|
219
|
+
if (!specifier) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
source.findJSXElements(specifier).forEach((element) => {
|
|
224
|
+
getJSXAttributesByName(j, element, from).forEach((attribute) => {
|
|
225
|
+
j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(to), attribute.node.value));
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
let variable = hasVariableAssignment(j, source, specifier);
|
|
230
|
+
if (variable) {
|
|
231
|
+
(variable as Collection<VariableDeclaration>)
|
|
232
|
+
.find(j.VariableDeclarator)
|
|
233
|
+
.forEach((declarator) => {
|
|
234
|
+
j(declarator)
|
|
235
|
+
.find(j.Identifier)
|
|
236
|
+
.filter((identifier) => identifier.name === 'id')
|
|
237
|
+
.forEach((ids) => {
|
|
238
|
+
findIdentifierAndReplaceAttribute(j, source, ids.node.name, from, to);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
};
|
|
265
243
|
|
|
266
244
|
export const createRemoveFuncFor =
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
245
|
+
(component: string, importName: string, prop: string, comment?: string) =>
|
|
246
|
+
(j: core.JSCodeshift, source: Collection<Node>) => {
|
|
247
|
+
const specifier = getNamedSpecifier(j, source, component, importName);
|
|
248
|
+
|
|
249
|
+
if (!specifier) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
source.findJSXElements(specifier).forEach((element) => {
|
|
254
|
+
getJSXAttributesByName(j, element, prop).forEach((attribute) => {
|
|
255
|
+
j(attribute).remove();
|
|
256
|
+
if (comment) {
|
|
257
|
+
addCommentToStartOfFile({ j, base: source, message: comment });
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
};
|
|
284
262
|
|
|
285
263
|
export const createRenameImportFor =
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
base: source,
|
|
366
|
-
importSpecifier: newSpecifier,
|
|
367
|
-
packageName: newPackagePath,
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
// Remove old path
|
|
371
|
-
source
|
|
372
|
-
.find(j.ImportDeclaration)
|
|
373
|
-
.filter((path) => path.node.source.value === oldPackagePath)
|
|
374
|
-
.remove();
|
|
375
|
-
};
|
|
264
|
+
({
|
|
265
|
+
componentName,
|
|
266
|
+
newComponentName,
|
|
267
|
+
oldPackagePath,
|
|
268
|
+
newPackagePath,
|
|
269
|
+
}: {
|
|
270
|
+
componentName: string;
|
|
271
|
+
newComponentName?: string;
|
|
272
|
+
oldPackagePath: string;
|
|
273
|
+
newPackagePath: string;
|
|
274
|
+
}) =>
|
|
275
|
+
(j: core.JSCodeshift, source: Collection<Node>) => {
|
|
276
|
+
const isUsingName: boolean =
|
|
277
|
+
source
|
|
278
|
+
.find(j.ImportDeclaration)
|
|
279
|
+
.filter((path) => path.node.source.value === oldPackagePath)
|
|
280
|
+
.find(j.ImportSpecifier)
|
|
281
|
+
.nodes()
|
|
282
|
+
.filter((specifier) => specifier.imported && specifier.imported.name === componentName)
|
|
283
|
+
.length > 0;
|
|
284
|
+
if (!isUsingName) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const existingAlias: Nullable<string> =
|
|
289
|
+
source
|
|
290
|
+
.find(j.ImportDeclaration)
|
|
291
|
+
.filter((path) => path.node.source.value === oldPackagePath)
|
|
292
|
+
.find(j.ImportSpecifier)
|
|
293
|
+
.nodes()
|
|
294
|
+
.map((specifier): Nullable<string> => {
|
|
295
|
+
if (specifier.imported && specifier.imported.name !== componentName) {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
// If aliased: return the alias
|
|
299
|
+
if (specifier.local && specifier.local.name !== componentName) {
|
|
300
|
+
return specifier.local.name;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return null;
|
|
304
|
+
})
|
|
305
|
+
.filter(Boolean)[0] || null;
|
|
306
|
+
|
|
307
|
+
// Check to see if need to create new package path
|
|
308
|
+
// Try create an import declaration just before the old import
|
|
309
|
+
tryCreateImport({
|
|
310
|
+
j,
|
|
311
|
+
base: source,
|
|
312
|
+
relativeToPackage: oldPackagePath,
|
|
313
|
+
packageName: newPackagePath,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const newSpecifier: ImportSpecifier | ImportDefaultSpecifier = (() => {
|
|
317
|
+
// If there's a new name use that
|
|
318
|
+
if (newComponentName) {
|
|
319
|
+
return j.importSpecifier(j.identifier(newComponentName), j.identifier(newComponentName));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (existingAlias) {
|
|
323
|
+
return j.importSpecifier(j.identifier(componentName), j.identifier(existingAlias));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Add specifier
|
|
327
|
+
return j.importSpecifier(j.identifier(componentName), j.identifier(componentName));
|
|
328
|
+
})();
|
|
329
|
+
|
|
330
|
+
addToImport({
|
|
331
|
+
j,
|
|
332
|
+
base: source,
|
|
333
|
+
importSpecifier: newSpecifier,
|
|
334
|
+
packageName: newPackagePath,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// Remove old path
|
|
338
|
+
source
|
|
339
|
+
.find(j.ImportDeclaration)
|
|
340
|
+
.filter((path) => path.node.source.value === oldPackagePath)
|
|
341
|
+
.remove();
|
|
342
|
+
};
|
|
376
343
|
|
|
377
344
|
export const createRemoveImportsFor =
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
.filter((path) => path.node.source.value === packagePath)
|
|
447
|
-
.remove();
|
|
448
|
-
}
|
|
449
|
-
};
|
|
345
|
+
({
|
|
346
|
+
importsToRemove,
|
|
347
|
+
packagePath,
|
|
348
|
+
comment,
|
|
349
|
+
}: {
|
|
350
|
+
importsToRemove: string[];
|
|
351
|
+
packagePath: string;
|
|
352
|
+
comment: string;
|
|
353
|
+
}) =>
|
|
354
|
+
(j: core.JSCodeshift, source: Collection<Node>) => {
|
|
355
|
+
const isUsingName: boolean =
|
|
356
|
+
source.find(j.ImportDeclaration).filter((path) => path.node.source.value === packagePath)
|
|
357
|
+
.length > 0;
|
|
358
|
+
if (!isUsingName) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const existingAlias: Nullable<string> =
|
|
363
|
+
source
|
|
364
|
+
.find(j.ImportDeclaration)
|
|
365
|
+
.filter((path) => path.node.source.value === packagePath)
|
|
366
|
+
.find(j.ImportSpecifier)
|
|
367
|
+
.nodes()
|
|
368
|
+
.map((specifier): Nullable<string> => {
|
|
369
|
+
if (!importsToRemove.includes(specifier.imported.name)) {
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
// If aliased: return the alias
|
|
373
|
+
if (specifier.local && !importsToRemove.includes(specifier.local.name)) {
|
|
374
|
+
return specifier.local.name;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return null;
|
|
378
|
+
})
|
|
379
|
+
.filter(Boolean)[0] || null;
|
|
380
|
+
|
|
381
|
+
// Remove imports
|
|
382
|
+
source
|
|
383
|
+
.find(j.ImportDeclaration)
|
|
384
|
+
.filter((path) => path.node.source.value === packagePath)
|
|
385
|
+
.find(j.ImportSpecifier)
|
|
386
|
+
.find(j.Identifier)
|
|
387
|
+
.filter((identifier) => {
|
|
388
|
+
if (
|
|
389
|
+
importsToRemove.includes(identifier.value.name) ||
|
|
390
|
+
identifier.value.name === existingAlias
|
|
391
|
+
) {
|
|
392
|
+
addCommentToStartOfFile({ j, base: source, message: comment });
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
return false;
|
|
396
|
+
})
|
|
397
|
+
.remove();
|
|
398
|
+
|
|
399
|
+
// Remove entire import if it is empty
|
|
400
|
+
const isEmptyImport =
|
|
401
|
+
source
|
|
402
|
+
.find(j.ImportDeclaration)
|
|
403
|
+
.filter((path) => path.node.source.value === packagePath)
|
|
404
|
+
.find(j.ImportSpecifier)
|
|
405
|
+
.find(j.Identifier).length === 0;
|
|
406
|
+
if (isEmptyImport) {
|
|
407
|
+
source
|
|
408
|
+
.find(j.ImportDeclaration)
|
|
409
|
+
.filter((path) => path.node.source.value === packagePath)
|
|
410
|
+
.remove();
|
|
411
|
+
}
|
|
412
|
+
};
|
|
450
413
|
|
|
451
414
|
export const createTransformer =
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
) =>
|
|
456
|
-
(fileInfo: FileInfo, { jscodeshift: j }: API, options: Options) => {
|
|
457
|
-
const source: Collection<Node> = j(fileInfo.source);
|
|
415
|
+
(component: string, migrates: { (j: core.JSCodeshift, source: Collection<Node>): void }[]) =>
|
|
416
|
+
(fileInfo: FileInfo, { jscodeshift: j }: API, options: Options) => {
|
|
417
|
+
const source: Collection<Node> = j(fileInfo.source);
|
|
458
418
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
419
|
+
if (!hasImportDeclaration(j, source, component)) {
|
|
420
|
+
return fileInfo.source;
|
|
421
|
+
}
|
|
462
422
|
|
|
463
|
-
|
|
423
|
+
migrates.forEach((tf) => tf(j, source));
|
|
464
424
|
|
|
465
|
-
|
|
466
|
-
|
|
425
|
+
return source.toSource(options.printOptions || { quote: 'single' });
|
|
426
|
+
};
|