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

Add rewriteRelativeImportExtension helper #270

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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");
});
});
14 changes: 7 additions & 7 deletions tslib.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,17 @@ export function __disposeResources(env) {

export function __rewriteRelativeImportExtension(path, preserveJsx) {
if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) {
rbuckton marked this conversation as resolved.
Show resolved Hide resolved
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")) {
return path;
var extMatch = path.match(/\.[cm]?ts$/i);
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
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")) {
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 10 additions & 10 deletions tslib.es6.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,17 @@ 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.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
14 changes: 7 additions & 7 deletions tslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,17 @@ 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")) {
return path;
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.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