Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add rewriteRelativeImportExtension helper - #270

#270
Merged
andrewbranch merged 9 commits into
mainmicrosoft/tslib:mainfrom
rewriteRelativeImportExtensionmicrosoft/tslib:rewriteRelativeImportExtensionCopy head branch name to clipboard
Oct 14, 2024
Merged

Add rewriteRelativeImportExtension helper#270
andrewbranch merged 9 commits into
mainmicrosoft/tslib:mainfrom
rewriteRelativeImportExtensionmicrosoft/tslib:rewriteRelativeImportExtensionCopy head branch name to clipboard

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Comment thread tslib.es6.js Outdated
Comment thread tslib.es6.js Outdated
Comment thread tslib.es6.js Outdated
Comment thread tslib.es6.js Outdated
Comment thread tslib.es6.js Outdated
Comment thread tslib.es6.js Outdated
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 || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js");

@rbuckton rbuckton Sep 24, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

To reduce the amount of || "" that you're doing, you could move the quantifiers inside of the capture groups so that they capture empty strings instead of undefined:

/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i

Also, to clarify the d && (ext && !cm || !ext) case: is this saying that d.ts (no ext and no cm) remains .d.ts, .d.cts (no ext but has cm) remains .d.cts, but .d.css.cts (has ext and cm) becomes .d.css.cjs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that’s right. .d.css.cts is not a recognized declaration file extension (even though it looks suspiciously like one), so we would have to treat it as any other *.cts file name. A real fishy edge case, but correct for today’s compiler, anyway.

Comment thread tslib.js
Comment thread tslib.es6.js
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
Copy Markdown
Contributor

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");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.