@atlaskit/radio 8.4.7 → 8.5.1

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