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

Polyfill: clarify error message about why valueOf throws #2667

Merged
merged 1 commit into from
Aug 31, 2023

Conversation

justingrant
Copy link
Collaborator

We've received enough user confusion about valueOf throwing (see #1681 for why we throw) that I think it's worth clarifying the error message.

@justingrant justingrant added non-prod-polyfill THIS POLYFILL IS NOT FOR PRODUCTION USE! no-spec-text PR can be ignored by implementors labels Aug 26, 2023
@justingrant justingrant requested a review from ptomato August 26, 2023 01:06
@codecov
Copy link

codecov bot commented Aug 26, 2023

Codecov Report

Merging #2667 (946a9a4) into main (678fdd1) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #2667   +/-   ##
=======================================
  Coverage   96.07%   96.08%           
=======================================
  Files          20       20           
  Lines       11706    11723   +17     
  Branches     2186     2188    +2     
=======================================
+ Hits        11247    11264   +17     
  Misses        395      395           
  Partials       64       64           
Files Changed Coverage Δ
polyfill/lib/duration.mjs 94.71% <100.00%> (ø)
polyfill/lib/ecmascript.mjs 98.40% <100.00%> (+<0.01%) ⬆️
polyfill/lib/instant.mjs 96.90% <100.00%> (ø)
polyfill/lib/plaindate.mjs 99.67% <100.00%> (ø)
polyfill/lib/plaindatetime.mjs 97.23% <100.00%> (ø)
polyfill/lib/plainmonthday.mjs 97.50% <100.00%> (ø)
polyfill/lib/plaintime.mjs 98.21% <100.00%> (ø)
polyfill/lib/plainyearmonth.mjs 98.27% <100.00%> (ø)
polyfill/lib/zoneddatetime.mjs 100.00% <100.00%> (ø)

Copy link
Collaborator

@ptomato ptomato left a comment

Choose a reason for hiding this comment

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

Nice.

@@ -5726,6 +5726,21 @@ export function ASCIILowercase(str) {
]);
}

export function ValueOfThrows(constructorName) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't ecmascript.mjs intended to correspond with operations that are in current ECMA-262? It seems like the wrong home for this function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ecmascript.mjs is usually where we put all shared code. Unlike the spec files where there's mainadditions.html for the existing 262 overrides, in the polyfill things are all mixed together. That said, this function isn't in the spec so I'll add a comment explaining why it exists.

Comment on lines +5735 to +5743
throw new TypeError(
'Do not use built-in arithmetic operators with Temporal objects. ' +
`When comparing, use ${compareCode}, not obj1 > obj2. ` +
"When coercing to strings, use obj.toString() or String(obj), not +obj nor '' + obj. " +
'When concatenating with strings, use str.concat(obj) or `${str}${obj}`, not str + obj. ' +
'In React, call obj.toString() before rendering a Temporal object.'
);
Copy link
Collaborator

@gibson042 gibson042 Aug 26, 2023

Choose a reason for hiding this comment

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

That's a lot of quote variance. For sanity, I think maybe just ` with post-processing would be better.

Suggested change
throw new TypeError(
'Do not use built-in arithmetic operators with Temporal objects. ' +
`When comparing, use ${compareCode}, not obj1 > obj2. ` +
"When coercing to strings, use obj.toString() or String(obj), not +obj nor '' + obj. " +
'When concatenating with strings, use str.concat(obj) or `${str}${obj}`, not str + obj. ' +
'In React, call obj.toString() before rendering a Temporal object.'
);
const rawMessage = `
Do not use built-in arithmetic operators with Temporal objects.
When comparing, use ${compareCode} rather than obj1 > obj2.
When coercing to string, use obj.toString() or String(obj) rather than '' + obj.
When concatenating to a string, use str.concat(obj) or \`\${str}\${obj}\` rather than str + obj.
In React, call obj.toString() before rendering a Temporal object.
`;
const trimmed = Call(StringPrototypeTrim, rawMessage, []);
const singleLine = Call(StringPrototypeReplace, trimmed, [/\s+/g, ' ']);
throw new TypeError(singleLine);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The quote variance comes from prettier; it's automatic. And anyone who's seen a lot of prettier code shouldn't be surprised by this. Also, I'm not convinced that this change would add much extra readability. So I'm inclined to merge as-is. Holler if you strongly disagree!

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, not a big deal.

We've received enough user confusion about valueOf throwing that it's
worth clarifying the error message.
@ptomato ptomato merged commit 088f2b3 into tc39:main Aug 31, 2023
justingrant added a commit that referenced this pull request Sep 29, 2023
Continue where #2667 left off by further clarifying this error message and fixing a mistake where `+foo` was treated as a string coercion, not the number coercion that it is.
justingrant added a commit that referenced this pull request Sep 29, 2023
Continue where #2667 left off by further clarifying this error message
and fixing a mistake where `+foo` was treated as a string coercion, not
the number coercion that it is.
Ms2ger pushed a commit that referenced this pull request Oct 2, 2023
Continue where #2667 left off by further clarifying this error message
and fixing a mistake where `+foo` was treated as a string coercion, not
the number coercion that it is.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-spec-text PR can be ignored by implementors non-prod-polyfill THIS POLYFILL IS NOT FOR PRODUCTION USE!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants