Skip to content

Commit

Permalink
Case insensitivity, remove lookbehind
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Sep 24, 2024
1 parent 689efe3 commit e7b6aa0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
4 changes: 4 additions & 0 deletions test/node/rewriteRelativeImportExtension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ 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", () => {
assert.equal(__rewriteRelativeImportExtension("./foo.mts"), "./foo.mjs");
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", () => {
Expand Down Expand Up @@ -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");
});
});
13 changes: 7 additions & 6 deletions tslib.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
}
return path;
}
Expand Down
21 changes: 11 additions & 10 deletions tslib.es6.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,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") {
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")) {
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(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
}
return path;
}
Expand Down
13 changes: 7 additions & 6 deletions tslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,18 @@ var __rewriteRelativeImportExtension;

__rewriteRelativeImportExtension = function (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(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
}
return path;
};
Expand Down

0 comments on commit e7b6aa0

Please sign in to comment.