From 9e4f3371a6fd11c1ebb21fa8ff29ca3771c88b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20=C5=9Awierczek?= <109723953+arkadiusz-swierczek-pm@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:22:12 +0200 Subject: [PATCH 1/3] fix(schema-compiler): ExportNamedDeclaration for variables object --- .../src/compiler/transpilers/ImportExportTranspiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts b/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts index 519ab8503405b..52de714af67e7 100644 --- a/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts +++ b/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts @@ -50,7 +50,7 @@ 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, From b1c8cf0f1c3565a979a2c5bd8782df12781448c5 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Wed, 26 Mar 2025 11:26:26 +0200 Subject: [PATCH 2/3] small fix --- .../src/compiler/transpilers/ImportExportTranspiler.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts b/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts index 52de714af67e7..a21ff17cf23b1 100644 --- a/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts +++ b/packages/cubejs-schema-compiler/src/compiler/transpilers/ImportExportTranspiler.ts @@ -52,8 +52,7 @@ export class ImportExportTranspiler implements TranspilerInterface { const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(declarations)]); 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 From 58aa81e3c2fc69740e456ac98f8c158bf25cee81 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Tue, 8 Apr 2025 21:24:58 +0300 Subject: [PATCH 3/3] add tests --- .../test/unit/transpilers.test.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) 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 + }); });