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