FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

url: check forEach callback is a function by TimothyGu · Pull Request #10905 · nodejs/node · GitHub

/ node Public

url: check forEach callback is a function - #10905

Closed
TimothyGu wants to merge 6 commits into
nodejs:masterfrom
TimothyGu:urlsearchparams-foreach
Closed

url: check forEach callback is a function#10905
TimothyGu wants to merge 6 commits into
nodejs:masterfrom
TimothyGu:urlsearchparams-foreach

Conversation

Copy link
Copy Markdown
Member

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:

void forEach(Function callback, optional any thisArg);

callback therefore has type Function, which in turn is defined as a callback function:

callback Function = any (any... arguments);

The process to convert an ECMAScript value to an IDL callback function then contains the following:

  1. If the result of calling IsCallable(V) is false and the conversion to an IDL value is not being performed due to V being assigned to an attribute whose type is a nullable callback function that is annotated with [TreatNonObjectAsNull], then throw a TypeError.

And since callback is not declared with [TreatNonObjectAsNull], forEach should throw a TypeError in case callback is not a function.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

url

nodejs-github-bot added dont-land-on-v4.x whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Jan 20, 2017
Comment thread lib/internal/url.js
if (arguments.length < 1) {
throw new TypeError('The `callback` argument needs to be specified');
}
if (typeof callback !== 'function') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Does this make the check on line 817 obsolete?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I would remove the extraneous check as suggested

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Changed.

sp.forEach(function() {
assert.strictEqual(this, m);
}, m);
assert.throws(() => sp.forEach(), TypeError);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Instead of the TypeError constructor, can you pass a regular expression like /^TypeError: The callback argument must be a function$/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Fixed.

Copy link
Copy Markdown
Member Author

/cc @nodejs/url

Comment thread lib/internal/url.js Outdated
throw new TypeError('The `callback` argument needs to be specified');
}
if (typeof callback !== 'function') {
throw new TypeError('The `callback` argument must be a function');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

our standard way of writing this would be TypeError('"callback" argument must be a function')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Fixed.

Copy link
Copy Markdown
Member Author

Copy link
Copy Markdown
Member Author

Landed in ed0086f.

TimothyGu closed this Jan 24, 2017
TimothyGu deleted the urlsearchparams-foreach branch January 24, 2017 00:44
TimothyGu added a commit that referenced this pull request Jan 24, 2017
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>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 25, 2017
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>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 27, 2017
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>
italoacasas mentioned this pull request Jan 29, 2017
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
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>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

whatwg-url Issues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants


Back | FazBrowse Home | New Git URL