From cc08d3062f9b0bace76eff382fe218e9198de66d Mon Sep 17 00:00:00 2001 From: Voltrex Date: Wed, 4 Aug 2021 14:04:51 +0430 Subject: [PATCH] lib: cleanup instance validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cleaned up the `URLSearchParams`'s `this` validation to increase readability by moving them to an extra helper function. PR-URL: https://github.com/nodejs/node/pull/39656 Reviewed-By: Tobias Nießen Reviewed-By: Darshan Sen --- lib/internal/url.js | 47 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 043b7311a2ce70..378248b9486b52 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -179,6 +179,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. @@ -248,9 +252,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'); @@ -285,9 +288,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'); } @@ -299,9 +302,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'); } @@ -320,9 +323,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'); } @@ -338,9 +341,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'); } @@ -357,9 +360,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'); } @@ -375,9 +378,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'); } @@ -462,17 +465,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]; @@ -490,17 +492,15 @@ 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'); } @@ -508,9 +508,8 @@ class URLSearchParams { // 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]); }