Skip to content

Commit

Permalink
Add support for URL and URLSearchParams
Browse files Browse the repository at this point in the history
The workaround in
DefinitelyTyped/DefinitelyTyped#34960 (comment)
was used to provide type compatibility with NodeJS and the dom types.
  • Loading branch information
remcohaszing committed Mar 7, 2021
1 parent 02aa0cc commit fa0a09e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yaml
@@ -1,3 +1,5 @@
extends:
- remcohaszing
- remcohaszing/jest
rules:
jsdoc/require-jsdoc: off
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -16,6 +16,9 @@
"url": "https://github.com/remcohaszing/estree-util-value-to-estree/issues"
},
"homepage": "https://github.com/remcohaszing/estree-util-value-to-estree#readme",
"engines": {
"node": ">=10.0.0"
},
"keywords": [
"esast",
"estree",
Expand Down
28 changes: 28 additions & 0 deletions src/index.test.ts
Expand Up @@ -198,6 +198,34 @@ describe('valueToEstree', () => {
});
});

it('should handle URL', () => {
expect(valueToEstree(new URL('https://example.com'))).toStrictEqual({
type: 'NewExpression',
callee: { type: 'Identifier', name: 'URL' },
arguments: [
{
type: 'Literal',
value: 'https://example.com/',
raw: '"https://example.com/"',
},
],
});
});

it('should handle URLSearchParams', () => {
expect(valueToEstree(new URLSearchParams('everything=awesome'))).toStrictEqual({
type: 'NewExpression',
callee: { type: 'Identifier', name: 'URLSearchParams' },
arguments: [
{
type: 'Literal',
value: 'everything=awesome',
raw: '"everything=awesome"',
},
],
});
});

it('should handle object literals', () => {
expect(valueToEstree({ number: 1, string: 'Hello', nothing: null })).toStrictEqual({
type: 'ObjectExpression',
Expand Down
24 changes: 18 additions & 6 deletions src/index.ts
@@ -1,5 +1,13 @@
import { Expression } from 'estree';

// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34960#issuecomment-576906058
declare const URL: typeof globalThis extends { URL: infer URLCtor }
? URLCtor
: typeof import('url').URL;
declare const URLSearchParams: typeof globalThis extends { URL: infer URLSearchParamsCtor }
? URLSearchParamsCtor
: typeof import('url').URLSearchParams;

/**
* A value that can be serialized by `estree-util-from-value`.
*/
Expand All @@ -12,17 +20,14 @@ export type Value =
| Uint8ClampedArray
| Uint16Array
| Uint32Array
| URL
| URLSearchParams
| Value[]
| boolean
| number
| string
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
| {
/**
* Recursively allow serializing values in objects.
*/
[key: string]: Value;
}
| { [key: string]: Value }
| null
| undefined;

Expand Down Expand Up @@ -88,6 +93,13 @@ export function valueToEstree(value?: Value): Expression {
arguments: [valueToEstree([...value])],
};
}
if (value instanceof URL || value instanceof URLSearchParams) {
return {
type: 'NewExpression',
callee: { type: 'Identifier', name: value.constructor.name },
arguments: [valueToEstree(String(value))],
};
}
return {
type: 'ObjectExpression',
properties: Object.entries(value).map(([name, val]) => ({
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"declaration": true,
"lib": ["es2018"],
"lib": ["es2018", "dom"],
"module": "commonjs",
"noEmit": true,
"noImplicitAny": true,
Expand Down

0 comments on commit fa0a09e

Please sign in to comment.