@carbon/upgrade 11.38.0 → 11.39.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.
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Copyright IBM Corp. 2026, 2026
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /**
9
+ * Rewrites PageHeader imports from Carbon packages to IBM Products packages.
10
+ *
11
+ * Transforms:
12
+ *
13
+ * import { preview__PageHeader as PageHeader } from '@carbon/react';
14
+ *
15
+ * Into:
16
+ *
17
+ * import { PageHeader } from '@carbon/ibm-products';
18
+ */
19
+
20
+ 'use strict';
21
+
22
+ const PRODUCTS_REACT_PATH = '@carbon/ibm-products';
23
+ const PRODUCTS_WEB_COMPONENTS_PATH = '@carbon/ibm-products-web-components';
24
+ const REACT_PAGE_HEADER_IMPORTS = new Set([
25
+ 'PageHeader',
26
+ 'PageHeaderBreadcrumbBar',
27
+ 'PageHeaderContent',
28
+ 'PageHeaderContentPageActions',
29
+ 'PageHeaderContentText',
30
+ 'PageHeaderHeroImage',
31
+ 'PageHeaderTabBar',
32
+ 'preview__PageHeader',
33
+ 'unstable__PageHeader',
34
+ ]);
35
+
36
+ const normalizeReactImportName = (name) => {
37
+ if (name === 'preview__PageHeader' || name === 'unstable__PageHeader') {
38
+ return 'PageHeader';
39
+ }
40
+
41
+ return name;
42
+ };
43
+
44
+ const isReactPageHeaderPath = (value) =>
45
+ /^@carbon\/react\/(es|lib)\/components\/PageHeader(?:\/index(?:\.js)?)?$/.test(
46
+ value
47
+ );
48
+
49
+ const rewriteWebComponentsPath = (value) => {
50
+ if (value.startsWith('@carbon/web-components/es/components/page-header')) {
51
+ return value.replace(
52
+ '@carbon/web-components/es/components/page-header',
53
+ `${PRODUCTS_WEB_COMPONENTS_PATH}/es/components/page-header`
54
+ );
55
+ }
56
+
57
+ if (value.startsWith('@carbon/web-components/lib/components/page-header')) {
58
+ return value.replace(
59
+ '@carbon/web-components/lib/components/page-header',
60
+ `${PRODUCTS_WEB_COMPONENTS_PATH}/lib/components/page-header`
61
+ );
62
+ }
63
+
64
+ return value;
65
+ };
66
+
67
+ const dedupeSpecifiers = (specifiers) => {
68
+ const seen = new Set();
69
+
70
+ return specifiers.filter((specifier) => {
71
+ if (specifier.type !== 'ImportSpecifier') {
72
+ return true;
73
+ }
74
+
75
+ const importedName = specifier.imported.name;
76
+ const localName = specifier.local?.name ?? importedName;
77
+ const key = `${importedName}:${localName}`;
78
+
79
+ if (seen.has(key)) {
80
+ return false;
81
+ }
82
+
83
+ seen.add(key);
84
+ return true;
85
+ });
86
+ };
87
+
88
+ const createPageHeaderSpecifier = (j, specifier) => {
89
+ if (
90
+ specifier.type !== 'ImportSpecifier' ||
91
+ !REACT_PAGE_HEADER_IMPORTS.has(specifier.imported.name)
92
+ ) {
93
+ return specifier;
94
+ }
95
+
96
+ const importedName = normalizeReactImportName(specifier.imported.name);
97
+ const localName = specifier.local?.name;
98
+
99
+ return localName && localName !== importedName
100
+ ? j.importSpecifier(j.identifier(importedName), j.identifier(localName))
101
+ : j.importSpecifier(j.identifier(importedName));
102
+ };
103
+
104
+ function transformer(file, api) {
105
+ const j = api.jscodeshift;
106
+ const root = j(file.source);
107
+
108
+ function findProductsReactImport(excludePath) {
109
+ let productsReactImport = null;
110
+
111
+ root
112
+ .find(j.ImportDeclaration, {
113
+ source: {
114
+ value: PRODUCTS_REACT_PATH,
115
+ },
116
+ })
117
+ .forEach((path) => {
118
+ if (!productsReactImport && path !== excludePath) {
119
+ productsReactImport = path;
120
+ }
121
+ });
122
+
123
+ return productsReactImport;
124
+ }
125
+
126
+ function addProductsReactSpecifiers(specifiers, insertAfterPath) {
127
+ const productsReactImport = findProductsReactImport();
128
+
129
+ if (productsReactImport) {
130
+ productsReactImport.node.specifiers = dedupeSpecifiers([
131
+ ...productsReactImport.node.specifiers,
132
+ ...specifiers,
133
+ ]);
134
+ return;
135
+ }
136
+
137
+ const newImportDeclaration = j.importDeclaration(
138
+ dedupeSpecifiers(specifiers),
139
+ j.literal(PRODUCTS_REACT_PATH)
140
+ );
141
+
142
+ j(insertAfterPath).insertAfter(newImportDeclaration);
143
+ }
144
+
145
+ root
146
+ .find(j.ImportDeclaration, {
147
+ source: {
148
+ value: '@carbon/react',
149
+ },
150
+ })
151
+ .forEach((path) => {
152
+ const movedSpecifiers = [];
153
+ const remainingSpecifiers = [];
154
+
155
+ path.node.specifiers.forEach((specifier) => {
156
+ if (
157
+ specifier.type === 'ImportSpecifier' &&
158
+ REACT_PAGE_HEADER_IMPORTS.has(specifier.imported.name)
159
+ ) {
160
+ movedSpecifiers.push(createPageHeaderSpecifier(j, specifier));
161
+ return;
162
+ }
163
+
164
+ remainingSpecifiers.push(specifier);
165
+ });
166
+
167
+ if (movedSpecifiers.length === 0) {
168
+ return;
169
+ }
170
+
171
+ if (remainingSpecifiers.length === 0) {
172
+ const productsReactImport = findProductsReactImport(path);
173
+
174
+ if (productsReactImport) {
175
+ productsReactImport.node.specifiers = dedupeSpecifiers([
176
+ ...productsReactImport.node.specifiers,
177
+ ...movedSpecifiers,
178
+ ]);
179
+ j(path).remove();
180
+ } else {
181
+ const newImportDeclaration = j.importDeclaration(
182
+ dedupeSpecifiers(movedSpecifiers),
183
+ j.literal(PRODUCTS_REACT_PATH)
184
+ );
185
+
186
+ j(path).replaceWith(newImportDeclaration);
187
+ }
188
+
189
+ return;
190
+ }
191
+
192
+ path.node.specifiers = remainingSpecifiers;
193
+ addProductsReactSpecifiers(movedSpecifiers, path);
194
+ });
195
+
196
+ root.find(j.ImportDeclaration).forEach((path) => {
197
+ const sourceValue = path.node.source.value;
198
+
199
+ if (typeof sourceValue !== 'string') {
200
+ return;
201
+ }
202
+
203
+ if (isReactPageHeaderPath(sourceValue)) {
204
+ const specifiers = path.node.specifiers.map((specifier) =>
205
+ createPageHeaderSpecifier(j, specifier)
206
+ );
207
+ const productsReactImport = findProductsReactImport(path);
208
+
209
+ if (productsReactImport) {
210
+ productsReactImport.node.specifiers = dedupeSpecifiers([
211
+ ...productsReactImport.node.specifiers,
212
+ ...specifiers,
213
+ ]);
214
+ j(path).remove();
215
+ } else {
216
+ path.node.source = j.literal(PRODUCTS_REACT_PATH);
217
+ path.node.specifiers = dedupeSpecifiers(specifiers);
218
+ }
219
+
220
+ return;
221
+ }
222
+
223
+ const rewrittenWebComponentsPath = rewriteWebComponentsPath(sourceValue);
224
+
225
+ if (rewrittenWebComponentsPath !== sourceValue) {
226
+ path.node.source = j.literal(rewrittenWebComponentsPath);
227
+ }
228
+ });
229
+
230
+ return root.toSource();
231
+ }
232
+
233
+ module.exports = transformer;