Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/converter/convert-to-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ interface PluginArguments {
types: typeof types;
}

// Rewrites a `@cloudscape-design/.../dom` module path to `.../selectors`.
function rewriteDomSourceToSelectors(source: NodePath<types.StringLiteral>, 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: {
ImportDeclaration(path: NodePath<types.ImportDeclaration>) {
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) {
Expand All @@ -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<types.ExportNamedDeclaration>) {
if (path.node.source) {
rewriteDomSourceToSelectors(path.get('source') as NodePath<types.StringLiteral>, t);
}
},
ExportAllDeclaration(path: NodePath<types.ExportAllDeclaration>) {
rewriteDomSourceToSelectors(path.get('source'), t);
},
ClassDeclaration(path: NodePath<types.ClassDeclaration>) {
// our wrapper classes have generic parameters only in DOM version
if (ourWrappers.includes((path.node.superClass as any)?.name) && path.node.superTypeParameters) {
Expand Down
15 changes: 15 additions & 0 deletions src/converter/test/__snapshots__/converter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Loading