@atlaskit/avatar 21.9.1 → 21.9.2

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,157 +1,125 @@
1
1
  // eslint-disable-next-line @repo/internal/fs/filename-pattern-match
2
2
  import type {
3
- API,
4
- ASTPath,
5
- default as core,
6
- FileInfo,
7
- ImportDeclaration,
8
- ImportSpecifier,
9
- Options,
3
+ API,
4
+ ASTPath,
5
+ default as core,
6
+ FileInfo,
7
+ ImportDeclaration,
8
+ ImportSpecifier,
9
+ Options,
10
10
  } from 'jscodeshift';
11
11
 
12
- function getDefaultSpecifier(
13
- j: core.JSCodeshift,
14
- source: ReturnType<typeof j>,
15
- specifier: string,
16
- ) {
17
- const specifiers = source
18
- .find(j.ImportDeclaration)
19
- .filter(
20
- (path: ASTPath<ImportDeclaration>) =>
21
- path.node.source.value === specifier,
22
- )
23
- .find(j.ImportDefaultSpecifier);
24
-
25
- if (!specifiers.length) {
26
- return null;
27
- }
28
- return specifiers.nodes()[0]!.local!.name;
12
+ function getDefaultSpecifier(j: core.JSCodeshift, source: ReturnType<typeof j>, specifier: string) {
13
+ const specifiers = source
14
+ .find(j.ImportDeclaration)
15
+ .filter((path: ASTPath<ImportDeclaration>) => path.node.source.value === specifier)
16
+ .find(j.ImportDefaultSpecifier);
17
+
18
+ if (!specifiers.length) {
19
+ return null;
20
+ }
21
+ return specifiers.nodes()[0]!.local!.name;
29
22
  }
30
23
 
31
24
  function getImportSpecifier(
32
- j: core.JSCodeshift,
33
- source: ReturnType<typeof j>,
34
- specifier: string,
35
- imported: string,
25
+ j: core.JSCodeshift,
26
+ source: ReturnType<typeof j>,
27
+ specifier: string,
28
+ imported: string,
36
29
  ) {
37
- const specifiers = source
38
- .find(j.ImportDeclaration)
39
- .filter(
40
- (path: ASTPath<ImportDeclaration>) =>
41
- path.node.source.value === specifier,
42
- )
43
- .find(j.ImportSpecifier)
44
- .filter(
45
- (path: ASTPath<ImportSpecifier>) => path.value.imported.name === imported,
46
- );
47
-
48
- if (!specifiers.length) {
49
- return null;
50
- }
51
-
52
- return specifiers.nodes()[0]!.local!.name;
30
+ const specifiers = source
31
+ .find(j.ImportDeclaration)
32
+ .filter((path: ASTPath<ImportDeclaration>) => path.node.source.value === specifier)
33
+ .find(j.ImportSpecifier)
34
+ .filter((path: ASTPath<ImportSpecifier>) => path.value.imported.name === imported);
35
+
36
+ if (!specifiers.length) {
37
+ return null;
38
+ }
39
+
40
+ return specifiers.nodes()[0]!.local!.name;
53
41
  }
54
42
 
55
- function getJSXAttributesByName(
56
- j: core.JSCodeshift,
57
- element: ASTPath<any>,
58
- attributeName: string,
59
- ) {
60
- return j(element)
61
- .find(j.JSXOpeningElement)
62
- .find(j.JSXAttribute)
63
- .filter((attribute) => {
64
- const matches = j(attribute)
65
- .find(j.JSXIdentifier)
66
- .filter((identifier) => identifier.value.name === attributeName);
67
- return Boolean(matches.length);
68
- });
43
+ function getJSXAttributesByName(j: core.JSCodeshift, element: ASTPath<any>, attributeName: string) {
44
+ return j(element)
45
+ .find(j.JSXOpeningElement)
46
+ .find(j.JSXAttribute)
47
+ .filter((attribute) => {
48
+ const matches = j(attribute)
49
+ .find(j.JSXIdentifier)
50
+ .filter((identifier) => identifier.value.name === attributeName);
51
+ return Boolean(matches.length);
52
+ });
69
53
  }
70
54
 
71
- function wrapChildrenProp(
72
- j: core.JSCodeshift,
73
- source: ReturnType<typeof j>,
74
- specifier: string,
75
- ) {
76
- source.findJSXElements(specifier).forEach((element) => {
77
- const componentProp = getJSXAttributesByName(j, element, 'component').get();
78
- const componentName = j(componentProp)
79
- .find(j.JSXExpressionContainer)
80
- .find(j.Expression)
81
- .get().value.name;
82
-
83
- const customComponent = j.jsxElement(
84
- j.jsxOpeningElement(
85
- j.jsxIdentifier(componentName),
86
- [j.jsxSpreadAttribute(j.identifier('props'))],
87
- true,
88
- ),
89
- );
90
-
91
- const childrenExpression = j.jsxExpressionContainer(
92
- j.arrowFunctionExpression(
93
- [
94
- j.objectPattern([
95
- j.objectProperty(j.identifier('ref'), j.identifier('_')),
96
- j.restProperty(j.identifier('props')),
97
- ]),
98
- ],
99
- customComponent,
100
- ),
101
- );
102
-
103
- j(componentProp).remove();
104
- j(element).replaceWith(
105
- j.jsxElement(
106
- j.jsxOpeningElement(
107
- element.value.openingElement.name,
108
- element.value.openingElement.attributes,
109
- false,
110
- ),
111
- j.jsxClosingElement(element.value.openingElement.name),
112
- [childrenExpression],
113
- ),
114
- );
115
- });
55
+ function wrapChildrenProp(j: core.JSCodeshift, source: ReturnType<typeof j>, specifier: string) {
56
+ source.findJSXElements(specifier).forEach((element) => {
57
+ const componentProp = getJSXAttributesByName(j, element, 'component').get();
58
+ const componentName = j(componentProp).find(j.JSXExpressionContainer).find(j.Expression).get()
59
+ .value.name;
60
+
61
+ const customComponent = j.jsxElement(
62
+ j.jsxOpeningElement(
63
+ j.jsxIdentifier(componentName),
64
+ [j.jsxSpreadAttribute(j.identifier('props'))],
65
+ true,
66
+ ),
67
+ );
68
+
69
+ const childrenExpression = j.jsxExpressionContainer(
70
+ j.arrowFunctionExpression(
71
+ [
72
+ j.objectPattern([
73
+ j.objectProperty(j.identifier('ref'), j.identifier('_')),
74
+ j.restProperty(j.identifier('props')),
75
+ ]),
76
+ ],
77
+ customComponent,
78
+ ),
79
+ );
80
+
81
+ j(componentProp).remove();
82
+ j(element).replaceWith(
83
+ j.jsxElement(
84
+ j.jsxOpeningElement(
85
+ element.value.openingElement.name,
86
+ element.value.openingElement.attributes,
87
+ false,
88
+ ),
89
+ j.jsxClosingElement(element.value.openingElement.name),
90
+ [childrenExpression],
91
+ ),
92
+ );
93
+ });
116
94
  }
117
95
 
118
96
  function hasImportDeclaration(
119
- j: core.JSCodeshift,
120
- source: ReturnType<typeof j>,
121
- importPath: string,
97
+ j: core.JSCodeshift,
98
+ source: ReturnType<typeof j>,
99
+ importPath: string,
122
100
  ) {
123
- return !!source
124
- .find(j.ImportDeclaration)
125
- .filter((path) => path.node.source.value === importPath).length;
101
+ return !!source.find(j.ImportDeclaration).filter((path) => path.node.source.value === importPath)
102
+ .length;
126
103
  }
127
104
 
128
- export default function transformer(
129
- fileInfo: FileInfo,
130
- { jscodeshift: j }: API,
131
- options: Options,
132
- ) {
133
- const source = j(fileInfo.source);
105
+ export default function transformer(fileInfo: FileInfo, { jscodeshift: j }: API, options: Options) {
106
+ const source = j(fileInfo.source);
134
107
 
135
- if (hasImportDeclaration(j, source, '@atlaskit/avatar')) {
136
- const defaultSpecifier = getDefaultSpecifier(j, source, '@atlaskit/avatar');
108
+ if (hasImportDeclaration(j, source, '@atlaskit/avatar')) {
109
+ const defaultSpecifier = getDefaultSpecifier(j, source, '@atlaskit/avatar');
137
110
 
138
- if (defaultSpecifier != null) {
139
- wrapChildrenProp(j, source, defaultSpecifier);
140
- }
111
+ if (defaultSpecifier != null) {
112
+ wrapChildrenProp(j, source, defaultSpecifier);
113
+ }
141
114
 
142
- const importSpecifier = getImportSpecifier(
143
- j,
144
- source,
145
- '@atlaskit/avatar',
146
- 'AvatarItem',
147
- );
115
+ const importSpecifier = getImportSpecifier(j, source, '@atlaskit/avatar', 'AvatarItem');
148
116
 
149
- if (importSpecifier != null) {
150
- wrapChildrenProp(j, source, importSpecifier);
151
- }
117
+ if (importSpecifier != null) {
118
+ wrapChildrenProp(j, source, importSpecifier);
119
+ }
152
120
 
153
- return source.toSource(options.printOptions || { quote: 'single' });
154
- }
121
+ return source.toSource(options.printOptions || { quote: 'single' });
122
+ }
155
123
 
156
- return fileInfo.source;
124
+ return fileInfo.source;
157
125
  }