From c9c9e2e52d3f56993047709fc3e9a491d940d49d Mon Sep 17 00:00:00 2001 From: Brandon Vandegrift Date: Thu, 16 Jul 2026 19:25:18 +0000 Subject: [PATCH] fix: rewrite re-export sources to selectors --- src/converter/convert-to-selectors.ts | 22 +++++++++++++++---- .../test/__snapshots__/converter.test.ts.snap | 15 +++++++++++++ .../converter-no-typecheck/reexport-source.ts | 12 ++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/converter/test/inputs/converter-no-typecheck/reexport-source.ts diff --git a/src/converter/convert-to-selectors.ts b/src/converter/convert-to-selectors.ts index 3e84491..b820a00 100644 --- a/src/converter/convert-to-selectors.ts +++ b/src/converter/convert-to-selectors.ts @@ -10,6 +10,14 @@ interface PluginArguments { types: typeof types; } +// Rewrites a `@cloudscape-design/.../dom` module path to `.../selectors`. +function rewriteDomSourceToSelectors(source: NodePath, t: typeof types) { + const value = source.node.value; + if (value.startsWith('@cloudscape-design/')) { + source.replaceWith(t.stringLiteral(value.replace(/\b\/dom\b/, '/selectors'))); + } +} + function selectorUtilsGenerator({ types: t }: PluginArguments): PluginObj { return { visitor: { @@ -17,10 +25,7 @@ function selectorUtilsGenerator({ types: t }: PluginArguments): PluginObj { const source = path.get('source'); // Rewrite import path @cloudscape-design/.../dom -> @cloudscape-design/.../selectors - if (source.node.value.startsWith('@cloudscape-design/')) { - const newImportPath = source.node.value.replace(/\b\/dom\b/, '/selectors'); - source.replaceWith(t.stringLiteral(newImportPath)); - } + rewriteDomSourceToSelectors(source, t); // Remove @usesDom decorator if (source.node.value === runtimeSelectorsPath) { @@ -35,6 +40,15 @@ function selectorUtilsGenerator({ types: t }: PluginArguments): PluginObj { path.remove(); } }, + // Re-exports need the same rewrite as imports. Local exports have no source. + ExportNamedDeclaration(path: NodePath) { + if (path.node.source) { + rewriteDomSourceToSelectors(path.get('source') as NodePath, t); + } + }, + ExportAllDeclaration(path: NodePath) { + rewriteDomSourceToSelectors(path.get('source'), t); + }, ClassDeclaration(path: NodePath) { // our wrapper classes have generic parameters only in DOM version if (ourWrappers.includes((path.node.superClass as any)?.name) && path.node.superTypeParameters) { diff --git a/src/converter/test/__snapshots__/converter.test.ts.snap b/src/converter/test/__snapshots__/converter.test.ts.snap index df9476c..112095a 100644 --- a/src/converter/test/__snapshots__/converter.test.ts.snap +++ b/src/converter/test/__snapshots__/converter.test.ts.snap @@ -18,6 +18,21 @@ export default class DummyWrapper extends ComponentWrapper { }" `; +exports[`reexport-source 1`] = ` +"// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Proxied wrappers are often written as re-exports, so the \`/dom\` -> \`/selectors\` +// rewrite has to apply to re-export sources too, not just imports. +export { default } from "@cloudscape-design/components/test-utils/selectors/button"; +export * from "@cloudscape-design/components/test-utils/selectors/container"; + +// Local exports have no module source, so they stay as-is. +const value = 1; +export { value }; +export default value;" +`; + exports[`simple 1`] = ` "// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 diff --git a/src/converter/test/inputs/converter-no-typecheck/reexport-source.ts b/src/converter/test/inputs/converter-no-typecheck/reexport-source.ts new file mode 100644 index 0000000..59cdb7f --- /dev/null +++ b/src/converter/test/inputs/converter-no-typecheck/reexport-source.ts @@ -0,0 +1,12 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Proxied wrappers are often written as re-exports, so the `/dom` -> `/selectors` +// rewrite has to apply to re-export sources too, not just imports. +export { default } from '@cloudscape-design/components/test-utils/dom/button'; +export * from '@cloudscape-design/components/test-utils/dom/container'; + +// Local exports have no module source, so they stay as-is. +const value = 1; +export { value }; +export default value;