Skip to content

Commit

Permalink
fix: sanitize variable names for re-exports (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
petermuessig committed Jan 17, 2024
1 parent db5d809 commit f475dbc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/plugin/__test__/__snapshots__/test.js.snap
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions 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";
4 changes: 2 additions & 2 deletions packages/plugin/src/modules/visitor.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit f475dbc

Please sign in to comment.