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

url: check forEach callback is a function #10905

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,7 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 2) {
throw new TypeError(
'Both `name` and `value` arguments need to be specified');
throw new TypeError('"name" and "value" arguments must be specified');
}

name = String(name);
Expand All @@ -687,7 +686,7 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 1) {
throw new TypeError('The `name` argument needs to be specified');
throw new TypeError('"name" argument must be specified');
}

const list = this[searchParams];
Expand All @@ -708,8 +707,7 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 2) {
throw new TypeError(
'Both `name` and `value` arguments need to be specified');
throw new TypeError('"name" and "value" arguments must be specified');
}

const list = this[searchParams];
Expand Down Expand Up @@ -749,7 +747,7 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 1) {
throw new TypeError('The `name` argument needs to be specified');
throw new TypeError('"name" argument must be specified');
}

const list = this[searchParams];
Expand All @@ -767,7 +765,7 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 1) {
throw new TypeError('The `name` argument needs to be specified');
throw new TypeError('"name" argument must be specified');
}

const list = this[searchParams];
Expand All @@ -786,7 +784,7 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 1) {
throw new TypeError('The `name` argument needs to be specified');
throw new TypeError('"name" argument must be specified');
}

const list = this[searchParams];
Expand Down Expand Up @@ -814,8 +812,8 @@ class URLSearchParams {
if (!this || !(this instanceof URLSearchParams)) {
throw new TypeError('Value of `this` is not a URLSearchParams');
}
if (arguments.length < 1) {
throw new TypeError('The `callback` argument needs to be specified');
if (typeof callback !== 'function') {
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed.

throw new TypeError('"callback" argument must be a function');
}

let list = this[searchParams];
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-whatwg-url-searchparams.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ n = 0;
for (val of sp.values()) {
assert.strictEqual(val, String(values[n++]));
}
n = 0;
sp.forEach(function(val, key, obj) {
assert.strictEqual(this, undefined);
assert.strictEqual(key, 'a');
assert.strictEqual(val, String(values[n++]));
assert.strictEqual(obj, sp);
});
sp.forEach(function() {
assert.strictEqual(this, m);
}, m);
assert.throws(() => sp.forEach(),
/^TypeError: "callback" argument must be a function$/);
assert.throws(() => sp.forEach(1),
/^TypeError: "callback" argument must be a function$/);

m.search = '?a=a&b=b';
assert.strictEqual(sp.toString(), 'a=a&b=b');