-
Notifications
You must be signed in to change notification settings - Fork 158
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
Conversation
Codecov Report
@@ 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
|
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.
Nice.
@@ -5726,6 +5726,21 @@ export function ASCIILowercase(str) { | |||
]); | |||
} | |||
|
|||
export function ValueOfThrows(constructorName) { |
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.
Isn't ecmascript.mjs intended to correspond with operations that are in current ECMA-262? It seems like the wrong home for this function.
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.
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.
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.' | ||
); |
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.
That's a lot of quote variance. For sanity, I think maybe just `
with post-processing would be better.
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); |
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.
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!
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.
No, not a big deal.
We've received enough user confusion about valueOf throwing that it's worth clarifying the error message.
b8af801
to
946a9a4
Compare
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.
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.
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.
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.