Skip to content

Commit b16e8dd

Browse files
fix(schema-compiler): ExportNamedDeclaration for variables object (#9390)
* fix(schema-compiler): ExportNamedDeclaration for variables object * small fix * add tests --------- Co-authored-by: Arkadiusz Świerczek <[email protected]>
1 parent a0d21dd commit b16e8dd

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ export class ImportExportTranspiler implements TranspilerInterface {
5050
}
5151
});
5252
const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(<t.ObjectProperty[]>declarations)]);
53-
if (path.get('declaration')) {
53+
if ('declaration' in path.node && path.node.declaration) {
5454
path.replaceWithMultiple([
55-
// @todo fix without any
56-
(<any>path.get('declaration')).node,
55+
path.node.declaration,
5756
t.callExpression(t.identifier('addExport'), [
5857
t.objectExpression(
5958
// @ts-ignore

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import { parse } from '@babel/parser';
2+
import babelGenerator from '@babel/generator';
3+
import babelTraverse from '@babel/traverse';
4+
15
import { prepareCompiler } from './PrepareCompiler';
6+
import { ImportExportTranspiler } from '../../src/compiler/transpilers';
7+
import { ErrorReporter } from '../../src/compiler/ErrorReporter';
28

39
describe('Transpilers', () => {
410
it('CubeCheckDuplicatePropTranspiler', async () => {
@@ -43,4 +49,36 @@ describe('Transpilers', () => {
4349

4450
await compiler.compile();
4551
});
52+
53+
it('ImportExportTranspiler', async () => {
54+
const ieTranspiler = new ImportExportTranspiler();
55+
const errorsReport = new ErrorReporter();
56+
const code = `
57+
export const helperFunction = () => 'hello'
58+
export { helperFunction as alias }
59+
export default helperFunction
60+
`;
61+
const ast = parse(
62+
code,
63+
{
64+
sourceFilename: 'code.js',
65+
sourceType: 'module',
66+
plugins: ['objectRestSpread'],
67+
},
68+
);
69+
70+
babelTraverse(ast, ieTranspiler.traverseObject(errorsReport));
71+
const content = babelGenerator(ast, {}, code).code;
72+
73+
expect(content).toEqual(`const helperFunction = () => 'hello';
74+
addExport({
75+
helperFunction: helperFunction
76+
})
77+
addExport({
78+
alias: helperFunction
79+
});
80+
setExport(helperFunction);`);
81+
82+
errorsReport.throwIfAny(); // should not throw
83+
});
4684
});

0 commit comments

Comments
 (0)