@atlaskit/button 17.17.2 → 17.17.3

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 (48) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/__perf__/button.tsx +6 -6
  3. package/__perf__/custom.tsx +6 -6
  4. package/__perf__/customised.tsx +1 -3
  5. package/__perf__/utils/example-runner.tsx +32 -36
  6. package/__perf__/utils/interaction-tasks.tsx +65 -86
  7. package/codemods/15.0.0-lite-mode.tsx +32 -36
  8. package/codemods/15.1.1-data-testid.tsx +136 -142
  9. package/codemods/__tests__/15.0.0-lite-mode/optimistic.tsx +121 -121
  10. package/codemods/__tests__/15.0.0-lite-mode/safe.tsx +54 -54
  11. package/codemods/__tests__/15.0.0-lite-mode/shared.tsx +96 -97
  12. package/codemods/__tests__/15.1.1-data-testid/rename-data-testid.tsx +32 -32
  13. package/codemods/__tests__/_framework.tsx +32 -32
  14. package/codemods/helpers/15.0.0-runner.tsx +135 -137
  15. package/codemods/helpers/helpers-generic.tsx +524 -556
  16. package/codemods/optimistic-15.0.0-lite-mode.tsx +208 -231
  17. package/dist/cjs/old-button/shared/button-base.js +1 -1
  18. package/dist/es2019/new-button/variants/default/link.js +2 -2
  19. package/dist/es2019/new-button/variants/icon/link.js +3 -3
  20. package/dist/es2019/old-button/shared/button-base.js +1 -1
  21. package/dist/esm/new-button/variants/default/link.js +2 -2
  22. package/dist/esm/new-button/variants/icon/link.js +3 -3
  23. package/dist/esm/old-button/shared/button-base.js +1 -1
  24. package/dist/types/entry-points/loading-button.d.ts +1 -1
  25. package/dist/types/entry-points/new.d.ts +3 -3
  26. package/dist/types/entry-points/types.d.ts +2 -2
  27. package/dist/types/index.d.ts +3 -3
  28. package/dist/types/new.d.ts +3 -3
  29. package/dist/types-ts4.5/entry-points/loading-button.d.ts +1 -1
  30. package/dist/types-ts4.5/entry-points/new.d.ts +3 -3
  31. package/dist/types-ts4.5/entry-points/types.d.ts +2 -2
  32. package/dist/types-ts4.5/index.d.ts +3 -3
  33. package/dist/types-ts4.5/new.d.ts +3 -3
  34. package/extract-react-types/custom-theme-button-props.tsx +2 -4
  35. package/extract-react-types/legacy-button/custom-theme-button-props.tsx +2 -4
  36. package/extract-react-types/legacy-button/loading-button-props.tsx +1 -1
  37. package/extract-react-types/legacy-button/shared-props.tsx +1 -1
  38. package/extract-react-types/loading-button-props.tsx +1 -1
  39. package/extract-react-types/new-button/containers/split-button/split-button-props.tsx +1 -1
  40. package/extract-react-types/new-button/variants/default/button-props.tsx +1 -1
  41. package/extract-react-types/new-button/variants/default/common-default-button-props.tsx +4 -4
  42. package/extract-react-types/new-button/variants/default/link-button-props.tsx +4 -4
  43. package/extract-react-types/new-button/variants/icon-button/common-icon-button-props.tsx +4 -4
  44. package/extract-react-types/new-button/variants/icon-button/icon-button-props.tsx +1 -1
  45. package/extract-react-types/new-button/variants/icon-button/link-icon-button-props.tsx +2 -4
  46. package/extract-react-types/shared-props.tsx +1 -1
  47. package/package.json +2 -2
  48. package/report.api.md +78 -96
@@ -1,410 +1,401 @@
1
1
  import { type NodePath } from 'ast-types/lib/node-path';
2
2
  import type {
3
- ASTPath,
4
- default as core,
5
- ImportDeclaration,
6
- ImportDefaultSpecifier,
7
- ImportSpecifier,
8
- JSXAttribute,
9
- JSXElement,
10
- Node,
11
- Program,
3
+ ASTPath,
4
+ default as core,
5
+ ImportDeclaration,
6
+ ImportDefaultSpecifier,
7
+ ImportSpecifier,
8
+ JSXAttribute,
9
+ JSXElement,
10
+ Node,
11
+ Program,
12
12
  } from 'jscodeshift';
13
13
  import { type Collection } from 'jscodeshift/src/Collection';
14
14
 
15
15
  export type Nullable<T> = T | null;
16
16
 
17
17
  export function hasImportDeclaration(
18
- j: core.JSCodeshift,
19
- source: string,
20
- importPath: string,
18
+ j: core.JSCodeshift,
19
+ source: string,
20
+ importPath: string,
21
21
  ): boolean {
22
- return (
23
- j(source)
24
- .find(j.ImportDeclaration)
25
- .filter(
26
- (path: ASTPath<ImportDeclaration>) =>
27
- path.node.source.value === importPath,
28
- ).length > 0
29
- );
22
+ return (
23
+ j(source)
24
+ .find(j.ImportDeclaration)
25
+ .filter((path: ASTPath<ImportDeclaration>) => path.node.source.value === importPath).length >
26
+ 0
27
+ );
30
28
  }
31
29
 
32
30
  export function getDefaultSpecifierName({
33
- j,
34
- base,
35
- packageName,
31
+ j,
32
+ base,
33
+ packageName,
36
34
  }: {
37
- j: core.JSCodeshift;
38
- base: Collection<any>;
39
- packageName: string;
35
+ j: core.JSCodeshift;
36
+ base: Collection<any>;
37
+ packageName: string;
40
38
  }): Nullable<string> {
41
- const specifiers = base
42
- .find(j.ImportDeclaration)
43
- .filter((path) => path.node.source.value === packageName)
44
- .find(j.ImportDefaultSpecifier);
45
-
46
- if (!specifiers.length) {
47
- return null;
48
- }
49
- return specifiers.nodes()[0]!.local!.name;
39
+ const specifiers = base
40
+ .find(j.ImportDeclaration)
41
+ .filter((path) => path.node.source.value === packageName)
42
+ .find(j.ImportDefaultSpecifier);
43
+
44
+ if (!specifiers.length) {
45
+ return null;
46
+ }
47
+ return specifiers.nodes()[0]!.local!.name;
50
48
  }
51
49
 
52
50
  export function getJSXAttributesByName({
53
- j,
54
- element,
55
- attributeName,
51
+ j,
52
+ element,
53
+ attributeName,
56
54
  }: {
57
- j: core.JSCodeshift;
58
- element: JSXElement;
59
- attributeName: string;
55
+ j: core.JSCodeshift;
56
+ element: JSXElement;
57
+ attributeName: string;
60
58
  }): Collection<JSXAttribute> {
61
- return j(element)
62
- .find(j.JSXOpeningElement)
63
- .find(j.JSXAttribute)
64
- .filter((attribute) => {
65
- const matches = j(attribute)
66
- // This will find identifiers on nested jsx elements
67
- // so we are going to do a filter to ensure we are only
68
- // going one level deep
69
- .find(j.JSXIdentifier)
70
- .filter((identifer) => {
71
- j(identifer).closest(j.JSXOpeningElement);
72
- // Checking we are on the same level as the jsx element
73
- const closest = j(identifer).closest(j.JSXOpeningElement).nodes()[0];
74
-
75
- if (!closest) {
76
- return false;
77
- }
78
- return (
79
- closest.name.type === 'JSXIdentifier' &&
80
- element.openingElement.name.type === 'JSXIdentifier' &&
81
- element.openingElement.name.name === closest.name.name
82
- );
83
- })
84
- .filter((identifier) => identifier.value.name === attributeName);
85
- return Boolean(matches.length);
86
- });
59
+ return j(element)
60
+ .find(j.JSXOpeningElement)
61
+ .find(j.JSXAttribute)
62
+ .filter((attribute) => {
63
+ const matches = j(attribute)
64
+ // This will find identifiers on nested jsx elements
65
+ // so we are going to do a filter to ensure we are only
66
+ // going one level deep
67
+ .find(j.JSXIdentifier)
68
+ .filter((identifer) => {
69
+ j(identifer).closest(j.JSXOpeningElement);
70
+ // Checking we are on the same level as the jsx element
71
+ const closest = j(identifer).closest(j.JSXOpeningElement).nodes()[0];
72
+
73
+ if (!closest) {
74
+ return false;
75
+ }
76
+ return (
77
+ closest.name.type === 'JSXIdentifier' &&
78
+ element.openingElement.name.type === 'JSXIdentifier' &&
79
+ element.openingElement.name.name === closest.name.name
80
+ );
81
+ })
82
+ .filter((identifier) => identifier.value.name === attributeName);
83
+ return Boolean(matches.length);
84
+ });
87
85
  }
88
86
 
89
87
  export function hasJSXAttributesByName({
90
- j,
91
- element,
92
- attributeName,
88
+ j,
89
+ element,
90
+ attributeName,
93
91
  }: {
94
- j: core.JSCodeshift;
95
- element: JSXElement;
96
- attributeName: string;
92
+ j: core.JSCodeshift;
93
+ element: JSXElement;
94
+ attributeName: string;
97
95
  }): boolean {
98
- return getJSXAttributesByName({ j, element, attributeName }).length > 0;
96
+ return getJSXAttributesByName({ j, element, attributeName }).length > 0;
99
97
  }
100
98
 
101
99
  export function removeImport({
102
- j,
103
- base,
104
- packageName,
100
+ j,
101
+ base,
102
+ packageName,
105
103
  }: {
106
- j: core.JSCodeshift;
107
- base: Collection<any>;
108
- packageName: string;
104
+ j: core.JSCodeshift;
105
+ base: Collection<any>;
106
+ packageName: string;
109
107
  }) {
110
- base
111
- .find(j.ImportDeclaration)
112
- .filter((path) => path.node.source.value === packageName)
113
- .remove();
108
+ base
109
+ .find(j.ImportDeclaration)
110
+ .filter((path) => path.node.source.value === packageName)
111
+ .remove();
114
112
  }
115
113
 
116
114
  export function tryCreateImport({
117
- j,
118
- base,
119
- relativeToPackage,
120
- packageName,
115
+ j,
116
+ base,
117
+ relativeToPackage,
118
+ packageName,
121
119
  }: {
122
- j: core.JSCodeshift;
123
- base: Collection<any>;
124
- relativeToPackage: string;
125
- packageName: string;
120
+ j: core.JSCodeshift;
121
+ base: Collection<any>;
122
+ relativeToPackage: string;
123
+ packageName: string;
126
124
  }) {
127
- const exists: boolean =
128
- base
129
- .find(j.ImportDeclaration)
130
- .filter((path) => path.value.source.value === packageName).length > 0;
131
-
132
- if (exists) {
133
- return;
134
- }
135
-
136
- base
137
- .find(j.ImportDeclaration)
138
- .filter((path) => path.value.source.value === relativeToPackage)
139
- .insertBefore(j.importDeclaration([], j.literal(packageName)));
125
+ const exists: boolean =
126
+ base.find(j.ImportDeclaration).filter((path) => path.value.source.value === packageName)
127
+ .length > 0;
128
+
129
+ if (exists) {
130
+ return;
131
+ }
132
+
133
+ base
134
+ .find(j.ImportDeclaration)
135
+ .filter((path) => path.value.source.value === relativeToPackage)
136
+ .insertBefore(j.importDeclaration([], j.literal(packageName)));
140
137
  }
141
138
 
142
139
  export function addToImport({
143
- j,
144
- base,
145
- importSpecifier,
146
- packageName,
140
+ j,
141
+ base,
142
+ importSpecifier,
143
+ packageName,
147
144
  }: {
148
- j: core.JSCodeshift;
149
- base: Collection<any>;
150
- importSpecifier: ImportSpecifier | ImportDefaultSpecifier;
151
- packageName: string;
145
+ j: core.JSCodeshift;
146
+ base: Collection<any>;
147
+ importSpecifier: ImportSpecifier | ImportDefaultSpecifier;
148
+ packageName: string;
152
149
  }) {
153
- base
154
- .find(j.ImportDeclaration)
155
- .filter((path) => path.value.source.value === packageName)
156
- .replaceWith((declaration) => {
157
- return j.importDeclaration(
158
- [
159
- // we are appending to the existing specifiers
160
- // We are doing a filter hear because sometimes specifiers can be removed
161
- // but they hand around in the declaration
162
- ...(declaration.value.specifiers || []).filter(
163
- (item) => item.type === 'ImportSpecifier' && item.imported != null,
164
- ),
165
- importSpecifier,
166
- ],
167
- j.literal(packageName),
168
- );
169
- });
150
+ base
151
+ .find(j.ImportDeclaration)
152
+ .filter((path) => path.value.source.value === packageName)
153
+ .replaceWith((declaration) => {
154
+ return j.importDeclaration(
155
+ [
156
+ // we are appending to the existing specifiers
157
+ // We are doing a filter hear because sometimes specifiers can be removed
158
+ // but they hand around in the declaration
159
+ ...(declaration.value.specifiers || []).filter(
160
+ (item) => item.type === 'ImportSpecifier' && item.imported != null,
161
+ ),
162
+ importSpecifier,
163
+ ],
164
+ j.literal(packageName),
165
+ );
166
+ });
170
167
  }
171
168
 
172
169
  export function doesIdentifierExist({
173
- j,
174
- base,
175
- name,
170
+ j,
171
+ base,
172
+ name,
176
173
  }: {
177
- j: core.JSCodeshift;
178
- base: Collection<any>;
179
- name: string;
174
+ j: core.JSCodeshift;
175
+ base: Collection<any>;
176
+ name: string;
180
177
  }): boolean {
181
- return (
182
- base.find(j.Identifier).filter((identifer) => identifer.value.name === name)
183
- .length > 0
184
- );
178
+ return base.find(j.Identifier).filter((identifer) => identifer.value.name === name).length > 0;
185
179
  }
186
180
 
187
181
  export function isUsingSupportedSpread({
188
- j,
189
- base,
190
- element,
182
+ j,
183
+ base,
184
+ element,
191
185
  }: {
192
- j: core.JSCodeshift;
193
- base: Collection<any>;
194
- element: NodePath<JSXElement, JSXElement>;
186
+ j: core.JSCodeshift;
187
+ base: Collection<any>;
188
+ element: NodePath<JSXElement, JSXElement>;
195
189
  }): boolean {
196
- const isUsingSpread: boolean =
197
- j(element).find(j.JSXSpreadAttribute).length > 0;
198
-
199
- if (!isUsingSpread) {
200
- return true;
201
- }
202
-
203
- return (
204
- j(element)
205
- .find(j.JSXSpreadAttribute)
206
- .filter((spread) => {
207
- const argument = spread.value.argument;
208
- // in place expression is supported
209
- if (argument.type === 'ObjectExpression') {
210
- return true;
211
- }
212
-
213
- // Supporting identifiers that point to an a local object expression
214
- if (argument.type === 'Identifier') {
215
- return (
216
- base.find(j.VariableDeclarator).filter((declarator): boolean => {
217
- return Boolean(
218
- declarator.value.id.type === 'Identifier' &&
219
- declarator.value.init &&
220
- declarator.value.init.type === 'ObjectExpression',
221
- );
222
- }).length > 0
223
- );
224
- }
225
-
226
- // We don't support anything else
227
- return false;
228
- }).length > 0
229
- );
190
+ const isUsingSpread: boolean = j(element).find(j.JSXSpreadAttribute).length > 0;
191
+
192
+ if (!isUsingSpread) {
193
+ return true;
194
+ }
195
+
196
+ return (
197
+ j(element)
198
+ .find(j.JSXSpreadAttribute)
199
+ .filter((spread) => {
200
+ const argument = spread.value.argument;
201
+ // in place expression is supported
202
+ if (argument.type === 'ObjectExpression') {
203
+ return true;
204
+ }
205
+
206
+ // Supporting identifiers that point to an a local object expression
207
+ if (argument.type === 'Identifier') {
208
+ return (
209
+ base.find(j.VariableDeclarator).filter((declarator): boolean => {
210
+ return Boolean(
211
+ declarator.value.id.type === 'Identifier' &&
212
+ declarator.value.init &&
213
+ declarator.value.init.type === 'ObjectExpression',
214
+ );
215
+ }).length > 0
216
+ );
217
+ }
218
+
219
+ // We don't support anything else
220
+ return false;
221
+ }).length > 0
222
+ );
230
223
  }
231
224
 
232
225
  export function isOnlyUsingNameForJSX({
233
- j,
234
- base,
235
- name,
226
+ j,
227
+ base,
228
+ name,
236
229
  }: {
237
- j: core.JSCodeshift;
238
- base: Collection<any>;
239
- name: string;
230
+ j: core.JSCodeshift;
231
+ base: Collection<any>;
232
+ name: string;
240
233
  }): boolean {
241
- const jsxIdentifierCount: number = base
242
- .find(j.JSXIdentifier)
243
- .filter((identifier) => identifier.value.name === name).length;
244
-
245
- // Not used in JSX at all
246
- if (jsxIdentifierCount === 0) {
247
- return false;
248
- }
249
-
250
- const nonJSXIdentifierCount: number = base
251
- .find(j.Identifier)
252
- .filter((identifier) => {
253
- if (identifier.value.name !== name) {
254
- return false;
255
- }
256
-
257
- // @ts-ignore
258
- if (identifier.value.type === 'JSXIdentifier') {
259
- return false;
260
- }
261
-
262
- // Excluding exports
263
- if (j(identifier).closest(j.ImportDefaultSpecifier).length) {
264
- return false;
265
- }
266
- if (j(identifier).closest(j.ImportSpecifier).length) {
267
- return false;
268
- }
269
-
270
- return true;
271
- }).length;
272
-
273
- if (nonJSXIdentifierCount > 0) {
274
- return false;
275
- }
276
-
277
- return true;
234
+ const jsxIdentifierCount: number = base
235
+ .find(j.JSXIdentifier)
236
+ .filter((identifier) => identifier.value.name === name).length;
237
+
238
+ // Not used in JSX at all
239
+ if (jsxIdentifierCount === 0) {
240
+ return false;
241
+ }
242
+
243
+ const nonJSXIdentifierCount: number = base.find(j.Identifier).filter((identifier) => {
244
+ if (identifier.value.name !== name) {
245
+ return false;
246
+ }
247
+
248
+ // @ts-ignore
249
+ if (identifier.value.type === 'JSXIdentifier') {
250
+ return false;
251
+ }
252
+
253
+ // Excluding exports
254
+ if (j(identifier).closest(j.ImportDefaultSpecifier).length) {
255
+ return false;
256
+ }
257
+ if (j(identifier).closest(j.ImportSpecifier).length) {
258
+ return false;
259
+ }
260
+
261
+ return true;
262
+ }).length;
263
+
264
+ if (nonJSXIdentifierCount > 0) {
265
+ return false;
266
+ }
267
+
268
+ return true;
278
269
  }
279
270
 
280
271
  export function getSafeImportName({
281
- j,
282
- base,
283
- currentDefaultSpecifierName,
284
- desiredName,
285
- fallbackName,
272
+ j,
273
+ base,
274
+ currentDefaultSpecifierName,
275
+ desiredName,
276
+ fallbackName,
286
277
  }: {
287
- j: core.JSCodeshift;
288
- base: Collection<any>;
289
- currentDefaultSpecifierName: string;
290
- desiredName: string;
291
- fallbackName: string;
278
+ j: core.JSCodeshift;
279
+ base: Collection<any>;
280
+ currentDefaultSpecifierName: string;
281
+ desiredName: string;
282
+ fallbackName: string;
292
283
  }) {
293
- if (currentDefaultSpecifierName === desiredName) {
294
- return desiredName;
295
- }
284
+ if (currentDefaultSpecifierName === desiredName) {
285
+ return desiredName;
286
+ }
296
287
 
297
- const isUsed: boolean = doesIdentifierExist({ j, base, name: desiredName });
288
+ const isUsed: boolean = doesIdentifierExist({ j, base, name: desiredName });
298
289
 
299
- return isUsed ? fallbackName : desiredName;
290
+ return isUsed ? fallbackName : desiredName;
300
291
  }
301
292
 
302
293
  export function isUsingThroughSpread({
303
- j,
304
- base,
305
- element,
306
- propName,
294
+ j,
295
+ base,
296
+ element,
297
+ propName,
307
298
  }: {
308
- j: core.JSCodeshift;
309
- base: Collection<any>;
310
- element: NodePath<JSXElement, JSXElement>;
311
- propName: string;
299
+ j: core.JSCodeshift;
300
+ base: Collection<any>;
301
+ element: NodePath<JSXElement, JSXElement>;
302
+ propName: string;
312
303
  }): boolean {
313
- if (!isUsingSupportedSpread({ j, base, element })) {
314
- return false;
315
- }
316
-
317
- const isUsedThroughExpression: boolean =
318
- j(element)
319
- .find(j.JSXSpreadAttribute)
320
- .find(j.ObjectExpression)
321
- .filter((item) => {
322
- const match: boolean =
323
- item.value.properties.filter(
324
- (property) =>
325
- property.type === 'ObjectProperty' &&
326
- property.key.type === 'Identifier' &&
327
- property.key.name === propName,
328
- ).length > 0;
329
-
330
- return match;
331
- }).length > 0;
332
-
333
- if (isUsedThroughExpression) {
334
- return true;
335
- }
336
-
337
- const isUsedThroughIdentifier: boolean =
338
- j(element)
339
- .find(j.JSXSpreadAttribute)
340
- .find(j.Identifier)
341
- .filter((identifier): boolean => {
342
- return (
343
- base
344
- .find(j.VariableDeclarator)
345
- .filter(
346
- (declarator) =>
347
- declarator.value.id.type === 'Identifier' &&
348
- declarator.value.id.name === identifier.value.name,
349
- )
350
- .filter((declarator) => {
351
- const value = declarator.value;
352
- if (value.id.type !== 'Identifier') {
353
- return false;
354
- }
355
-
356
- if (value.id.name !== identifier.value.name) {
357
- return false;
358
- }
359
-
360
- if (!value.init) {
361
- return false;
362
- }
363
-
364
- if (value.init.type !== 'ObjectExpression') {
365
- return false;
366
- }
367
-
368
- const match: boolean =
369
- value.init.properties.filter(
370
- (property) =>
371
- property.type === 'ObjectProperty' &&
372
- property.key.type === 'Identifier' &&
373
- property.key.name === propName,
374
- ).length > 0;
375
-
376
- return match;
377
- }).length > 0
378
- );
379
- }).length > 0;
380
-
381
- return isUsedThroughIdentifier;
304
+ if (!isUsingSupportedSpread({ j, base, element })) {
305
+ return false;
306
+ }
307
+
308
+ const isUsedThroughExpression: boolean =
309
+ j(element)
310
+ .find(j.JSXSpreadAttribute)
311
+ .find(j.ObjectExpression)
312
+ .filter((item) => {
313
+ const match: boolean =
314
+ item.value.properties.filter(
315
+ (property) =>
316
+ property.type === 'ObjectProperty' &&
317
+ property.key.type === 'Identifier' &&
318
+ property.key.name === propName,
319
+ ).length > 0;
320
+
321
+ return match;
322
+ }).length > 0;
323
+
324
+ if (isUsedThroughExpression) {
325
+ return true;
326
+ }
327
+
328
+ const isUsedThroughIdentifier: boolean =
329
+ j(element)
330
+ .find(j.JSXSpreadAttribute)
331
+ .find(j.Identifier)
332
+ .filter((identifier): boolean => {
333
+ return (
334
+ base
335
+ .find(j.VariableDeclarator)
336
+ .filter(
337
+ (declarator) =>
338
+ declarator.value.id.type === 'Identifier' &&
339
+ declarator.value.id.name === identifier.value.name,
340
+ )
341
+ .filter((declarator) => {
342
+ const value = declarator.value;
343
+ if (value.id.type !== 'Identifier') {
344
+ return false;
345
+ }
346
+
347
+ if (value.id.name !== identifier.value.name) {
348
+ return false;
349
+ }
350
+
351
+ if (!value.init) {
352
+ return false;
353
+ }
354
+
355
+ if (value.init.type !== 'ObjectExpression') {
356
+ return false;
357
+ }
358
+
359
+ const match: boolean =
360
+ value.init.properties.filter(
361
+ (property) =>
362
+ property.type === 'ObjectProperty' &&
363
+ property.key.type === 'Identifier' &&
364
+ property.key.name === propName,
365
+ ).length > 0;
366
+
367
+ return match;
368
+ }).length > 0
369
+ );
370
+ }).length > 0;
371
+
372
+ return isUsedThroughIdentifier;
382
373
  }
383
374
 
384
375
  export function isUsingProp({
385
- j,
386
- base,
387
- element,
388
- propName,
376
+ j,
377
+ base,
378
+ element,
379
+ propName,
389
380
  }: {
390
- j: core.JSCodeshift;
391
- base: Collection<any>;
392
- element: NodePath<JSXElement, JSXElement>;
393
- propName: string;
381
+ j: core.JSCodeshift;
382
+ base: Collection<any>;
383
+ element: NodePath<JSXElement, JSXElement>;
384
+ propName: string;
394
385
  }): boolean {
395
- return (
396
- hasJSXAttributesByName({
397
- j,
398
- element: element.value,
399
- attributeName: propName,
400
- }) ||
401
- isUsingThroughSpread({
402
- j,
403
- base,
404
- element,
405
- propName,
406
- })
407
- );
386
+ return (
387
+ hasJSXAttributesByName({
388
+ j,
389
+ element: element.value,
390
+ attributeName: propName,
391
+ }) ||
392
+ isUsingThroughSpread({
393
+ j,
394
+ base,
395
+ element,
396
+ propName,
397
+ })
398
+ );
408
399
  }
409
400
 
410
401
  // not replacing newlines (which \s does)
@@ -412,250 +403,227 @@ const spacesAndTabs: RegExp = /[ \t]{2,}/g;
412
403
  const lineStartWithSpaces: RegExp = /^[ \t]*/gm;
413
404
 
414
405
  function clean(value: string): string {
415
- return (
416
- value
417
- .replace(spacesAndTabs, ' ')
418
- .replace(lineStartWithSpaces, '')
419
- // using .trim() to clear the any newlines before the first text and after last text
420
- .trim()
421
- );
406
+ return (
407
+ value
408
+ .replace(spacesAndTabs, ' ')
409
+ .replace(lineStartWithSpaces, '')
410
+ // using .trim() to clear the any newlines before the first text and after last text
411
+ .trim()
412
+ );
422
413
  }
423
414
 
424
415
  export function addCommentToStartOfFile({
425
- j,
426
- base,
427
- message,
416
+ j,
417
+ base,
418
+ message,
428
419
  }: {
429
- j: core.JSCodeshift;
430
- base: Collection<Node>;
431
- message: string;
420
+ j: core.JSCodeshift;
421
+ base: Collection<Node>;
422
+ message: string;
432
423
  }) {
433
- addCommentBefore({
434
- j,
435
- target: base.find(j.Program),
436
- message,
437
- });
424
+ addCommentBefore({
425
+ j,
426
+ target: base.find(j.Program),
427
+ message,
428
+ });
438
429
  }
439
430
 
440
431
  export function addCommentBefore({
441
- j,
442
- target,
443
- message,
432
+ j,
433
+ target,
434
+ message,
444
435
  }: {
445
- j: core.JSCodeshift;
446
- target:
447
- | Collection<Node>
448
- | Collection<Program>
449
- | Collection<ImportDeclaration>;
450
- message: string;
436
+ j: core.JSCodeshift;
437
+ target: Collection<Node> | Collection<Program> | Collection<ImportDeclaration>;
438
+ message: string;
451
439
  }) {
452
- const content: string = ` TODO: (from codemod) ${clean(message)} `;
453
- target.forEach((path) => {
454
- path.value.comments = path.value.comments || [];
440
+ const content: string = ` TODO: (from codemod) ${clean(message)} `;
441
+ target.forEach((path) => {
442
+ path.value.comments = path.value.comments || [];
455
443
 
456
- const exists = path.value.comments.find(
457
- (comment) => comment.value === content,
458
- );
444
+ const exists = path.value.comments.find((comment) => comment.value === content);
459
445
 
460
- // avoiding duplicates of the same comment
461
- if (exists) {
462
- return;
463
- }
446
+ // avoiding duplicates of the same comment
447
+ if (exists) {
448
+ return;
449
+ }
464
450
 
465
- path.value.comments.push(j.commentBlock(content));
466
- });
451
+ path.value.comments.push(j.commentBlock(content));
452
+ });
467
453
  }
468
454
 
469
455
  export function shiftDefaultImport({
470
- j,
471
- base,
472
- defaultName,
473
- oldPackagePath,
474
- newPackagePath,
456
+ j,
457
+ base,
458
+ defaultName,
459
+ oldPackagePath,
460
+ newPackagePath,
475
461
  }: {
476
- j: core.JSCodeshift;
477
- base: Collection<any>;
478
- defaultName: string;
479
- oldPackagePath: string;
480
- newPackagePath: string;
462
+ j: core.JSCodeshift;
463
+ base: Collection<any>;
464
+ defaultName: string;
465
+ oldPackagePath: string;
466
+ newPackagePath: string;
481
467
  }) {
482
- tryCreateImport({
483
- j,
484
- base,
485
- relativeToPackage: oldPackagePath,
486
- packageName: newPackagePath,
487
- });
488
-
489
- addToImport({
490
- j,
491
- base,
492
- importSpecifier: j.importDefaultSpecifier(j.identifier(defaultName)),
493
- packageName: newPackagePath,
494
- });
495
-
496
- // removing old default specifier
497
- base
498
- .find(j.ImportDeclaration)
499
- .filter((path) => path.node.source.value === oldPackagePath)
500
- .find(j.ImportDefaultSpecifier)
501
- .remove();
468
+ tryCreateImport({
469
+ j,
470
+ base,
471
+ relativeToPackage: oldPackagePath,
472
+ packageName: newPackagePath,
473
+ });
474
+
475
+ addToImport({
476
+ j,
477
+ base,
478
+ importSpecifier: j.importDefaultSpecifier(j.identifier(defaultName)),
479
+ packageName: newPackagePath,
480
+ });
481
+
482
+ // removing old default specifier
483
+ base
484
+ .find(j.ImportDeclaration)
485
+ .filter((path) => path.node.source.value === oldPackagePath)
486
+ .find(j.ImportDefaultSpecifier)
487
+ .remove();
502
488
  }
503
489
 
504
490
  type Option =
505
- | {
506
- type: 'change-name';
507
- oldName: string;
508
- newName: string;
509
- fallbackNameAlias: string;
510
- }
511
- | {
512
- type: 'keep-name';
513
- name: string;
514
- behaviour: 'move-to-default-import' | 'keep-as-named-import';
515
- };
491
+ | {
492
+ type: 'change-name';
493
+ oldName: string;
494
+ newName: string;
495
+ fallbackNameAlias: string;
496
+ }
497
+ | {
498
+ type: 'keep-name';
499
+ name: string;
500
+ behaviour: 'move-to-default-import' | 'keep-as-named-import';
501
+ };
516
502
 
517
503
  // try to avoid this one if you can. I'm not super happy with it
518
504
  export function changeImportFor({
519
- j,
520
- base,
521
- option,
522
- oldPackagePath,
523
- newPackagePath,
505
+ j,
506
+ base,
507
+ option,
508
+ oldPackagePath,
509
+ newPackagePath,
524
510
  }: {
525
- j: core.JSCodeshift;
526
- base: Collection<any>;
527
- option: Option;
528
- oldPackagePath: string;
529
- newPackagePath: string;
511
+ j: core.JSCodeshift;
512
+ base: Collection<any>;
513
+ option: Option;
514
+ oldPackagePath: string;
515
+ newPackagePath: string;
530
516
  }) {
531
- const currentName: string =
532
- option.type === 'change-name' ? option.oldName : option.name;
533
- const desiredName: string =
534
- option.type === 'change-name' ? option.newName : option.name;
535
-
536
- const isUsingName: boolean =
537
- base
538
- .find(j.ImportDeclaration)
539
- .filter((path) => path.node.source.value === oldPackagePath)
540
- .find(j.ImportSpecifier)
541
- .find(j.Identifier)
542
- .filter((identifier) => identifier.value.name === currentName).length > 0;
543
-
544
- if (!isUsingName) {
545
- return;
546
- }
547
-
548
- const existingAlias: Nullable<string> =
549
- base
550
- .find(j.ImportDeclaration)
551
- .filter((path) => path.node.source.value === oldPackagePath)
552
- .find(j.ImportSpecifier)
553
- .nodes()
554
- .map((specifier): Nullable<string> => {
555
- if (specifier.imported.name !== currentName) {
556
- return null;
557
- }
558
- // If aliased: return the alias
559
- if (specifier.local && specifier.local.name !== currentName) {
560
- return specifier.local.name;
561
- }
562
-
563
- return null;
564
- })
565
- .filter(Boolean)[0] || null;
566
-
567
- base
568
- .find(j.ImportDeclaration)
569
- .filter((path) => path.node.source.value === oldPackagePath)
570
- .find(j.ImportSpecifier)
571
- .find(j.Identifier)
572
- .filter((identifier) => {
573
- if (identifier.value.name === currentName) {
574
- return true;
575
- }
576
- if (identifier.value.name === existingAlias) {
577
- return true;
578
- }
579
- return false;
580
- })
581
- .remove();
582
-
583
- // Check to see if need to create new package path
584
- // Try create an import declaration just before the old import
585
- tryCreateImport({
586
- j,
587
- base,
588
- relativeToPackage: oldPackagePath,
589
- packageName: newPackagePath,
590
- });
591
-
592
- if (option.type === 'keep-name') {
593
- const newSpecifier: ImportSpecifier | ImportDefaultSpecifier = (() => {
594
- if (option.behaviour === 'keep-as-named-import') {
595
- if (existingAlias) {
596
- return j.importSpecifier(
597
- j.identifier(desiredName),
598
- j.identifier(existingAlias),
599
- );
600
- }
601
-
602
- return j.importSpecifier(j.identifier(desiredName));
603
- }
604
-
605
- // moving to default specifier
606
- return j.importDefaultSpecifier(
607
- j.identifier(existingAlias || desiredName),
608
- );
609
- })();
610
-
611
- // We don't need to touch anything else in the file
612
-
613
- addToImport({
614
- j,
615
- base,
616
- importSpecifier: newSpecifier,
617
- packageName: newPackagePath,
618
- });
619
- return;
620
- }
621
-
622
- const isNewNameAvailable: boolean =
623
- base.find(j.Identifier).filter((i) => i.value.name === option.newName)
624
- .length === 0;
625
-
626
- const newSpecifier: ImportSpecifier = (() => {
627
- if (existingAlias) {
628
- return j.importSpecifier(
629
- j.identifier(desiredName),
630
- j.identifier(existingAlias),
631
- );
632
- }
633
-
634
- if (isNewNameAvailable) {
635
- return j.importSpecifier(j.identifier(desiredName));
636
- }
637
-
638
- // new type name is not available: need to use a new alias
639
- return j.importSpecifier(
640
- j.identifier(desiredName),
641
- j.identifier(option.fallbackNameAlias),
642
- );
643
- })();
644
-
645
- addToImport({
646
- j,
647
- base,
648
- importSpecifier: newSpecifier,
649
- packageName: newPackagePath,
650
- });
651
-
652
- // Change usages of old type in file
653
- base
654
- .find(j.Identifier)
655
- .filter((identifier) => identifier.value.name === option.oldName)
656
- .replaceWith(
657
- j.identifier(
658
- isNewNameAvailable ? option.newName : option.fallbackNameAlias,
659
- ),
660
- );
517
+ const currentName: string = option.type === 'change-name' ? option.oldName : option.name;
518
+ const desiredName: string = option.type === 'change-name' ? option.newName : option.name;
519
+
520
+ const isUsingName: boolean =
521
+ base
522
+ .find(j.ImportDeclaration)
523
+ .filter((path) => path.node.source.value === oldPackagePath)
524
+ .find(j.ImportSpecifier)
525
+ .find(j.Identifier)
526
+ .filter((identifier) => identifier.value.name === currentName).length > 0;
527
+
528
+ if (!isUsingName) {
529
+ return;
530
+ }
531
+
532
+ const existingAlias: Nullable<string> =
533
+ base
534
+ .find(j.ImportDeclaration)
535
+ .filter((path) => path.node.source.value === oldPackagePath)
536
+ .find(j.ImportSpecifier)
537
+ .nodes()
538
+ .map((specifier): Nullable<string> => {
539
+ if (specifier.imported.name !== currentName) {
540
+ return null;
541
+ }
542
+ // If aliased: return the alias
543
+ if (specifier.local && specifier.local.name !== currentName) {
544
+ return specifier.local.name;
545
+ }
546
+
547
+ return null;
548
+ })
549
+ .filter(Boolean)[0] || null;
550
+
551
+ base
552
+ .find(j.ImportDeclaration)
553
+ .filter((path) => path.node.source.value === oldPackagePath)
554
+ .find(j.ImportSpecifier)
555
+ .find(j.Identifier)
556
+ .filter((identifier) => {
557
+ if (identifier.value.name === currentName) {
558
+ return true;
559
+ }
560
+ if (identifier.value.name === existingAlias) {
561
+ return true;
562
+ }
563
+ return false;
564
+ })
565
+ .remove();
566
+
567
+ // Check to see if need to create new package path
568
+ // Try create an import declaration just before the old import
569
+ tryCreateImport({
570
+ j,
571
+ base,
572
+ relativeToPackage: oldPackagePath,
573
+ packageName: newPackagePath,
574
+ });
575
+
576
+ if (option.type === 'keep-name') {
577
+ const newSpecifier: ImportSpecifier | ImportDefaultSpecifier = (() => {
578
+ if (option.behaviour === 'keep-as-named-import') {
579
+ if (existingAlias) {
580
+ return j.importSpecifier(j.identifier(desiredName), j.identifier(existingAlias));
581
+ }
582
+
583
+ return j.importSpecifier(j.identifier(desiredName));
584
+ }
585
+
586
+ // moving to default specifier
587
+ return j.importDefaultSpecifier(j.identifier(existingAlias || desiredName));
588
+ })();
589
+
590
+ // We don't need to touch anything else in the file
591
+
592
+ addToImport({
593
+ j,
594
+ base,
595
+ importSpecifier: newSpecifier,
596
+ packageName: newPackagePath,
597
+ });
598
+ return;
599
+ }
600
+
601
+ const isNewNameAvailable: boolean =
602
+ base.find(j.Identifier).filter((i) => i.value.name === option.newName).length === 0;
603
+
604
+ const newSpecifier: ImportSpecifier = (() => {
605
+ if (existingAlias) {
606
+ return j.importSpecifier(j.identifier(desiredName), j.identifier(existingAlias));
607
+ }
608
+
609
+ if (isNewNameAvailable) {
610
+ return j.importSpecifier(j.identifier(desiredName));
611
+ }
612
+
613
+ // new type name is not available: need to use a new alias
614
+ return j.importSpecifier(j.identifier(desiredName), j.identifier(option.fallbackNameAlias));
615
+ })();
616
+
617
+ addToImport({
618
+ j,
619
+ base,
620
+ importSpecifier: newSpecifier,
621
+ packageName: newPackagePath,
622
+ });
623
+
624
+ // Change usages of old type in file
625
+ base
626
+ .find(j.Identifier)
627
+ .filter((identifier) => identifier.value.name === option.oldName)
628
+ .replaceWith(j.identifier(isNewNameAvailable ? option.newName : option.fallbackNameAlias));
661
629
  }