Skip to content

Commit 5e08ace

Browse files
committed
charts + simplify
1 parent ccbaef6 commit 5e08ace

File tree

1 file changed

+42
-45
lines changed
  • packages/cli/src/scripts/codemod/transforms/export-maps

1 file changed

+42
-45
lines changed
Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call */
2-
31
import fs from 'fs';
42
import path from 'path';
53
import type { API, FileInfo, JSCodeshift, Collection } from 'jscodeshift';
@@ -8,9 +6,9 @@ const mainPackageName = '@ui5/webcomponents-react';
86
const basePackageName = '@ui5/webcomponents-react-base';
97
const chartsPackageName = '@ui5/webcomponents-react-charts';
108

11-
const componentPackageNames = [mainPackageName, chartsPackageName];
9+
const packageNames = [mainPackageName, basePackageName, chartsPackageName];
1210

13-
// main enums
11+
// Enums for main package
1412
const libraryPath = require.resolve('@ui5/webcomponents-react/package.json');
1513
const enumsDir = path.join(path.dirname(libraryPath), 'dist', 'enums');
1614
let enumNames: Set<string> = new Set();
@@ -28,7 +26,7 @@ try {
2826
console.warn(`⚠️ Could not read enums directory at ${enumsDir}. Skipping enum detection.`, e);
2927
}
3028

31-
// exports-map base
29+
// Mapping functions
3230
function resolveBaseExport(importedName: string): string | undefined {
3331
const directMap: Record<string, string> = {
3432
Device: `${basePackageName}/Device`,
@@ -42,13 +40,26 @@ function resolveBaseExport(importedName: string): string | undefined {
4240
utils: `${basePackageName}/utils`,
4341
addCustomCSSWithScoping: `${basePackageName}/internal/addCustomCSSWithScoping.js`,
4442
};
45-
if (directMap[importedName]) {
46-
return directMap[importedName];
47-
}
48-
// fallback
49-
if (importedName === 'default' || importedName === 'index') {
50-
return basePackageName;
51-
}
43+
if (directMap[importedName]) return directMap[importedName];
44+
if (importedName === 'default' || importedName === 'index') return basePackageName;
45+
return undefined;
46+
}
47+
48+
function resolveChartsExport(importedName: string): string | undefined {
49+
const directMap: Record<string, string> = {
50+
TimelineChartAnnotation: `${chartsPackageName}/TimelineChartAnnotation`,
51+
BarChartPlaceholder: `${chartsPackageName}/BarChartPlaceholder`,
52+
BulletChartPlaceholder: `${chartsPackageName}/BulletChartPlaceholder`,
53+
ColumnChartPlaceholder: `${chartsPackageName}/ColumnChartPlaceholder`,
54+
ColumnChartWithTrendPlaceholder: `${chartsPackageName}/ColumnChartWithTrendPlaceholder`,
55+
ComposedChartPlaceholder: `${chartsPackageName}/ComposedChartPlaceholder`,
56+
LineChartPlaceholder: `${chartsPackageName}/LineChartPlaceholder`,
57+
PieChartPlaceholder: `${chartsPackageName}/PieChartPlaceholder`,
58+
ScatterChartPlaceholder: `${chartsPackageName}/ScatterChartPlaceholder`,
59+
TimelineChartPlaceholder: `${chartsPackageName}/TimelineChartPlaceholder`,
60+
};
61+
if (directMap[importedName]) return directMap[importedName];
62+
if (importedName === 'default' || importedName === 'index') return chartsPackageName;
5263
return undefined;
5364
}
5465

@@ -58,9 +69,8 @@ export default function transform(file: FileInfo, api: API): string | undefined
5869

5970
let isDirty = false;
6071

61-
// main & charts pkg
62-
componentPackageNames.forEach((packageName) => {
63-
root.find(j.ImportDeclaration, { source: { value: packageName } }).forEach((importPath) => {
72+
packageNames.forEach((pkg) => {
73+
root.find(j.ImportDeclaration, { source: { value: pkg } }).forEach((importPath) => {
6474
const specifiers = importPath.node.specifiers || [];
6575
specifiers.forEach((spec) => {
6676
if (spec.type !== 'ImportSpecifier') return;
@@ -69,19 +79,29 @@ export default function transform(file: FileInfo, api: API): string | undefined
6979
if (importPath.node.importKind === 'type') {
7080
if (importedName.endsWith('PropTypes')) {
7181
componentName = importedName.replace(/PropTypes$/, '');
72-
// charts props
7382
} else if (importedName.endsWith('Props')) {
7483
componentName = importedName.replace(/Props$/, '');
7584
} else if (importedName.endsWith('DomRef')) {
7685
componentName = importedName.replace(/DomRef$/, '');
7786
}
7887
}
79-
const newSource =
80-
importPath.node.importKind === 'type'
81-
? `${packageName}/${componentName}`
82-
: enumNames.has(importedName)
83-
? `${packageName}/enums/${importedName}`
84-
: `${packageName}/${importedName}`;
88+
89+
let newSource: string;
90+
if (pkg === mainPackageName) {
91+
newSource =
92+
importPath.node.importKind === 'type'
93+
? `${mainPackageName}/${componentName}`
94+
: enumNames.has(importedName)
95+
? `${mainPackageName}/enums/${importedName}`
96+
: `${mainPackageName}/${importedName}`;
97+
} else if (pkg === basePackageName) {
98+
newSource = resolveBaseExport(importedName) || basePackageName;
99+
} else if (pkg === chartsPackageName) {
100+
newSource = resolveChartsExport(componentName) || `${chartsPackageName}/${componentName}`;
101+
} else {
102+
newSource = pkg;
103+
}
104+
85105
const newImport = j.importDeclaration(
86106
[
87107
j.importSpecifier(
@@ -99,28 +119,5 @@ export default function transform(file: FileInfo, api: API): string | undefined
99119
});
100120
});
101121

102-
// base pkg
103-
root.find(j.ImportDeclaration, { source: { value: basePackageName } }).forEach((importPath) => {
104-
const specifiers = importPath.node.specifiers || [];
105-
specifiers.forEach((spec) => {
106-
if (spec.type !== 'ImportSpecifier') return;
107-
const importedName = spec.imported.name as string;
108-
const newSource = resolveBaseExport(importedName) || basePackageName;
109-
const newImport = j.importDeclaration(
110-
[
111-
j.importSpecifier(
112-
j.identifier(importedName),
113-
j.identifier(spec.local && typeof spec.local.name === 'string' ? spec.local.name : importedName),
114-
),
115-
],
116-
j.literal(newSource),
117-
);
118-
newImport.importKind = importPath.node.importKind;
119-
j(importPath).insertBefore(newImport);
120-
isDirty = true;
121-
});
122-
j(importPath).remove();
123-
});
124-
125122
return isDirty ? root.toSource({ quote: 'single' }) : undefined;
126123
}

0 commit comments

Comments
 (0)