-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out URLSearchParams, require typedef for unparsable files (#45783)
Summary: Pull Request resolved: #45783 Improves type strictness in the `react-native` package. - Break out `URLSearchParams` from `URL.js` into its own module, to isolate a `$FlowFixMe[unsupported-syntax]` suppression within that definition. - Update `public-api-test` to require an adjacent `<module>.js.flow` type definition file whenever a `$FlowFixMe[unsupported-syntax]`is present. - Add `URLSearchParams.js.flow` with a Flow parser compatible typedef (`@iterator` instead of `[Symbol.iterator]`). The result of these changes is to add missing typedef test coverage for `Libraries/Blob/URL.js` (see updated test snapshots). Changelog: [Internal] Reviewed By: hoxyq Differential Revision: D60376327
- Loading branch information
1 parent
03f5dc0
commit e4b4d22
Showing
5 changed files
with
154 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
// Small subset from whatwg-url: https://github.com/jsdom/whatwg-url/tree/master/src | ||
// The reference code bloat comes from Unicode issues with URLs, so those won't work here. | ||
export class URLSearchParams { | ||
_searchParams: Array<Array<string>> = []; | ||
|
||
constructor(params: any) { | ||
if (typeof params === 'object') { | ||
Object.keys(params).forEach(key => this.append(key, params[key])); | ||
} | ||
} | ||
|
||
append(key: string, value: string): void { | ||
this._searchParams.push([key, value]); | ||
} | ||
|
||
delete(name: string): void { | ||
throw new Error('URLSearchParams.delete is not implemented'); | ||
} | ||
|
||
get(name: string): void { | ||
throw new Error('URLSearchParams.get is not implemented'); | ||
} | ||
|
||
getAll(name: string): void { | ||
throw new Error('URLSearchParams.getAll is not implemented'); | ||
} | ||
|
||
has(name: string): void { | ||
throw new Error('URLSearchParams.has is not implemented'); | ||
} | ||
|
||
set(name: string, value: string): void { | ||
throw new Error('URLSearchParams.set is not implemented'); | ||
} | ||
|
||
sort(): void { | ||
throw new Error('URLSearchParams.sort is not implemented'); | ||
} | ||
|
||
// $FlowFixMe[unsupported-syntax] | ||
// $FlowFixMe[missing-local-annot] | ||
[Symbol.iterator]() { | ||
return this._searchParams[Symbol.iterator](); | ||
} | ||
|
||
toString(): string { | ||
if (this._searchParams.length === 0) { | ||
return ''; | ||
} | ||
const last = this._searchParams.length - 1; | ||
return this._searchParams.reduce((acc, curr, index) => { | ||
return ( | ||
acc + | ||
encodeURIComponent(curr[0]) + | ||
'=' + | ||
encodeURIComponent(curr[1]) + | ||
(index === last ? '' : '&') | ||
); | ||
}, ''); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/react-native/Libraries/Blob/URLSearchParams.js.flow
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
declare export class URLSearchParams { | ||
_searchParams: Array<Array<string>>; | ||
constructor(params: any): void; | ||
append(key: string, value: string): void; | ||
delete(name: string): void; | ||
get(name: string): void; | ||
getAll(name: string): void; | ||
has(name: string): void; | ||
set(name: string, value: string): void; | ||
sort(): void; | ||
@@iterator: Iterator<Array<string>>; | ||
toString(): string; | ||
} |
This file contains 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
This file contains 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