From 46886c80f8657df19af23c52b66cea2834f452fa Mon Sep 17 00:00:00 2001 From: Peter Muessig Date: Wed, 17 Jan 2024 13:13:36 +0100 Subject: [PATCH] fix: sanitize variable names for re-exports --- .../__test__/__snapshots__/test.js.snap | 25 +++++++++++++++++++ .../fixtures/typescript/ts-re-export.ts | 3 +++ packages/plugin/src/modules/visitor.js | 4 +-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 packages/plugin/__test__/fixtures/typescript/ts-re-export.ts diff --git a/packages/plugin/__test__/__snapshots__/test.js.snap b/packages/plugin/__test__/__snapshots__/test.js.snap index 5f82676..26bd71e 100644 --- a/packages/plugin/__test__/__snapshots__/test.js.snap +++ b/packages/plugin/__test__/__snapshots__/test.js.snap @@ -1875,6 +1875,31 @@ exports[`typescript ts-export-type.ts 1`] = ` });" `; +exports[`typescript ts-re-export.ts 1`] = ` +"sap.ui.define(["module", "./having-a-dash", "path/having-a-dash"], function (__module, ___having_a_dash, __path_having_a_dash) { + "use strict"; + + var __exports = { + __esModule: true + }; + function extendExports(exports, obj) { + Object.keys(obj).forEach(function (key) { + if (key === "default" || key === "__esModule") return; + Object.defineProperty(exports, key, { + enumerable: true, + get: function get() { + return obj[key]; + } + }); + }); + } + extendExports(__exports, __module); + extendExports(__exports, ___having_a_dash); + extendExports(__exports, __path_having_a_dash); + return __exports; +});" +`; + exports[`typescript-preset-env ts-class-anonymous.ts 1`] = ` "sap.ui.define(["sap/Class"], function (SAPClass) { "use strict"; diff --git a/packages/plugin/__test__/fixtures/typescript/ts-re-export.ts b/packages/plugin/__test__/fixtures/typescript/ts-re-export.ts new file mode 100644 index 0000000..893b1cd --- /dev/null +++ b/packages/plugin/__test__/fixtures/typescript/ts-re-export.ts @@ -0,0 +1,3 @@ +export * from "module"; +export * from "./having-a-dash"; +export * from "path/having-a-dash"; diff --git a/packages/plugin/src/modules/visitor.js b/packages/plugin/src/modules/visitor.js index a7b0c82..c2a701c 100644 --- a/packages/plugin/src/modules/visitor.js +++ b/packages/plugin/src/modules/visitor.js @@ -4,9 +4,9 @@ import * as ast from "../utils/ast"; import { hasJsdocGlobalExportFlag } from "../classes/helpers/jsdoc"; -const tempModuleName = (name) => `__${name}`; const cleanImportSource = (src) => src.replace(/(\/)|(-)|(@)/g, "_").replace(/\./g, ""); +const tempModuleName = (name) => `__${name}`; const hasGlobalExportFlag = (node) => hasJsdocGlobalExportFlag(node); const addModuleImport = (imports, name) => { const existingImport = imports.find((imp) => imp.src === name); @@ -269,7 +269,7 @@ export const ModuleTransformVisitor = { ExportAllDeclaration(path) { const src = path.node.source.value; - const name = src.replace(/\//g, "_").replace(/\./g, ""); + const name = cleanImportSource(src); const tmpName = tempModuleName(name); this.imports.push({ src, name, tmpName });