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 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
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;
57 changes: 57 additions & 0 deletions test/node/rewriteRelativeImportExtension.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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");
assert.equal(__rewriteRelativeImportExtension("./foo.TS"), "./foo.js");
assert.equal(__rewriteRelativeImportExtension("./foo.Ts"), "./foo.js");
assert.equal(__rewriteRelativeImportExtension("./foo/.hidden/foo.ts"), "./foo/.hidden/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");
assert.equal(__rewriteRelativeImportExtension("./foo.d.css.mts"), "./foo.d.css.mjs");
assert.equal(__rewriteRelativeImportExtension("./foo.d.tsx"), "./foo.d.js");
});

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");
assert.equal(__rewriteRelativeImportExtension("./foo.d.php?q=1.ts"), "./foo.d.php?q=1.ts");
});

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");
assert.equal(__rewriteRelativeImportExtension("./foo.D.ts"), "./foo.D.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;
10 changes: 10 additions & 0 deletions tslib.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ export function __disposeResources(env) {
return next();
}

export function __rewriteRelativeImportExtension(path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving one character probably won't matter too much, but this is equivalent

Suggested change
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
return tsx ? preserveJsx ? ".jsx" : ".js" : d && !(ext && cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");

});
}
return path;
}

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

export function __rewriteRelativeImportExtension(path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
});
}
return path;
}

export default {
__extends,
__assign,
Expand Down Expand Up @@ -379,4 +388,5 @@ export default {
__classPrivateFieldIn,
__addDisposableResource,
__disposeResources,
__rewriteRelativeImportExtension,
};
12 changes: 12 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,15 @@ var __disposeResources;
return next();
};

__rewriteRelativeImportExtension = function (path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
});
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
}
return path;
};

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

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