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

lib: cleanup instance validation #39656

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 23 additions & 24 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class URLContext {
}
}

function isURLSearchParams(self) {
return self && self[searchParams] && !self[searchParams][searchParams];
}

class URLSearchParams {
// URL Standard says the default value is '', but as undefined and '' have
// the same result, undefined is used to prevent unnecessary parsing.
Expand Down Expand Up @@ -222,9 +226,8 @@ class URLSearchParams {
}

[inspect.custom](recurseTimes, ctx) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (typeof recurseTimes === 'number' && recurseTimes < 0)
return ctx.stylize('[Object]', 'special');
Expand Down Expand Up @@ -259,9 +262,9 @@ class URLSearchParams {
}

append(name, value) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('name', 'value');
}
Expand All @@ -273,9 +276,9 @@ class URLSearchParams {
}

delete(name) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (arguments.length < 1) {
throw new ERR_MISSING_ARGS('name');
}
Expand All @@ -294,9 +297,9 @@ class URLSearchParams {
}

get(name) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (arguments.length < 1) {
throw new ERR_MISSING_ARGS('name');
}
Expand All @@ -312,9 +315,9 @@ class URLSearchParams {
}

getAll(name) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (arguments.length < 1) {
throw new ERR_MISSING_ARGS('name');
}
Expand All @@ -331,9 +334,9 @@ class URLSearchParams {
}

has(name) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (arguments.length < 1) {
throw new ERR_MISSING_ARGS('name');
}
Expand All @@ -349,9 +352,9 @@ class URLSearchParams {
}

set(name, value) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('name', 'value');
}
Expand Down Expand Up @@ -436,17 +439,16 @@ class URLSearchParams {
// Define entries here rather than [Symbol.iterator] as the function name
// must be set to `entries`.
entries() {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

return createSearchParamsIterator(this, 'key+value');
}

forEach(callback, thisArg = undefined) {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

validateCallback(callback);

let list = this[searchParams];
Expand All @@ -464,27 +466,24 @@ class URLSearchParams {

// https://heycam.github.io/webidl/#es-iterable
keys() {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

return createSearchParamsIterator(this, 'key');
}

values() {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

return createSearchParamsIterator(this, 'value');
}

// https://heycam.github.io/webidl/#es-stringifier
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
toString() {
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
}

return serializeParams(this[searchParams]);
}
Expand Down