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