Skip to content

Commit cc08d30

Browse files
VoltrexKeyvadanielleadams
authored andcommitted
lib: cleanup instance validation
Cleaned up the `URLSearchParams`'s `this` validation to increase readability by moving them to an extra helper function. PR-URL: #39656 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 2751cdf commit cc08d30

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

lib/internal/url.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class URLContext {
179179
}
180180
}
181181

182+
function isURLSearchParams(self) {
183+
return self && self[searchParams] && !self[searchParams][searchParams];
184+
}
185+
182186
class URLSearchParams {
183187
// URL Standard says the default value is '', but as undefined and '' have
184188
// the same result, undefined is used to prevent unnecessary parsing.
@@ -248,9 +252,8 @@ class URLSearchParams {
248252
}
249253

250254
[inspect.custom](recurseTimes, ctx) {
251-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
255+
if (!isURLSearchParams(this))
252256
throw new ERR_INVALID_THIS('URLSearchParams');
253-
}
254257

255258
if (typeof recurseTimes === 'number' && recurseTimes < 0)
256259
return ctx.stylize('[Object]', 'special');
@@ -285,9 +288,9 @@ class URLSearchParams {
285288
}
286289

287290
append(name, value) {
288-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
291+
if (!isURLSearchParams(this))
289292
throw new ERR_INVALID_THIS('URLSearchParams');
290-
}
293+
291294
if (arguments.length < 2) {
292295
throw new ERR_MISSING_ARGS('name', 'value');
293296
}
@@ -299,9 +302,9 @@ class URLSearchParams {
299302
}
300303

301304
delete(name) {
302-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
305+
if (!isURLSearchParams(this))
303306
throw new ERR_INVALID_THIS('URLSearchParams');
304-
}
307+
305308
if (arguments.length < 1) {
306309
throw new ERR_MISSING_ARGS('name');
307310
}
@@ -320,9 +323,9 @@ class URLSearchParams {
320323
}
321324

322325
get(name) {
323-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
326+
if (!isURLSearchParams(this))
324327
throw new ERR_INVALID_THIS('URLSearchParams');
325-
}
328+
326329
if (arguments.length < 1) {
327330
throw new ERR_MISSING_ARGS('name');
328331
}
@@ -338,9 +341,9 @@ class URLSearchParams {
338341
}
339342

340343
getAll(name) {
341-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
344+
if (!isURLSearchParams(this))
342345
throw new ERR_INVALID_THIS('URLSearchParams');
343-
}
346+
344347
if (arguments.length < 1) {
345348
throw new ERR_MISSING_ARGS('name');
346349
}
@@ -357,9 +360,9 @@ class URLSearchParams {
357360
}
358361

359362
has(name) {
360-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
363+
if (!isURLSearchParams(this))
361364
throw new ERR_INVALID_THIS('URLSearchParams');
362-
}
365+
363366
if (arguments.length < 1) {
364367
throw new ERR_MISSING_ARGS('name');
365368
}
@@ -375,9 +378,9 @@ class URLSearchParams {
375378
}
376379

377380
set(name, value) {
378-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
381+
if (!isURLSearchParams(this))
379382
throw new ERR_INVALID_THIS('URLSearchParams');
380-
}
383+
381384
if (arguments.length < 2) {
382385
throw new ERR_MISSING_ARGS('name', 'value');
383386
}
@@ -462,17 +465,16 @@ class URLSearchParams {
462465
// Define entries here rather than [Symbol.iterator] as the function name
463466
// must be set to `entries`.
464467
entries() {
465-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
468+
if (!isURLSearchParams(this))
466469
throw new ERR_INVALID_THIS('URLSearchParams');
467-
}
468470

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

472474
forEach(callback, thisArg = undefined) {
473-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
475+
if (!isURLSearchParams(this))
474476
throw new ERR_INVALID_THIS('URLSearchParams');
475-
}
477+
476478
validateCallback(callback);
477479

478480
let list = this[searchParams];
@@ -490,27 +492,24 @@ class URLSearchParams {
490492

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

497498
return createSearchParamsIterator(this, 'key');
498499
}
499500

500501
values() {
501-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
502+
if (!isURLSearchParams(this))
502503
throw new ERR_INVALID_THIS('URLSearchParams');
503-
}
504504

505505
return createSearchParamsIterator(this, 'value');
506506
}
507507

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

515514
return serializeParams(this[searchParams]);
516515
}

0 commit comments

Comments
 (0)