From e7b6aa0a882601b7352edfb3799c92d85006aaab Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 09:32:46 -0700 Subject: [PATCH] Case insensitivity, remove lookbehind --- .../rewriteRelativeImportExtension.test.js | 4 ++++ tslib.es6.js | 13 ++++++------ tslib.es6.mjs | 21 ++++++++++--------- tslib.js | 13 ++++++------ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/test/node/rewriteRelativeImportExtension.test.js b/test/node/rewriteRelativeImportExtension.test.js index 772d173..25158e1 100644 --- a/test/node/rewriteRelativeImportExtension.test.js +++ b/test/node/rewriteRelativeImportExtension.test.js @@ -7,6 +7,8 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.ts"), "./foo.js"); assert.equal(__rewriteRelativeImportExtension("../foo.ts"), "../foo.js"); assert.equal(__rewriteRelativeImportExtension("../../foo.ts"), "../../foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo.TS"), "./foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo.Ts"), "./foo.js"); }); test("rewrites other TypeScript extensions", () => { @@ -14,6 +16,7 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.cts"), "./foo.cjs"); assert.equal(__rewriteRelativeImportExtension("./foo.tsx"), "./foo.js"); assert.equal(__rewriteRelativeImportExtension("./foo.tsx", true), "./foo.jsx"); + assert.equal(__rewriteRelativeImportExtension("./foo.Tsx", true), "./foo.jsx"); }); test("does not rewrite other extensions", () => { @@ -45,5 +48,6 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.d.mts"), "./foo.d.mts"); assert.equal(__rewriteRelativeImportExtension("./foo.d.cts"), "./foo.d.cts"); assert.equal(__rewriteRelativeImportExtension("./foo.d.css.ts"), "./foo.d.css.ts"); + assert.equal(__rewriteRelativeImportExtension("./foo.D.ts"), "./foo.D.ts"); }); }); diff --git a/tslib.es6.js b/tslib.es6.js index a1323a6..36b58a5 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -350,17 +350,18 @@ export function __disposeResources(env) { export function __rewriteRelativeImportExtension(path, preserveJsx) { if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) { - if (path.substring(path.length - 4) === ".tsx") { + if (path.substring(path.length - 4).toLowerCase() === ".tsx") { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } - if (path.substring(path.length - 3) === ".ts") { - var dot = path.lastIndexOf(".", path.length - 4); - if (dot >= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + var extMatch = path.match(/\.[cm]?ts$/i); + if (extMatch) { + var ext = extMatch[0].toLowerCase(); + var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); + if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { return path; } - return path.substring(0, path.length - 3) + ".js"; + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.replace(/(?= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { - return path; + if (path.substring(path.length - 4).toLowerCase() === ".tsx") { + return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); + } + var extMatch = path.match(/\.[cm]?ts$/i); + if (extMatch) { + var ext = extMatch[0].toLowerCase(); + var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); + if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { + return path; + } + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.substring(0, path.length - 3) + ".js"; - } - return path.replace(/(?= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + var extMatch = path.match(/\.[cm]?ts$/i); + if (extMatch) { + var ext = extMatch[0].toLowerCase(); + var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); + if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { return path; } - return path.substring(0, path.length - 3) + ".js"; + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.replace(/(?