This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

18
node_modules/estree-util-value-to-estree/LICENSE.md generated vendored Normal file
View File

@@ -0,0 +1,18 @@
# MIT License
Copyright © 2021 Remco Haszing
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

258
node_modules/estree-util-value-to-estree/README.md generated vendored Normal file
View File

@@ -0,0 +1,258 @@
# estree-util-value-to-estree
[![github actions](https://github.com/remcohaszing/estree-util-value-to-estree/actions/workflows/ci.yaml/badge.svg)](https://github.com/remcohaszing/estree-util-value-to-estree/actions/workflows/ci.yaml)
[![codecov](https://codecov.io/gh/remcohaszing/estree-util-value-to-estree/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/estree-util-value-to-estree)
[![npm](https://img.shields.io/npm/v/estree-util-value-to-estree)](https://www.npmjs.com/package/estree-util-value-to-estree)
[![prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io)
Convert a JavaScript value to an [estree](https://github.com/estree/estree) expression
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [`valueToEstree(value, options?)`](#valuetoestreevalue-options)
- [options](#options)
- [License](#license)
## Installation
```sh
npm install estree-util-value-to-estree
```
## Usage
This package converts a JavaScript value to an [estree](https://github.com/estree/estree) for values
that can be constructed without the need for a context.
Currently the following types are supported:
- arrays
- bigints
- booleans
- dates
- maps
- null
- numbers (including `Infinity`, `NaN`, and negative numbers)
- objects (plain objects only)
- regular expressions
- sets
- strings
- symbols (only global symbols)
- typed arrays (`BigInt64Array`, `BigUint64Array`, `Float32Array`, `Float64Array`, `Int8Array`,
`Int16Array`, `Int32Array`, `Uint8Array`, `Uint8ClampedArray`, `Uint16Array`, and`Uint32Array`)
- undefined
- URL objects
- URLSearchParams objects
if `options.instanceAsObject` is set to `true`, other objects are turned into plain object.
```ts
import { deepEqual, throws } from 'node:assert/strict'
import { valueToEstree } from 'estree-util-value-to-estree'
const result = valueToEstree({
null: null,
undefined,
string: 'Hello world!',
number: 42,
negativeNumber: -1337,
infinity: Number.POSITIVE_INFINITY,
notANumber: Number.NaN,
regex: /\w+/i,
date: new Date('1970-01-01'),
array: ['Im an array item!'],
object: { nested: 'value' }
})
deepEqual(result, {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'null' },
value: { type: 'Literal', value: null }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'undefined' },
value: { type: 'Identifier', name: 'undefined' }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'string' },
value: { type: 'Literal', value: 'Hello world!' }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'number' },
value: { type: 'Literal', value: 42 }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'negativeNumber' },
value: {
type: 'UnaryExpression',
operator: '-',
prefix: true,
argument: { type: 'Literal', value: 1337 }
}
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'infinity' },
value: { type: 'Identifier', name: 'Infinity' }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'notANumber' },
value: { type: 'Identifier', name: 'NaN' }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'regex' },
value: {
type: 'Literal',
value: /\w+/i,
regex: { pattern: '\\w+', flags: 'i' }
}
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'date' },
value: {
type: 'NewExpression',
callee: { type: 'Identifier', name: 'Date' },
arguments: [{ type: 'Literal', value: 0 }]
}
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'array' },
value: {
type: 'ArrayExpression',
elements: [{ type: 'Literal', value: 'Im an array item!' }]
}
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'object' },
value: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'nested' },
value: { type: 'Literal', value: 'value' }
}
]
}
}
]
})
class Point {
line: number
column: number
constructor(line: number, column: number) {
this.line = line
this.column = column
}
}
// Normally complex objects throw.
throws(() => valueToEstree(new Point(2, 3)))
// `instanceAsObject: true` treats them as plain objects.
deepEqual(valueToEstree(new Point(2, 3), { instanceAsObject: true }), {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'line' },
value: { type: 'Literal', value: 2 }
},
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Literal', value: 'column' },
value: { type: 'Literal', value: 3 }
}
]
})
```
### API
This API exports the function `valueToEstree`.
#### `valueToEstree(value, options?)`
Convert a value to an ESTree node.
##### options
- `instanceAsObject` (boolean, default: `false`) — If true, treat objects that have a prototype as
plain objects.
## License
[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)

15
node_modules/estree-util-value-to-estree/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,15 @@
import { type Expression } from 'estree';
export interface Options {
/**
* If true, treat objects that have a prototype as plain objects.
*/
instanceAsObject?: boolean;
}
/**
* Convert a value to an ESTree node
*
* @param value The value to convert
* @param options Additional options to configure the output.
* @returns The ESTree node.
*/
export declare function valueToEstree(value: unknown, options?: Options): Expression;

148
node_modules/estree-util-value-to-estree/index.js generated vendored Normal file
View File

@@ -0,0 +1,148 @@
import isPlainObject from 'is-plain-obj';
/**
* Convert a value to an ESTree node
*
* @param value The value to convert
* @param options Additional options to configure the output.
* @returns The ESTree node.
*/
export function valueToEstree(value, options = {}) {
if (value === undefined || value === Number.POSITIVE_INFINITY || Number.isNaN(value)) {
return { type: 'Identifier', name: String(value) };
}
if (value == null || typeof value === 'string' || typeof value === 'boolean') {
return { type: 'Literal', value };
}
if (typeof value === 'bigint') {
return value >= 0
? { type: 'Literal', value, bigint: String(value) }
: {
type: 'UnaryExpression',
operator: '-',
prefix: true,
argument: valueToEstree(-value, options)
};
}
if (typeof value === 'number') {
return value >= 0 && !Object.is(value, -0)
? { type: 'Literal', value }
: {
type: 'UnaryExpression',
operator: '-',
prefix: true,
argument: valueToEstree(-value, options)
};
}
if (typeof value === 'symbol') {
if (value.description && value === Symbol.for(value.description)) {
return {
type: 'CallExpression',
optional: false,
callee: {
type: 'MemberExpression',
computed: false,
optional: false,
object: { type: 'Identifier', name: 'Symbol' },
property: { type: 'Identifier', name: 'for' }
},
arguments: [valueToEstree(value.description, options)]
};
}
throw new TypeError(`Only global symbols are supported, got: ${String(value)}`);
}
if (Array.isArray(value)) {
const elements = [];
for (let i = 0; i < value.length; i += 1) {
elements.push(i in value ? valueToEstree(value[i], options) : null);
}
return { type: 'ArrayExpression', elements };
}
if (value instanceof Boolean || value instanceof Number || value instanceof String) {
return {
type: 'NewExpression',
callee: { type: 'Identifier', name: value.constructor.name },
arguments: [valueToEstree(value.valueOf())]
};
}
if (value instanceof RegExp) {
return {
type: 'Literal',
value,
regex: { pattern: value.source, flags: value.flags }
};
}
if (value instanceof Date) {
return {
type: 'NewExpression',
callee: { type: 'Identifier', name: 'Date' },
arguments: [valueToEstree(value.getTime(), options)]
};
}
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
return {
type: 'CallExpression',
optional: false,
callee: {
type: 'MemberExpression',
computed: false,
optional: false,
object: { type: 'Identifier', name: 'Buffer' },
property: { type: 'Identifier', name: 'from' }
},
arguments: [valueToEstree([...value])]
};
}
if (value instanceof BigInt64Array ||
value instanceof BigUint64Array ||
value instanceof Float32Array ||
value instanceof Float64Array ||
value instanceof Int8Array ||
value instanceof Int16Array ||
value instanceof Int32Array ||
value instanceof Map ||
value instanceof Set ||
value instanceof Uint8Array ||
value instanceof Uint8ClampedArray ||
value instanceof Uint16Array ||
value instanceof Uint32Array) {
return {
type: 'NewExpression',
callee: { type: 'Identifier', name: value.constructor.name },
arguments: [valueToEstree([...value], options)]
};
}
if (value instanceof URL || value instanceof URLSearchParams) {
return {
type: 'NewExpression',
callee: { type: 'Identifier', name: value.constructor.name },
arguments: [valueToEstree(String(value), options)]
};
}
if (options.instanceAsObject || isPlainObject(value)) {
const properties = Reflect.ownKeys(value).map((key) => ({
type: 'Property',
method: false,
shorthand: false,
computed: typeof key !== 'string',
kind: 'init',
key: valueToEstree(key, options),
value: valueToEstree(value[key], options)
}));
if (Object.getPrototypeOf(value) == null) {
properties.unshift({
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Identifier', name: '__proto__' },
value: { type: 'Literal', value: null }
});
}
return {
type: 'ObjectExpression',
properties
};
}
throw new TypeError(`Unsupported value: ${String(value)}`);
}

48
node_modules/estree-util-value-to-estree/package.json generated vendored Normal file
View File

@@ -0,0 +1,48 @@
{
"name": "estree-util-value-to-estree",
"description": "Convert a JavaScript value to an estree expression",
"version": "3.0.1",
"exports": "./index.js",
"type": "module",
"files": [
"index.js",
"index.d.ts"
],
"author": "Remco Haszing <remcohaszing@gmail.com>",
"license": "MIT",
"repository": "remcohaszing/estree-util-value-to-estree",
"bugs": "https://github.com/remcohaszing/estree-util-value-to-estree/issues",
"homepage": "https://github.com/remcohaszing/estree-util-value-to-estree#readme",
"funding": "https://github.com/sponsors/remcohaszing",
"engines": {
"node": ">=16.0.0"
},
"keywords": [
"esast",
"estree",
"estree-util",
"language",
"unist"
],
"scripts": {
"prepack": "tsc --noEmit false",
"start": "tsx watch test.ts",
"test": "c8 tsx test.ts"
},
"dependencies": {
"@types/estree": "^1.0.0",
"is-plain-obj": "^4.0.0"
},
"devDependencies": {
"@types/node": "*",
"astring": "^1.0.0",
"c8": "^7.0.0",
"eslint": "^8.0.0",
"eslint-config-remcohaszing": "^9.0.0",
"prettier": "^2.0.0",
"remark-cli": "11.0.0",
"remark-preset-remcohaszing": "^1.0.0",
"tsx": "^3.0.0",
"typescript": "^5.0.0"
}
}