-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
url: check forEach callback is a function #10905
Conversation
@@ -817,6 +817,9 @@ class URLSearchParams { | |||
if (arguments.length < 1) { | |||
throw new TypeError('The `callback` argument needs to be specified'); | |||
} | |||
if (typeof callback !== '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.
Does this make the check on line 817 obsolete?
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.
@cjihrig, this provides more granular error messages, which I think is a good thing. Do you prefer me to just use the same error message for both cases?
In the browser, they have different error messages as well:
>> new URLSearchParams().forEach()
** TypeError: Failed to execute 'forEach' on 'URLSearchParams':
1 argument required, but only 0 present.
>> new URLSearchParams().forEach(2)
** TypeError: Failed to execute 'forEach' on 'URLSearchParams':
The callback provided as parameter 1 is not a 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.
I would remove the extraneous check as suggested
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.
Our error messages don't even seem to match up though. I'd still change it, but I won't fight it too much.
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.
Changed.
sp.forEach(function() { | ||
assert.strictEqual(this, m); | ||
}, m); | ||
assert.throws(() => sp.forEach(), TypeError); |
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.
Instead of the TypeError
constructor, can you pass a regular expression like /^TypeError: The
callback argument must be a 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.
Fixed.
/cc @nodejs/url |
981f558
to
182ce97
Compare
@@ -817,6 +817,9 @@ class URLSearchParams { | |||
if (arguments.length < 1) { | |||
throw new TypeError('The `callback` argument needs to be specified'); | |||
} | |||
if (typeof callback !== 'function') { | |||
throw new TypeError('The `callback` argument must be a 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.
our standard way of writing this would be TypeError('"callback" argument must be a 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.
Fixed.
Landed in ed0086f. |
The Web IDL spec mandates such a check. Also make error messages consistent with rest of Node.js and add additional tests for forEach(). PR-URL: #10905 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
The Web IDL spec mandates such a check. Also make error messages consistent with rest of Node.js and add additional tests for forEach(). PR-URL: nodejs#10905 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
The Web IDL spec mandates such a check. Also make error messages consistent with rest of Node.js and add additional tests for forEach(). PR-URL: nodejs#10905 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
The Web IDL spec mandates such a check. Also make error messages consistent with rest of Node.js and add additional tests for forEach(). PR-URL: nodejs#10905 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
The Web IDL spec mandates such a check. Also make error messages consistent with rest of Node.js and add additional tests for forEach(). PR-URL: nodejs#10905 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Currently,
URLSearchParams
'forEach
function does not have a check to make sure that the type of the provided callback is a function. However, the Web IDL spec, to which this function should conform, insists that a TypeError be thrown if the callback is not callable.This PR fulfills that requirement. Also included in this PR are additional tests for certain aspects of
forEach
that are defined in the spec but not yet tested.Web IDL defines
forEach
as the following:callback
therefore has typeFunction
, which in turn is defined as a callback function:The process to convert an ECMAScript value to an IDL callback function then contains the following:
And since
callback
is not declared with [TreatNonObjectAsNull
],forEach
should throw a TypeError in casecallback
is not a function.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
url