diff --git a/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts b/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts index 519ab8503405b..a21ff17cf23b1 100644 --- a/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts +++ b/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts @@ -50,10 +50,9 @@ export class ImportExportTranspiler implements TranspilerInterface { } }); const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(declarations)]); - if (path.get('declaration')) { + if ('declaration' in path.node && path.node.declaration) { path.replaceWithMultiple([ - // @todo fix without any - (path.get('declaration')).node, + path.node.declaration, t.callExpression(t.identifier('addExport'), [ t.objectExpression( // @ts-ignore diff --git a/packages/cubejs-schema-compiler/test/unit/transpilers.test.ts b/packages/cubejs-schema-compiler/test/unit/transpilers.test.ts index 30e48a8a2eeb0..34a72fbd30378 100644 --- a/packages/cubejs-schema-compiler/test/unit/transpilers.test.ts +++ b/packages/cubejs-schema-compiler/test/unit/transpilers.test.ts @@ -1,4 +1,10 @@ +import { parse } from '@babel/parser'; +import babelGenerator from '@babel/generator'; +import babelTraverse from '@babel/traverse'; + import { prepareCompiler } from './PrepareCompiler'; +import { ImportExportTranspiler } from '../../src/compiler/transpilers'; +import { ErrorReporter } from '../../src/compiler/ErrorReporter'; describe('Transpilers', () => { it('CubeCheckDuplicatePropTranspiler', async () => { @@ -43,4 +49,36 @@ describe('Transpilers', () => { await compiler.compile(); }); + + it('ImportExportTranspiler', async () => { + const ieTranspiler = new ImportExportTranspiler(); + const errorsReport = new ErrorReporter(); + const code = ` + export const helperFunction = () => 'hello' + export { helperFunction as alias } + export default helperFunction + `; + const ast = parse( + code, + { + sourceFilename: 'code.js', + sourceType: 'module', + plugins: ['objectRestSpread'], + }, + ); + + babelTraverse(ast, ieTranspiler.traverseObject(errorsReport)); + const content = babelGenerator(ast, {}, code).code; + + expect(content).toEqual(`const helperFunction = () => 'hello'; +addExport({ + helperFunction: helperFunction +}) +addExport({ + alias: helperFunction +}); +setExport(helperFunction);`); + + errorsReport.throwIfAny(); // should not throw + }); });