Skip to content

Commit 9bcb5a1

Browse files
authored
fix(schema-compiler): Support for export function xxx() during transpilation (#9645)
1 parent 24abdea commit 9bcb5a1

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,56 @@ export class ImportExportTranspiler implements TranspilerInterface {
4949
});
5050
}
5151
});
52-
const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(<t.ObjectProperty[]>declarations)]);
52+
5353
if ('declaration' in path.node && path.node.declaration) {
54-
path.replaceWithMultiple([
55-
path.node.declaration,
56-
t.callExpression(t.identifier('addExport'), [
57-
t.objectExpression(
58-
// @ts-ignore
59-
path.get('declaration').get('declarations').map(d => t.objectProperty(
60-
d.get('id').node,
61-
d.get('id').node
62-
))
63-
)
64-
])
65-
]);
66-
} else {
67-
path.replaceWith(addExportCall);
54+
const decl = path.get('declaration');
55+
56+
// If its FunctionDeclaration or ClassDeclaration
57+
if (
58+
t.isFunctionDeclaration(decl.node) ||
59+
t.isClassDeclaration(decl.node)
60+
) {
61+
const name = decl.node.id;
62+
if (!name) {
63+
reporter.syntaxError({
64+
message: 'Exported function/class must have a name',
65+
loc: decl.node.loc,
66+
});
67+
return;
68+
}
69+
70+
path.replaceWithMultiple([
71+
decl.node,
72+
t.callExpression(t.identifier('addExport'), [
73+
t.objectExpression([t.objectProperty(name, name)])
74+
])
75+
]);
76+
return;
77+
}
78+
79+
// VariableDeclaration (export const foo = ...)
80+
if (t.isVariableDeclaration(decl.node)) {
81+
path.replaceWithMultiple([
82+
decl.node,
83+
t.callExpression(t.identifier('addExport'), [
84+
t.objectExpression(
85+
// @ts-ignore
86+
decl.get('declarations').map(d => t.objectProperty(d.get('id').node, d.get('id').node))
87+
)
88+
])
89+
]);
90+
return;
91+
}
92+
93+
reporter.syntaxError({
94+
message: `Unsupported export declaration of type '${decl.node?.type}'`,
95+
loc: decl.node?.loc,
96+
});
97+
return;
6898
}
99+
100+
const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(<t.ObjectProperty[]>declarations)]);
101+
path.replaceWith(addExportCall);
69102
},
70103
ExportDefaultDeclaration(path) {
71104
// @ts-ignore

packages/cubejs-schema-compiler/test/unit/transpilers.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ describe('Transpilers', () => {
5757
export const helperFunction = () => 'hello'
5858
export { helperFunction as alias }
5959
export default helperFunction
60+
export function requireFilterParam() {
61+
return 'required';
62+
}
63+
export const someVar = 42
6064
`;
6165
const ast = parse(
6266
code,
@@ -77,7 +81,17 @@ addExport({
7781
addExport({
7882
alias: helperFunction
7983
});
80-
setExport(helperFunction);`);
84+
setExport(helperFunction);
85+
function requireFilterParam() {
86+
return 'required';
87+
}
88+
addExport({
89+
requireFilterParam: requireFilterParam
90+
})
91+
const someVar = 42;
92+
addExport({
93+
someVar: someVar
94+
})`);
8195

8296
errorsReport.throwIfAny(); // should not throw
8397
});

0 commit comments

Comments
 (0)