@atlaskit/section-message 6.5.2 → 6.5.4

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,215 +1,188 @@
1
1
  import {
2
- type ASTPath,
3
- type default as core,
4
- type ImportDeclaration,
5
- type JSXAttribute,
6
- type JSXElement,
7
- type JSXExpressionContainer,
8
- type VariableDeclaration,
2
+ type ASTPath,
3
+ type default as core,
4
+ type ImportDeclaration,
5
+ type JSXAttribute,
6
+ type JSXElement,
7
+ type JSXExpressionContainer,
8
+ type VariableDeclaration,
9
9
  } from 'jscodeshift';
10
10
  import { type Collection } from 'jscodeshift/src/Collection';
11
11
 
12
12
  import {
13
- addCommentBefore,
14
- getDefaultSpecifier,
15
- getJSXAttributesByName,
13
+ addCommentBefore,
14
+ getDefaultSpecifier,
15
+ getJSXAttributesByName,
16
16
  } from '@atlaskit/codemod-utils';
17
17
 
18
18
  import {
19
- ACTIONS_PROP_NAME,
20
- LINK_COMPONENT_PROP_NAME,
21
- SECTION_MESSAGE_ACTION_COMPONENT_NAME,
22
- SECTION_MESSAGE_ACTION_PACKAGE_NAME,
23
- SECTION_MESSAGE_PACKAGE_NAME,
19
+ ACTIONS_PROP_NAME,
20
+ LINK_COMPONENT_PROP_NAME,
21
+ SECTION_MESSAGE_ACTION_COMPONENT_NAME,
22
+ SECTION_MESSAGE_ACTION_PACKAGE_NAME,
23
+ SECTION_MESSAGE_PACKAGE_NAME,
24
24
  } from './constants';
25
25
  import { doesIdentifierExist, getDynamicImportName } from './utils';
26
26
 
27
27
  const addSectionMessageActionImportSpecifier = (
28
- j: core.JSCodeshift,
29
- source: Collection<Node>,
30
- name: string,
28
+ j: core.JSCodeshift,
29
+ source: Collection<Node>,
30
+ name: string,
31
31
  ) => {
32
- source
33
- .find(j.ImportDeclaration)
34
- .filter(
35
- (path: ASTPath<ImportDeclaration>) =>
36
- path.node.source.value === SECTION_MESSAGE_PACKAGE_NAME,
37
- )
38
- .forEach((path: ASTPath<ImportDeclaration>) => {
39
- path.value.specifiers?.push(
40
- j.importSpecifier(
41
- j.identifier(SECTION_MESSAGE_ACTION_COMPONENT_NAME),
42
- j.identifier(name),
43
- ),
44
- );
45
- });
32
+ source
33
+ .find(j.ImportDeclaration)
34
+ .filter(
35
+ (path: ASTPath<ImportDeclaration>) => path.node.source.value === SECTION_MESSAGE_PACKAGE_NAME,
36
+ )
37
+ .forEach((path: ASTPath<ImportDeclaration>) => {
38
+ path.value.specifiers?.push(
39
+ j.importSpecifier(j.identifier(SECTION_MESSAGE_ACTION_COMPONENT_NAME), j.identifier(name)),
40
+ );
41
+ });
46
42
  };
47
43
 
48
44
  const addSectionMessageActionDynamicImport = (
49
- j: core.JSCodeshift,
50
- target: Collection<VariableDeclaration>,
51
- name: string,
45
+ j: core.JSCodeshift,
46
+ target: Collection<VariableDeclaration>,
47
+ name: string,
52
48
  ) => {
53
- const node = j.variableDeclaration('const', [
54
- j.variableDeclarator(
55
- j.identifier(name),
56
- j.callExpression(
57
- j.memberExpression(j.identifier('React'), j.identifier('lazy')),
58
- [
59
- j.arrowFunctionExpression(
60
- [],
61
- j.callExpression(j.import(), [
62
- j.stringLiteral(SECTION_MESSAGE_ACTION_PACKAGE_NAME),
63
- ]),
64
- ),
65
- ],
66
- ),
67
- ),
68
- ]);
69
-
70
- target.insertAfter(node);
71
- addCommentBefore(
72
- j,
73
- j(node),
74
- 'We have added `React.lazy` here. Feel free to change it to `lazy` or other named import depending upon how you imported.',
75
- );
49
+ const node = j.variableDeclaration('const', [
50
+ j.variableDeclarator(
51
+ j.identifier(name),
52
+ j.callExpression(j.memberExpression(j.identifier('React'), j.identifier('lazy')), [
53
+ j.arrowFunctionExpression(
54
+ [],
55
+ j.callExpression(j.import(), [j.stringLiteral(SECTION_MESSAGE_ACTION_PACKAGE_NAME)]),
56
+ ),
57
+ ]),
58
+ ),
59
+ ]);
60
+
61
+ target.insertAfter(node);
62
+ addCommentBefore(
63
+ j,
64
+ j(node),
65
+ 'We have added `React.lazy` here. Feel free to change it to `lazy` or other named import depending upon how you imported.',
66
+ );
76
67
  };
77
68
 
78
- const transferLinkComponentProp = (
79
- j: core.JSCodeshift,
80
- element: ASTPath<JSXElement>,
81
- ) => {
82
- const linkComponentAttributeCollection: Collection<JSXAttribute> =
83
- getJSXAttributesByName(j, element, LINK_COMPONENT_PROP_NAME);
84
- const linkComponentAttribute =
85
- linkComponentAttributeCollection.length === 1
86
- ? linkComponentAttributeCollection.paths()[0]
87
- : null;
69
+ const transferLinkComponentProp = (j: core.JSCodeshift, element: ASTPath<JSXElement>) => {
70
+ const linkComponentAttributeCollection: Collection<JSXAttribute> = getJSXAttributesByName(
71
+ j,
72
+ element,
73
+ LINK_COMPONENT_PROP_NAME,
74
+ );
75
+ const linkComponentAttribute =
76
+ linkComponentAttributeCollection.length === 1
77
+ ? linkComponentAttributeCollection.paths()[0]
78
+ : null;
88
79
 
89
- const linkComponentAttributeValue = linkComponentAttribute?.node?.value;
80
+ const linkComponentAttributeValue = linkComponentAttribute?.node?.value;
90
81
 
91
- if (linkComponentAttribute) {
92
- j(linkComponentAttribute).remove();
93
- }
82
+ if (linkComponentAttribute) {
83
+ j(linkComponentAttribute).remove();
84
+ }
94
85
 
95
- return linkComponentAttributeValue;
86
+ return linkComponentAttributeValue;
96
87
  };
97
88
 
98
- export const mapActionsProp = (
99
- j: core.JSCodeshift,
100
- source: Collection<Node>,
101
- ) => {
102
- const sectionMessageDefaultSpecifierName =
103
- getDefaultSpecifier(j, source, SECTION_MESSAGE_PACKAGE_NAME) ?? '';
104
-
105
- const sectionMessageDynamicImportName =
106
- getDynamicImportName(j, source, SECTION_MESSAGE_PACKAGE_NAME) ?? '';
107
-
108
- if (!sectionMessageDefaultSpecifierName && !sectionMessageDynamicImportName) {
109
- return;
110
- }
111
-
112
- const sectionMessageActionName = doesIdentifierExist({
113
- j,
114
- base: source,
115
- name: SECTION_MESSAGE_ACTION_COMPONENT_NAME,
116
- })
117
- ? `Atlaskit${SECTION_MESSAGE_ACTION_COMPONENT_NAME}`
118
- : SECTION_MESSAGE_ACTION_COMPONENT_NAME;
119
-
120
- let actionsAttributes: Collection<JSXAttribute> | null = null;
121
-
122
- source
123
- .findJSXElements(
124
- sectionMessageDefaultSpecifierName || sectionMessageDynamicImportName,
125
- )
126
- .forEach((element) => {
127
- const linkComponentAttributeValue = transferLinkComponentProp(j, element);
128
- actionsAttributes = getJSXAttributesByName(j, element, ACTIONS_PROP_NAME);
129
-
130
- actionsAttributes?.forEach((attribute: ASTPath<JSXAttribute>) => {
131
- j(attribute)
132
- .find(j.JSXExpressionContainer)
133
- .forEach((expressionContainer: ASTPath<JSXExpressionContainer>) => {
134
- const { expression } = expressionContainer.node;
135
-
136
- if (expression.type === 'JSXEmptyExpression') {
137
- return;
138
- }
139
-
140
- const textObjectProperty = j.objectProperty(
141
- j.identifier('text'),
142
- j.identifier('text'),
143
- );
144
-
145
- textObjectProperty.shorthand = true;
146
-
147
- j(expressionContainer).replaceWith(
148
- j.jsxExpressionContainer(
149
- j.callExpression(
150
- j.memberExpression(expression, j.identifier('map')),
151
- [
152
- j.arrowFunctionExpression(
153
- [
154
- j.objectPattern([
155
- textObjectProperty,
156
- j.restProperty(j.identifier('restAction')),
157
- ]),
158
- ],
159
- j.jsxElement(
160
- j.jsxOpeningElement(
161
- j.jsxIdentifier(sectionMessageActionName),
162
- [
163
- linkComponentAttributeValue
164
- ? j.jsxAttribute(
165
- j.jsxIdentifier(LINK_COMPONENT_PROP_NAME),
166
- linkComponentAttributeValue,
167
- )
168
- : null,
169
- j.jsxSpreadAttribute(j.identifier('restAction')),
170
- ].filter(Boolean) as JSXAttribute[],
171
- ),
172
- j.jsxClosingElement(
173
- j.jsxIdentifier(sectionMessageActionName),
174
- ),
175
- [j.jsxExpressionContainer(j.identifier('text'))],
176
- ),
177
- ),
178
- ],
179
- ),
180
- ),
181
- );
182
- });
183
- });
184
- });
185
-
186
- // @ts-ignore
187
- if (actionsAttributes && actionsAttributes.length > 0) {
188
- if (sectionMessageDefaultSpecifierName) {
189
- addSectionMessageActionImportSpecifier(
190
- j,
191
- source,
192
- sectionMessageActionName,
193
- );
194
- }
195
-
196
- if (sectionMessageDynamicImportName) {
197
- addSectionMessageActionDynamicImport(
198
- j,
199
- source.find(j.VariableDeclaration).filter((variableDeclarationPath) => {
200
- return (
201
- j(variableDeclarationPath)
202
- .find(j.VariableDeclarator)
203
- .filter(
204
- (variableDeclaratorPath) =>
205
- variableDeclaratorPath.node.id.type === 'Identifier' &&
206
- variableDeclaratorPath.node.id.name ===
207
- sectionMessageDynamicImportName,
208
- ).length > 0
209
- );
210
- }),
211
- sectionMessageActionName,
212
- );
213
- }
214
- }
89
+ export const mapActionsProp = (j: core.JSCodeshift, source: Collection<Node>) => {
90
+ const sectionMessageDefaultSpecifierName =
91
+ getDefaultSpecifier(j, source, SECTION_MESSAGE_PACKAGE_NAME) ?? '';
92
+
93
+ const sectionMessageDynamicImportName =
94
+ getDynamicImportName(j, source, SECTION_MESSAGE_PACKAGE_NAME) ?? '';
95
+
96
+ if (!sectionMessageDefaultSpecifierName && !sectionMessageDynamicImportName) {
97
+ return;
98
+ }
99
+
100
+ const sectionMessageActionName = doesIdentifierExist({
101
+ j,
102
+ base: source,
103
+ name: SECTION_MESSAGE_ACTION_COMPONENT_NAME,
104
+ })
105
+ ? `Atlaskit${SECTION_MESSAGE_ACTION_COMPONENT_NAME}`
106
+ : SECTION_MESSAGE_ACTION_COMPONENT_NAME;
107
+
108
+ let actionsAttributes: Collection<JSXAttribute> | null = null;
109
+
110
+ source
111
+ .findJSXElements(sectionMessageDefaultSpecifierName || sectionMessageDynamicImportName)
112
+ .forEach((element) => {
113
+ const linkComponentAttributeValue = transferLinkComponentProp(j, element);
114
+ actionsAttributes = getJSXAttributesByName(j, element, ACTIONS_PROP_NAME);
115
+
116
+ actionsAttributes?.forEach((attribute: ASTPath<JSXAttribute>) => {
117
+ j(attribute)
118
+ .find(j.JSXExpressionContainer)
119
+ .forEach((expressionContainer: ASTPath<JSXExpressionContainer>) => {
120
+ const { expression } = expressionContainer.node;
121
+
122
+ if (expression.type === 'JSXEmptyExpression') {
123
+ return;
124
+ }
125
+
126
+ const textObjectProperty = j.objectProperty(j.identifier('text'), j.identifier('text'));
127
+
128
+ textObjectProperty.shorthand = true;
129
+
130
+ j(expressionContainer).replaceWith(
131
+ j.jsxExpressionContainer(
132
+ j.callExpression(j.memberExpression(expression, j.identifier('map')), [
133
+ j.arrowFunctionExpression(
134
+ [
135
+ j.objectPattern([
136
+ textObjectProperty,
137
+ j.restProperty(j.identifier('restAction')),
138
+ ]),
139
+ ],
140
+ j.jsxElement(
141
+ j.jsxOpeningElement(
142
+ j.jsxIdentifier(sectionMessageActionName),
143
+ [
144
+ linkComponentAttributeValue
145
+ ? j.jsxAttribute(
146
+ j.jsxIdentifier(LINK_COMPONENT_PROP_NAME),
147
+ linkComponentAttributeValue,
148
+ )
149
+ : null,
150
+ j.jsxSpreadAttribute(j.identifier('restAction')),
151
+ ].filter(Boolean) as JSXAttribute[],
152
+ ),
153
+ j.jsxClosingElement(j.jsxIdentifier(sectionMessageActionName)),
154
+ [j.jsxExpressionContainer(j.identifier('text'))],
155
+ ),
156
+ ),
157
+ ]),
158
+ ),
159
+ );
160
+ });
161
+ });
162
+ });
163
+
164
+ // @ts-ignore
165
+ if (actionsAttributes && actionsAttributes.length > 0) {
166
+ if (sectionMessageDefaultSpecifierName) {
167
+ addSectionMessageActionImportSpecifier(j, source, sectionMessageActionName);
168
+ }
169
+
170
+ if (sectionMessageDynamicImportName) {
171
+ addSectionMessageActionDynamicImport(
172
+ j,
173
+ source.find(j.VariableDeclaration).filter((variableDeclarationPath) => {
174
+ return (
175
+ j(variableDeclarationPath)
176
+ .find(j.VariableDeclarator)
177
+ .filter(
178
+ (variableDeclaratorPath) =>
179
+ variableDeclaratorPath.node.id.type === 'Identifier' &&
180
+ variableDeclaratorPath.node.id.name === sectionMessageDynamicImportName,
181
+ ).length > 0
182
+ );
183
+ }),
184
+ sectionMessageActionName,
185
+ );
186
+ }
187
+ }
215
188
  };
@@ -2,86 +2,70 @@ import { type CallExpression, type default as core } from 'jscodeshift';
2
2
  import { type Collection } from 'jscodeshift/src/Collection';
3
3
 
4
4
  export function doesIdentifierExist({
5
- j,
6
- base,
7
- name,
5
+ j,
6
+ base,
7
+ name,
8
8
  }: {
9
- j: core.JSCodeshift;
10
- base: Collection<any>;
11
- name: string;
9
+ j: core.JSCodeshift;
10
+ base: Collection<any>;
11
+ name: string;
12
12
  }): boolean {
13
- return (
14
- base.find(j.Identifier).filter((identifer) => identifer.value.name === name)
15
- .length > 0
16
- );
13
+ return base.find(j.Identifier).filter((identifer) => identifer.value.name === name).length > 0;
17
14
  }
18
15
 
19
- export function hasDynamicImport(
20
- j: core.JSCodeshift,
21
- source: Collection<any>,
22
- importPath: string,
23
- ) {
24
- return !!getDynamicImportName(j, source, importPath);
16
+ export function hasDynamicImport(j: core.JSCodeshift, source: Collection<any>, importPath: string) {
17
+ return !!getDynamicImportName(j, source, importPath);
25
18
  }
26
19
 
27
20
  export function getDynamicImportName(
28
- j: core.JSCodeshift,
29
- source: Collection<any>,
30
- importPath: string,
21
+ j: core.JSCodeshift,
22
+ source: Collection<any>,
23
+ importPath: string,
31
24
  ) {
32
- const dynamicImports = source
33
- .find(j.VariableDeclarator)
34
- .filter((variableDeclaratorPath) => {
35
- return (
36
- j(variableDeclaratorPath)
37
- .find(j.CallExpression)
38
- .filter((callExpressionPath) => {
39
- const { callee, arguments: callExpressionArguments } =
40
- callExpressionPath.node;
25
+ const dynamicImports = source.find(j.VariableDeclarator).filter((variableDeclaratorPath) => {
26
+ return (
27
+ j(variableDeclaratorPath)
28
+ .find(j.CallExpression)
29
+ .filter((callExpressionPath) => {
30
+ const { callee, arguments: callExpressionArguments } = callExpressionPath.node;
41
31
 
42
- return !!(
43
- isCallExpressionCalleeImportType(callee) &&
44
- isCallExpressionArgumentStringLiteralType(
45
- callExpressionArguments,
46
- ) &&
47
- isCallExpressionArgumentValueMatches(
48
- callExpressionArguments[0],
49
- j,
50
- importPath,
51
- )
52
- );
53
- }).length > 0
54
- );
55
- });
32
+ return !!(
33
+ isCallExpressionCalleeImportType(callee) &&
34
+ isCallExpressionArgumentStringLiteralType(callExpressionArguments) &&
35
+ isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath)
36
+ );
37
+ }).length > 0
38
+ );
39
+ });
56
40
 
57
- if (!dynamicImports.length) {
58
- return null;
59
- }
41
+ if (!dynamicImports.length) {
42
+ return null;
43
+ }
60
44
 
61
- const { id } = dynamicImports.nodes()[0];
45
+ const { id } = dynamicImports.nodes()[0];
62
46
 
63
- if (id.type !== 'Identifier') {
64
- return null;
65
- }
47
+ if (id.type !== 'Identifier') {
48
+ return null;
49
+ }
66
50
 
67
- return id.name;
51
+ return id.name;
68
52
  }
69
53
  function isCallExpressionCalleeImportType(callee: CallExpression['callee']) {
70
- return callee && callee.type === 'Import';
54
+ return callee && callee.type === 'Import';
71
55
  }
72
56
  function isCallExpressionArgumentStringLiteralType(
73
- callExpressionArguments: CallExpression['arguments'],
57
+ callExpressionArguments: CallExpression['arguments'],
74
58
  ) {
75
- return (
76
- callExpressionArguments &&
77
- callExpressionArguments.length &&
78
- callExpressionArguments[0].type === 'StringLiteral'
79
- );
59
+ return (
60
+ callExpressionArguments &&
61
+ callExpressionArguments.length &&
62
+ callExpressionArguments[0].type === 'StringLiteral'
63
+ );
80
64
  }
81
65
  function isCallExpressionArgumentValueMatches(
82
- callExpressionArgument: CallExpression['arguments'][0],
83
- j: core.JSCodeshift,
84
- value: string,
66
+ callExpressionArgument: CallExpression['arguments'][0],
67
+ j: core.JSCodeshift,
68
+ value: string,
85
69
  ) {
86
- return j(callExpressionArgument).some((path) => path.node.value === value);
70
+ return j(callExpressionArgument).some((path) => path.node.value === value);
87
71
  }
@@ -1 +1 @@
1
- export type { Appearance, SectionMessageProps, SectionMessageActionProps, } from '../types';
1
+ export type { Appearance, SectionMessageProps, SectionMessageActionProps } from '../types';
@@ -1,3 +1,3 @@
1
1
  export { default } from './section-message';
2
2
  export { default as SectionMessageAction } from './section-message-action';
3
- export type { Appearance, SectionMessageProps, SectionMessageActionProps, } from './types';
3
+ export type { Appearance, SectionMessageProps, SectionMessageActionProps } from './types';
@@ -1 +1 @@
1
- export type { Appearance, SectionMessageProps, SectionMessageActionProps, } from '../types';
1
+ export type { Appearance, SectionMessageProps, SectionMessageActionProps } from '../types';
@@ -1,3 +1,3 @@
1
1
  export { default } from './section-message';
2
2
  export { default as SectionMessageAction } from './section-message-action';
3
- export type { Appearance, SectionMessageProps, SectionMessageActionProps, } from './types';
3
+ export type { Appearance, SectionMessageProps, SectionMessageActionProps } from './types';