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 1 commit
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
1 change: 1 addition & 0 deletions modules/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export {
__createBinding,
__addDisposableResource,
__disposeResources,
__rewriteRelativeImportExtension,
} from '../tslib.js';
export * as default from '../tslib.js';
2 changes: 2 additions & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
__classPrivateFieldIn,
__addDisposableResource,
__disposeResources,
__rewriteRelativeImportExtension,
} = tslib;
export {
__extends,
Expand Down Expand Up @@ -64,5 +65,6 @@ export {
__classPrivateFieldIn,
__addDisposableResource,
__disposeResources,
__rewriteRelativeImportExtension,
};
export default tslib;
49 changes: 49 additions & 0 deletions test/node/rewriteRelativeImportExtension.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import assert from "node:assert";
import { test } from "node:test";
import { testHelper } from "./testHelper.js";

testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension => {
test("rewrites relative .ts to .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");
});

test("does not rewrite other extensions", () => {
assert.equal(__rewriteRelativeImportExtension("./foo.js"), "./foo.js");
assert.equal(__rewriteRelativeImportExtension("./foo.mjs"), "./foo.mjs");
assert.equal(__rewriteRelativeImportExtension("./foo.cjs"), "./foo.cjs");
assert.equal(__rewriteRelativeImportExtension("./foo.jsx"), "./foo.jsx");
assert.equal(__rewriteRelativeImportExtension("./foo.json"), "./foo.json");
assert.equal(__rewriteRelativeImportExtension("./foo.css"), "./foo.css");
assert.equal(__rewriteRelativeImportExtension("./foo"), "./foo");
});

test("does not rewrite non-relative imports", () => {
assert.equal(__rewriteRelativeImportExtension("foo.ts"), "foo.ts");
assert.equal(__rewriteRelativeImportExtension("foo.mts"), "foo.mts");
assert.equal(__rewriteRelativeImportExtension("foo.cts"), "foo.cts");
assert.equal(__rewriteRelativeImportExtension("foo.tsx"), "foo.tsx");
assert.equal(__rewriteRelativeImportExtension("foo.js"), "foo.js");
assert.equal(__rewriteRelativeImportExtension("foo.mjs"), "foo.mjs");
assert.equal(__rewriteRelativeImportExtension("foo.cjs"), "foo.cjs");
assert.equal(__rewriteRelativeImportExtension("foo.jsx"), "foo.jsx");
assert.equal(__rewriteRelativeImportExtension("foo.json"), "foo.json");
assert.equal(__rewriteRelativeImportExtension("foo.css"), "foo.css");
assert.equal(__rewriteRelativeImportExtension("foo"), "foo");
});

test("does not rewrite declaration file extensions", () => {
assert.equal(__rewriteRelativeImportExtension("./foo.d.ts"), "./foo.d.ts");
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");
});
});
7 changes: 7 additions & 0 deletions tslib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,10 @@ export declare function __addDisposableResource<T>(env: { stack: { value?: unkno
* @seealso {@link __addDisposableResource}
*/
export declare function __disposeResources(env: { stack: { value?: unknown, dispose?: Function, async: boolean }[]; error: unknown; hasError: boolean; }): any;

/**
* Transforms a relative import specifier ending in a non-declaration TypeScript file extension to its JavaScript file extension counterpart.
* @param path The import specifier.
* @param preserveJsx Causes '*.tsx' to transform to '*.jsx' instead of '*.js'. Should be true when `--jsx` is set to `preserve`.
*/
export declare function __rewriteRelativeImportExtension(path: string, preserveJsx?: boolean): string;
18 changes: 18 additions & 0 deletions tslib.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,23 @@ export function __disposeResources(env) {
return next();
}

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") {
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;
}
return path.substring(0, path.length - 3) + ".js";
}
return path.replace(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
}
return path;
}

export default {
__extends: __extends,
__assign: __assign,
Expand Down Expand Up @@ -380,4 +397,5 @@ export default {
__classPrivateFieldIn: __classPrivateFieldIn,
__addDisposableResource: __addDisposableResource,
__disposeResources: __disposeResources,
__rewriteRelativeImportExtension: __rewriteRelativeImportExtension,
};
18 changes: 18 additions & 0 deletions tslib.es6.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,23 @@ export function __disposeResources(env) {
return next();
}

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;
}
return path.substring(0, path.length - 3) + ".js";
}
return path.replace(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
}
return path;
}

export default {
__extends,
__assign,
Expand Down Expand Up @@ -379,4 +396,5 @@ export default {
__classPrivateFieldIn,
__addDisposableResource,
__disposeResources,
__rewriteRelativeImportExtension,
};
20 changes: 20 additions & 0 deletions tslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var __classPrivateFieldIn;
var __createBinding;
var __addDisposableResource;
var __disposeResources;
var __rewriteRelativeImportExtension;
(function (factory) {
var root = typeof global === "object" ? global : typeof self === "object" ? self : typeof this === "object" ? this : {};
if (typeof define === "function" && define.amd) {
Expand Down Expand Up @@ -395,6 +396,23 @@ var __disposeResources;
return next();
};

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

exporter("__extends", __extends);
exporter("__assign", __assign);
exporter("__rest", __rest);
Expand Down Expand Up @@ -426,6 +444,7 @@ var __disposeResources;
exporter("__classPrivateFieldIn", __classPrivateFieldIn);
exporter("__addDisposableResource", __addDisposableResource);
exporter("__disposeResources", __disposeResources);
exporter("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension);
});

0 && (module.exports = {
Expand Down Expand Up @@ -460,4 +479,5 @@ var __disposeResources;
__classPrivateFieldIn,
__addDisposableResource,
__disposeResources,
__rewriteRelativeImportExtension,
});