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

fix: avoid URL illegal constructor error #458

Merged
merged 4 commits into from
Dec 19, 2024

Conversation

onsclom
Copy link
Contributor

@onsclom onsclom commented Dec 18, 2024

nodeFileTrace errors on const URLParser = typeof window === 'undefined' ? URL : 'b' with TypeError: Class constructor URL cannot be invoked without 'new'.

This is because this code:

async function ConditionalExpression() {
  return {
    test: 'foo',
    then: URL,
    else: 'bar',
  };
}
await ConditionalExpression();

calls URL() since ConditionalExpression is an async function returning a thenable.

Solution: rename all .then fields to .ifTrue since then is reserved for thenables.

Closes #447

@onsclom onsclom requested review from ijjk, styfle and a team as code owners December 18, 2024 23:03
src/test.ts Outdated Show resolved Hide resolved
@@ -327,7 +327,7 @@ const visitors: Record<

return {
test: node.test,
then: thenValue.value,
then: thenValue.value === URL ? undefined : thenValue.value,
Copy link
Member

Choose a reason for hiding this comment

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

This looks wrong. Why would URL always be undefined?

I suspect the root cause is somewhere else since this code represents the ternary from what I can tell.

@@ -327,7 +327,7 @@ const visitors: Record<

return {
test: node.test,
then: thenValue.value,
then: thenValue.value === URL ? undefined : thenValue.value,
Copy link
Member

Choose a reason for hiding this comment

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

This will be confusing for future readers. Can we add a comment explaining why the special case here?

Comment on lines 9 to 14
export interface ConditionalValue {
test: string;
then: any;
// then is reserved for thenables
ifTrue: any;
else: any;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went with a new (and I think better) solution!

then is reservered for thenables, so changed everything in the code from then -> ifTrue.

Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

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

Wow, nice catch!

src/utils/types.ts Outdated Show resolved Hide resolved
@onsclom onsclom merged commit 5777c8b into main Dec 19, 2024
17 checks passed
@onsclom onsclom deleted the austinmerrick/fix-url-illegal-constructor branch December 19, 2024 00:56
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.

3 participants