Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize variable names for re-exports #122

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/plugin/__test__/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading