@atlaskit/checkbox 13.3.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 +873 -858
- 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 +362 -401
- package/dist/cjs/checkbox.js +27 -2
- package/dist/cjs/internal/checkbox-icon.js +2 -0
- package/dist/cjs/internal/label-text.js +4 -0
- package/dist/cjs/internal/label.js +10 -1
- package/dist/cjs/internal/required-indicator.js +6 -2
- package/dist/es2019/checkbox.js +27 -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 +10 -1
- package/dist/es2019/internal/required-indicator.js +6 -2
- package/dist/esm/checkbox.js +28 -2
- package/dist/esm/internal/checkbox-icon.js +2 -0
- package/dist/esm/internal/label-text.js +4 -0
- package/dist/esm/internal/label.js +10 -1
- package/dist/esm/internal/required-indicator.js +6 -2
- package/dist/types/internal/label-text.d.ts +4 -1
- package/dist/types/internal/label.d.ts +5 -2
- package/dist/types/internal/required-indicator.d.ts +3 -0
- package/dist/types/types.d.ts +12 -1
- package/dist/types-ts4.5/internal/label-text.d.ts +4 -1
- package/dist/types-ts4.5/internal/label.d.ts +5 -2
- package/dist/types-ts4.5/internal/required-indicator.d.ts +3 -0
- package/dist/types-ts4.5/types.d.ts +12 -1
- package/extract-react-types/checkbox-props.tsx +2 -2
- package/package.json +93 -93
- package/report.api.md +41 -46
package/codemods/utils.tsx
CHANGED
|
@@ -1,115 +1,105 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import type {
|
|
2
|
+
API,
|
|
3
|
+
ASTPath,
|
|
4
|
+
default as core,
|
|
5
|
+
FileInfo,
|
|
6
|
+
ImportDeclaration,
|
|
7
|
+
ImportDefaultSpecifier,
|
|
8
|
+
ImportSpecifier,
|
|
9
|
+
JSXAttribute,
|
|
10
|
+
Options,
|
|
11
|
+
Program,
|
|
12
|
+
VariableDeclaration,
|
|
12
13
|
} from 'jscodeshift';
|
|
13
|
-
import { Collection } from 'jscodeshift/src/Collection';
|
|
14
|
+
import { type Collection } from 'jscodeshift/src/Collection';
|
|
14
15
|
|
|
15
16
|
export type Nullable<T> = T | null;
|
|
16
17
|
|
|
17
18
|
export function getNamedSpecifier(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
j: core.JSCodeshift,
|
|
20
|
+
source: any,
|
|
21
|
+
specifier: string,
|
|
22
|
+
importName: string,
|
|
22
23
|
) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
if (!specifiers.length) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
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;
|
|
39
34
|
}
|
|
40
35
|
|
|
41
36
|
export function getJSXAttributesByName(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
j: core.JSCodeshift,
|
|
38
|
+
element: ASTPath<any>,
|
|
39
|
+
attributeName: string,
|
|
45
40
|
): Collection<JSXAttribute> {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
+
});
|
|
55
50
|
}
|
|
56
51
|
|
|
57
|
-
export function hasImportDeclaration(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
typeof path.node.source.value === 'string' &&
|
|
67
|
-
path.node.source.value.startsWith(importPath),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
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);
|
|
71
61
|
}
|
|
72
62
|
|
|
73
63
|
export function findIdentifierAndReplaceAttribute(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
64
|
+
j: core.JSCodeshift,
|
|
65
|
+
source: ReturnType<typeof j>,
|
|
66
|
+
identifierName: string,
|
|
67
|
+
searchAttr: string,
|
|
68
|
+
replaceWithAttr: string,
|
|
79
69
|
) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
+
});
|
|
97
87
|
}
|
|
98
88
|
|
|
99
89
|
export function hasVariableAssignment(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
90
|
+
j: core.JSCodeshift,
|
|
91
|
+
source: ReturnType<typeof j>,
|
|
92
|
+
identifierName: string,
|
|
103
93
|
): Collection<VariableDeclaration> | boolean {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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;
|
|
113
103
|
}
|
|
114
104
|
|
|
115
105
|
// not replacing newlines (which \s does)
|
|
@@ -117,349 +107,320 @@ const spacesAndTabs: RegExp = /[ \t]{2,}/g;
|
|
|
117
107
|
const lineStartWithSpaces: RegExp = /^[ \t]*/gm;
|
|
118
108
|
|
|
119
109
|
function clean(value: string): string {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
+
);
|
|
127
117
|
}
|
|
128
118
|
|
|
129
119
|
export function addCommentToStartOfFile({
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
120
|
+
j,
|
|
121
|
+
base,
|
|
122
|
+
message,
|
|
133
123
|
}: {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
124
|
+
j: core.JSCodeshift;
|
|
125
|
+
base: Collection<Node>;
|
|
126
|
+
message: string;
|
|
137
127
|
}) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
128
|
+
addCommentBefore({
|
|
129
|
+
j,
|
|
130
|
+
target: base.find(j.Program),
|
|
131
|
+
message,
|
|
132
|
+
});
|
|
143
133
|
}
|
|
144
134
|
|
|
145
135
|
export function addCommentBefore({
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
136
|
+
j,
|
|
137
|
+
target,
|
|
138
|
+
message,
|
|
149
139
|
}: {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
140
|
+
j: core.JSCodeshift;
|
|
141
|
+
target: Collection<Program> | Collection<ImportDeclaration>;
|
|
142
|
+
message: string;
|
|
153
143
|
}) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
144
|
+
const content: string = ` TODO: (from codemod) ${clean(message)} `;
|
|
145
|
+
target.forEach((path: ASTPath<Program | ImportDeclaration>) => {
|
|
146
|
+
path.value.comments = path.value.comments || [];
|
|
157
147
|
|
|
158
|
-
|
|
159
|
-
(comment) => comment.value === content,
|
|
160
|
-
);
|
|
148
|
+
const exists = path.value.comments.find((comment) => comment.value === content);
|
|
161
149
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
150
|
+
// avoiding duplicates of the same comment
|
|
151
|
+
if (exists) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
166
154
|
|
|
167
|
-
|
|
168
|
-
|
|
155
|
+
path.value.comments.push(j.commentBlock(content));
|
|
156
|
+
});
|
|
169
157
|
}
|
|
170
158
|
|
|
171
159
|
export function tryCreateImport({
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
160
|
+
j,
|
|
161
|
+
base,
|
|
162
|
+
relativeToPackage,
|
|
163
|
+
packageName,
|
|
176
164
|
}: {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
165
|
+
j: core.JSCodeshift;
|
|
166
|
+
base: Collection<any>;
|
|
167
|
+
relativeToPackage: string;
|
|
168
|
+
packageName: string;
|
|
181
169
|
}) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
.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)));
|
|
195
182
|
}
|
|
196
183
|
|
|
197
184
|
export function addToImport({
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
185
|
+
j,
|
|
186
|
+
base,
|
|
187
|
+
importSpecifier,
|
|
188
|
+
packageName,
|
|
202
189
|
}: {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
190
|
+
j: core.JSCodeshift;
|
|
191
|
+
base: Collection<any>;
|
|
192
|
+
importSpecifier: ImportSpecifier | ImportDefaultSpecifier;
|
|
193
|
+
packageName: string;
|
|
207
194
|
}) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
+
});
|
|
225
212
|
}
|
|
226
213
|
|
|
227
214
|
export const createRenameFuncFor =
|
|
228
|
-
|
|
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
|
-
ids.node.name,
|
|
257
|
-
from,
|
|
258
|
-
to,
|
|
259
|
-
);
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
};
|
|
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
|
+
};
|
|
264
243
|
|
|
265
244
|
export const createRemoveFuncFor =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
+
};
|
|
283
262
|
|
|
284
263
|
export const createRenameImportFor =
|
|
285
|
-
|
|
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
|
-
base: source,
|
|
365
|
-
importSpecifier: newSpecifier,
|
|
366
|
-
packageName: newPackagePath,
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
// Remove old path
|
|
370
|
-
source
|
|
371
|
-
.find(j.ImportDeclaration)
|
|
372
|
-
.filter((path) => path.node.source.value === oldPackagePath)
|
|
373
|
-
.remove();
|
|
374
|
-
};
|
|
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
|
+
};
|
|
375
343
|
|
|
376
344
|
export const createRemoveImportsFor =
|
|
377
|
-
|
|
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
|
-
.filter((path) => path.node.source.value === packagePath)
|
|
446
|
-
.remove();
|
|
447
|
-
}
|
|
448
|
-
};
|
|
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
|
+
};
|
|
449
413
|
|
|
450
414
|
export const createTransformer =
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
) =>
|
|
455
|
-
(fileInfo: FileInfo, { jscodeshift: j }: API, options: Options) => {
|
|
456
|
-
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);
|
|
457
418
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
419
|
+
if (!hasImportDeclaration(j, source, component)) {
|
|
420
|
+
return fileInfo.source;
|
|
421
|
+
}
|
|
461
422
|
|
|
462
|
-
|
|
423
|
+
migrates.forEach((tf) => tf(j, source));
|
|
463
424
|
|
|
464
|
-
|
|
465
|
-
|
|
425
|
+
return source.toSource(options.printOptions || { quote: 'single' });
|
|
426
|
+
};
|