Skip to content

fix(schema-compiler): Support for export function xxx() during transpilation #9645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,56 @@ export class ImportExportTranspiler implements TranspilerInterface {
});
}
});
const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(<t.ObjectProperty[]>declarations)]);

if ('declaration' in path.node && path.node.declaration) {
path.replaceWithMultiple([
path.node.declaration,
t.callExpression(t.identifier('addExport'), [
t.objectExpression(
// @ts-ignore
path.get('declaration').get('declarations').map(d => t.objectProperty(
d.get('id').node,
d.get('id').node
))
)
])
]);
} else {
path.replaceWith(addExportCall);
const decl = path.get('declaration');

// If its FunctionDeclaration or ClassDeclaration
if (
t.isFunctionDeclaration(decl.node) ||
t.isClassDeclaration(decl.node)
) {
const name = decl.node.id;
if (!name) {
reporter.syntaxError({
message: 'Exported function/class must have a name',
loc: decl.node.loc,
});
return;
}

path.replaceWithMultiple([
decl.node,
t.callExpression(t.identifier('addExport'), [
t.objectExpression([t.objectProperty(name, name)])
])
]);
return;
}

// VariableDeclaration (export const foo = ...)
if (t.isVariableDeclaration(decl.node)) {
path.replaceWithMultiple([
decl.node,
t.callExpression(t.identifier('addExport'), [
t.objectExpression(
// @ts-ignore
decl.get('declarations').map(d => t.objectProperty(d.get('id').node, d.get('id').node))
)
])
]);
return;
}

reporter.syntaxError({
message: `Unsupported export declaration of type '${decl.node?.type}'`,
loc: decl.node?.loc,
});
return;
}

const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(<t.ObjectProperty[]>declarations)]);
path.replaceWith(addExportCall);
},
ExportDefaultDeclaration(path) {
// @ts-ignore
Expand Down
16 changes: 15 additions & 1 deletion packages/cubejs-schema-compiler/test/unit/transpilers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ describe('Transpilers', () => {
export const helperFunction = () => 'hello'
export { helperFunction as alias }
export default helperFunction
export function requireFilterParam() {
return 'required';
}
export const someVar = 42
`;
const ast = parse(
code,
Expand All @@ -77,7 +81,17 @@ addExport({
addExport({
alias: helperFunction
});
setExport(helperFunction);`);
setExport(helperFunction);
function requireFilterParam() {
return 'required';
}
addExport({
requireFilterParam: requireFilterParam
})
const someVar = 42;
addExport({
someVar: someVar
})`);

errorsReport.throwIfAny(); // should not throw
});
Expand Down
Loading