-
Notifications
You must be signed in to change notification settings - Fork 132
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
Conversation
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
?
There was a problem hiding this comment.
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.
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"); |
There was a problem hiding this comment.
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
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"); |
Counterpart to microsoft/TypeScript#59767