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

49
node_modules/astring/.eslintrc.json generated vendored Normal file
View File

@@ -0,0 +1,49 @@
{
"plugins": ["import"],
"extends": ["eslint:recommended", "plugin:import/errors", "prettier"],
"env": {
"es6": true
},
"parserOptions": {
"ecmaVersion": 13,
"sourceType": "module"
},
"globals": {
"console": true,
"global": true,
"module": true,
"process": true,
"require": true,
"window": true,
"__dirname": true
},
"overrides": [
{
"files": ["src/tests/fixtures/**/*.js"],
"rules": {
"constructor-super": 0,
"getter-return": 0,
"no-cond-assign": 0,
"no-constant-condition": 0,
"no-control-regex": 0,
"no-debugger": 0,
"no-dupe-class-members": 0,
"no-dupe-keys": 0,
"no-duplicate-case": 0,
"no-empty": 0,
"no-inner-declarations": 0,
"no-irregular-whitespace": 0,
"no-redeclare": 0,
"no-sparse-arrays": 0,
"no-undef": 0,
"no-unreachable": 0,
"no-unsafe-negation": 0,
"no-unused-labels": 0,
"no-unused-vars": 0,
"no-useless-escape": 0,
"no-var": 0,
"no-loss-of-precision": 0
}
}
]
}

19
node_modules/astring/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright (c) 2015, David Bonnet <david@bonnet.cc>
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.

322
node_modules/astring/README.md generated vendored Normal file
View File

@@ -0,0 +1,322 @@
# Astring
[![NPM Version](https://img.shields.io/npm/v/astring.svg)](https://www.npmjs.org/package/astring)
[![Build Status](https://travis-ci.org/davidbonnet/astring.svg?branch=master)](https://travis-ci.org/davidbonnet/astring)
[![Coverage](https://codecov.io/gh/davidbonnet/astring/branch/master/graph/badge.svg)](https://codecov.io/gh/davidbonnet/astring)
[![devDependency Status](https://david-dm.org/davidbonnet/astring/dev-status.svg)](https://david-dm.org/davidbonnet/astring?type=dev)
🌳 Tiny and fast JavaScript code generator from an [ESTree](https://github.com/estree/estree)-compliant AST.
🎁 Checkout the [live demo](https://david.bonnet.cc/astring/demo/).
### Key features
- Generates JavaScript code up to [version 13 (2022)](https://tc39.github.io/ecma262/) and [finished proposals](https://github.com/tc39/proposals/blob/master/finished-proposals.md).
- Works on [ESTree](https://github.com/estree/estree)-compliant ASTs such as the ones produced by [Meriyah](https://github.com/meriyah/meriyah) or [Acorn](https://github.com/acornjs/acorn).
- Extendable with custom AST node handlers.
- Considerably faster than [Bublé](https://gitlab.com/Rich-Harris/buble) (up to 5×), [Escodegen](https://github.com/estools/escodegen) (up to 10×), [Babel](https://github.com/babel/babel) (up to 50×), [UglifyJS](https://github.com/mishoo/UglifyJS2) (up to 125×), and [Prettier](https://github.com/prettier/prettier) (up to 380×).
- Supports source map generation with [Source Map](https://github.com/mozilla/source-map#sourcemapgenerator).
- Supports comment generation with [Astravel](https://github.com/davidbonnet/astravel).
- No dependencies and small footprint (≈ 16 KB minified, ≈ 4 KB gziped).
- Runs on [🦕 Deno](https://deno.land).
## Contents
<!-- MarkdownTOC autolink="true" -->
- [Installation](#installation)
- [Import](#import)
- [API](#api)
- [`generate(node: object, options: object): string | object`](#generatenode-object-options-object-string-%7C-object)
- [`GENERATOR: object`](#generator-object)
- [`EXPRESSIONS_PRECEDENCE: object`](#expressions_precedence-object)
- [`NEEDS_PARENTHESES: number`](#needs_parentheses-number)
- [`baseGenerator: object`](#basegenerator-object)
- [Benchmark](#benchmark)
- [Generating code](#generating-code)
- [Parsing and generating code](#parsing-and-generating-code)
- [Examples](#examples)
- [Generating code](#generating-code-1)
- [Generating source maps](#generating-source-maps)
- [Using writable streams](#using-writable-streams)
- [Generating comments](#generating-comments)
- [Extending](#extending)
- [Command line interface](#command-line-interface)
- [Example](#example)
<!-- /MarkdownTOC -->
## Installation
> :warning: Astring relies on `String.prototype.repeat(amount)` and `String.prototype.endsWith(string)`. If the environment running Astring does not define these methods, use [`string.prototype.repeat`](https://www.npmjs.com/package/string.prototype.repeat), [`string.prototype.endsWith`](https://www.npmjs.com/package/string.prototype.endswith) or [`babel-polyfill`](https://www.npmjs.com/package/babel-polyfill).
Install with the [Node Package Manager](https://www.npmjs.com/package/astring):
```bash
npm install astring
```
Alternatively, checkout this repository and install the development dependencies to build the module file:
```bash
git clone https://github.com/davidbonnet/astring.git
cd astring
npm install
```
## Import
Import it from [Deno's third party module repository](https://deno.land/x/astring/src/astring.js):
```js
const { generate } = await import('https://deno.land/x/astring/src/astring.js')
```
With JavaScript 6 modules:
```js
import { generate } from 'astring'
```
With CommonJS:
```js
const { generate } = require('astring')
```
A browser-ready minified bundle containing Astring is available at `dist/astring.min.js`. The module exposes a global variable `astring`:
```html
<script src="astring.min.js" type="text/javascript"></script>
<script type="text/javascript">
var generate = astring.generate
</script>
```
## API
The `astring` module exposes the following properties:
### `generate(node: object, options: object): string | object`
Returns a string representing the rendered code of the provided AST `node`. However, if an `output` stream is provided in the `options`, it writes to that stream and returns it.
The `options` are:
- `indent`: string to use for indentation (defaults to `"␣␣"`)
- `lineEnd`: string to use for line endings (defaults to `"\n"`)
- `startingIndentLevel`: indent level to start from (defaults to `0`)
- `comments`: generate comments if `true` (defaults to `false`)
- `output`: output stream to write the rendered code to (defaults to `null`)
- `generator`: custom code generator (defaults to `GENERATOR`)
- `sourceMap`: [source map generator](https://github.com/mozilla/source-map#sourcemapgenerator) (defaults to `null`)
- `expressionsPrecedence`: custom map of node types and their precedence level (defaults to `EXPRESSIONS_PRECEDENCE`)
### `GENERATOR: object`
Base generator that can be used to [extend Astring](#extending).
### `EXPRESSIONS_PRECEDENCE: object`
Mapping of node types and their precedence level to let the generator know when to use parentheses.
### `NEEDS_PARENTHESES: number`
Default precedence level that always triggers the use of parentheses.
### `baseGenerator: object`
> :warning: Deprecated, use `GENERATOR` instead.
## Benchmark
### Generating code
Operations per second for generating each sample code from a pre-parsed AST:
| code sample (length) | escodegen | astring | uglify | babel | prettier |
| :------------------- | --------: | --------: | ------: | ------: | -------: |
| tiny code (11) | 1,257,527 | 7,185,642 | 129,467 | 156,184 | 333 |
| everything (8532) | 1,366 | 8,008 | 0 | 346 | 64 |
### Parsing and generating code
Operations per second for parsing and generating each sample code:
| code sample (length) | acorn + astring | meriyah + astring | buble | sucrase |
| :------------------- | --------------: | ----------------: | -----: | ------: |
| tiny code (11) | 92,578 | 864,665 | 25,911 | 575,370 |
| everything (8532) | 706 | 1,425 | 132 | 1,403 |
## Examples
The following examples are written in JavaScript 5 with Astring imported _à la CommonJS_.
### Generating code
This example uses [Acorn](https://github.com/acornjs/acorn), a blazingly fast JavaScript AST producer and therefore the perfect companion of Astring.
```javascript
// Make sure acorn and astring modules are imported
// Set example code
var code = 'let answer = 4 + 7 * 5 + 3;\n'
// Parse it into an AST
var ast = acorn.parse(code, { ecmaVersion: 6 })
// Format it into a code string
var formattedCode = astring.generate(ast)
// Check it
console.log(code === formattedCode ? 'It works!' : 'Something went wrong…')
```
### Generating source maps
This example uses the source map generator from the [Source Map](https://github.com/mozilla/source-map#sourcemapgenerator) module.
```javascript
// Make sure acorn, sourceMap and astring modules are imported
var code = 'function add(a, b) { return a + b; }\n'
var ast = acorn.parse(code, {
ecmaVersion: 6,
sourceType: 'module',
// Locations are needed in order for the source map generator to work
locations: true,
})
// Create empty source map generator
var map = new sourceMap.SourceMapGenerator({
// Source file name must be set and will be used for mappings
file: 'script.js',
})
var formattedCode = generate(ast, {
// Enable source maps
sourceMap: map,
})
// Display generated source map
console.log(map.toString())
```
### Using writable streams
This example for [Node](http://nodejs.org) shows how to use writable streams to get the rendered code.
```javascript
// Make sure acorn and astring modules are imported
// Set example code
var code = 'let answer = 4 + 7 * 5 + 3;\n'
// Parse it into an AST
var ast = acorn.parse(code, { ecmaVersion: 6 })
// Format it and write the result to stdout
var stream = astring.generate(ast, {
output: process.stdout,
})
// The returned value is the output stream
console.log('Does stream equal process.stdout?', stream === process.stdout)
```
### Generating comments
Astring supports comment generation, provided they are stored on the AST nodes. To do so, this example uses [Astravel](https://github.com/davidbonnet/astravel), a fast AST traveller and modifier.
```javascript
// Make sure acorn, astravel and astring modules are imported
// Set example code
var code =
[
'// Compute the answer to everything',
'let answer = 4 + 7 * 5 + 3;',
'// Display it',
'console.log(answer);',
].join('\n') + '\n'
// Parse it into an AST and retrieve the list of comments
var comments = []
var ast = acorn.parse(code, {
ecmaVersion: 6,
locations: true,
onComment: comments,
})
// Attach comments to AST nodes
astravel.attachComments(ast, comments)
// Format it into a code string
var formattedCode = astring.generate(ast, {
comments: true,
})
// Check it
console.log(code === formattedCode ? 'It works!' : 'Something went wrong…')
```
### Extending
Astring can easily be extended by updating or passing a custom code `generator`. A code `generator` consists of a mapping of node names and functions that take two arguments: `node` and `state`. The `node` points to the node from which to generate the code and the `state` exposes the `write` method that takes generated code strings.
This example shows how to support the `await` keyword which is part of the [asynchronous functions proposal](https://github.com/tc39/ecmascript-asyncawait). The corresponding `AwaitExpression` node is based on [this suggested definition](https://github.com/estree/estree/blob/master/es2017.md).
```javascript
// Make sure the astring module is imported and that `Object.assign` is defined
// Create a custom generator that inherits from Astring's base generator
var customGenerator = Object.assign({}, astring.GENERATOR, {
AwaitExpression: function (node, state) {
state.write('await ')
var argument = node.argument
if (argument != null) {
this[argument.type](argument, state)
}
},
})
// Obtain a custom AST somehow (note that this AST is not obtained from a valid code)
var ast = {
type: 'AwaitExpression',
argument: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'callable',
},
arguments: [],
},
}
// Format it
var code = astring.generate(ast, {
generator: customGenerator,
})
// Check it
console.log(
code === 'await callable();\n' ? 'It works!' : 'Something went wrong…',
)
```
## Command line interface
The `bin/astring` utility can be used to convert a JSON-formatted ESTree compliant AST of a JavaScript code. It accepts the following arguments:
- `-i`, `--indent`: string to use as indentation (defaults to `"␣␣"`)
- `-l`, `--line-end`: string to use for line endings (defaults to `"\n"`)
- `-s`, `--starting-indent-level`: indent level to start from (defaults to `0`)
- `-h`, `--help`: print a usage message and exit
- `-v`, `--version`: print package version and exit
The utility reads the AST from a provided list of files or from `stdin` if none is supplied and prints the generated code.
### Example
As in the previous example, these examples use [Acorn](https://github.com/acornjs/acorn) to get the JSON-formatted AST. This command pipes the AST output by Acorn from a `script.js` file to Astring and writes the formatted JavaScript code into a `result.js` file:
```bash
acorn --ecma6 script.js | astring > result.js
```
This command does the same, but reads the AST from an intermediary file:
```bash
acorn --ecma6 script.js > ast.json
astring ast.json > result.js
```
This command reads JavaScript 6 code from `stdin` and outputs a prettified version:
```bash
cat | acorn --ecma6 | astring
```

89
node_modules/astring/astring.d.ts generated vendored Normal file
View File

@@ -0,0 +1,89 @@
import type { Node as EstreeNode } from 'estree'
import type { Mapping, SourceMapGenerator } from 'source-map'
import type { Writable } from 'stream'
/**
* State object passed to generator functions.
*/
export interface State {
output: string
write(code: string, node?: EstreeNode): void
writeComments: boolean
indent: string
lineEnd: string
indentLevel: number
line?: number
column?: number
lineEndSize?: number
mapping?: Mapping
}
/**
* Code generator for each node type.
*/
export type Generator = {
[T in EstreeNode['type']]: (
node: EstreeNode & { type: T },
state: State,
) => void
}
/**
* Code generator options.
*/
export interface Options<Output = null> {
/**
* If present, source mappings will be written to the generator.
*/
sourceMap?: SourceMapGenerator
/**
* String to use for indentation, defaults to `"␣␣"`.
*/
indent?: string
/**
* String to use for line endings, defaults to `"\n"`.
*/
lineEnd?: string
/**
* Indent level to start from, defaults to `0`.
*/
startingIndentLevel?: number
/**
* Generate comments if `true`, defaults to `false`.
*/
comments?: boolean
/**
* Output stream to write the render code to, defaults to `null`.
*/
output?: Output
/**
* Custom code generator logic.
*/
generator?: Generator
}
/**
* Core Estree Node type to accommodate derived node types from parsers.
*/
interface Node {
type: string
}
/**
* Returns a string representing the rendered code of the provided AST `node`.
* However, if an `output` stream is provided in the `options`, it writes to that stream and returns it.
*/
export function generate(node: Node, options?: Options<null>): string
export function generate(node: Node, options?: Options<Writable>): Writable
/**
* Base code generator.
*/
export const GENERATOR: Generator
/**
* Base code generator.
*
* @deprecated Use {@link GENERATOR} instead.
*/
export const baseGenerator: Generator

103
node_modules/astring/bin/astring generated vendored Executable file
View File

@@ -0,0 +1,103 @@
#!/usr/bin/env node
const generate = require('../dist/astring').generate
const version = require('../package').version
const fs = require('fs')
const path = require('path')
const argv = process.argv.slice(2)
const options = {
indent: ' ',
lindeEnd: '\n',
startingIndentLevel: 0,
}
const files = []
function printHelp(status) {
// eslint-disable-next-line no-console
const print = status === 0 ? console.log : console.error
const binName = path.basename(process.argv[1])
print('Usage: ' + binName + ' [-h, --help] [-v, --version]')
print(
' ' +
binName +
' [-i, --indent INDENT] [-l, --line-end LINE_END] [-s, --starting-indent-level LEVEL] files...',
)
process.exit(status)
}
function printVersion() {
// eslint-disable-next-line no-console
console.log(version)
process.exit(0)
}
for (let i = 0, length = argv.length; i < length; i++) {
let arg = argv[i]
if (arg[0] === '-') {
switch (arg) {
case '-i':
case '--indent':
options.indent = argv[++i]
break
case '-l':
case '--line-end':
options.lineEnd = argv[++i]
break
case '-s':
case '--starting-indent-level':
options.startingIndentLevel = parseInt(argv[++i])
break
case '-h':
case '--help':
printHelp(0)
break
case '-v':
case '--version':
printVersion()
break
default:
console.error('Option "' + arg + '" not supported.')
printHelp(1)
break
}
} else {
files.push(arg)
}
}
options.output = process.stdout
if (files.length === 0) {
let data = ''
process.stdin.setEncoding('utf8')
process.stdin.resume()
process.stdin
.on('data', function (chunk) {
data += chunk
})
.on('end', function () {
try {
generate(JSON.parse(data), options)
} catch (error) {
console.error('Error: ' + error.message)
process.exit(1)
}
})
} else {
let hasError = false
for (let i = 0, length = files.length; i < length; i++) {
try {
let file = files[i]
generate(JSON.parse(fs.readFileSync(file, 'utf8')), options)
} catch (error) {
console.error('Error: ' + error.message)
if (hasError !== true) {
hasError = true
}
}
}
if (hasError) {
process.exit(1)
}
}

1213
node_modules/astring/dist/astring.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

1
node_modules/astring/dist/astring.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

3
node_modules/astring/dist/astring.min.js generated vendored Normal file

File diff suppressed because one or more lines are too long

1
node_modules/astring/dist/astring.min.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

1188
node_modules/astring/dist/astring.mjs generated vendored Normal file

File diff suppressed because it is too large Load Diff

97
node_modules/astring/package.json generated vendored Normal file
View File

@@ -0,0 +1,97 @@
{
"name": "astring",
"version": "1.8.6",
"description": "JavaScript code generator from an ESTree-compliant AST.",
"main": "./dist/astring.js",
"module": "./dist/astring.mjs",
"types": "./astring.d.ts",
"exports": {
"types": "./astring.d.ts",
"import": "./dist/astring.mjs",
"require": "./dist/astring.js",
"browser": "./dist/astring.min.js"
},
"bin": {
"astring": "bin/astring"
},
"scripts": {
"build": "babel src/astring.js --out-file dist/astring.js --source-maps --no-comments && cp src/astring.js dist/astring.mjs",
"build:watch": "babel src/astring.js --out-file dist/astring.js --source-maps --no-comments --watch",
"build:minified": "cross-env BABEL_MODE=minified babel src/astring.js --out-file dist/astring.min.js --source-maps --no-comments",
"build:demo": "npm run build:minified && cp dist/astring.min.* docs/demo/",
"prepare": "npm run build && npm run build:minified",
"test": "npm run eslint && npm run prettier:check && npm run build:minified && npm run test:coverage",
"dev": "ava --watch src/tests/astring.js",
"test:coverage": "c8 --reporter=html --reporter=text --reporter=lcov --include='src/*.js' --exclude='src/tests/**/*.js' ava src/tests/astring.js",
"test:scripts": "npm run test:scripts:build && ava src/tests/_scripts.js",
"test:performance": "ava src/tests/performance.js",
"benchmark": "node --require esm ./src/tests/benchmark.js",
"eslint": "eslint src",
"prettier": "prettier --write \"{src,scripts}/**/*.js\" \"bin/astring\"",
"prettier:check": "prettier --list-different \"{src,scripts}/**/*.js\" \"bin/astring\"",
"prepush": "npm test",
"release": "standard-version",
"deploy": "git push --follow-tags origin main && npm publish"
},
"keywords": [
"ast",
"codegen",
"code generator",
"estree",
"astravel"
],
"repository": {
"type": "git",
"url": "https://github.com/davidbonnet/astring.git"
},
"author": "David Bonnet <david@bonnet.cc>",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.14.3",
"@babel/core": "^7.14.3",
"@babel/generator": "^7.14.3",
"@babel/parser": "^7.16.4",
"@babel/preset-env": "^7.14.4",
"acorn": "^8.6.0",
"astravel": "^0.5.0",
"ava": "^3.15.0",
"babel-preset-minify": "^0.5.1",
"benchmark": "^2.1.4",
"buble": "^0.20.0",
"c8": "^7.10.0",
"cross-env": "^7.0.3",
"escodegen": "^2.0.0",
"eslint": "^8.3.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"esm": "^3.2.25",
"glob": "^7.1.7",
"husky": "^6.0.0",
"lodash": "^4.17.21",
"meriyah": "^4.1.5",
"normalize-newline": "^3.0.0",
"prettier": "^2.4.1",
"standard-version": "^9.3.0",
"sucrase": "^3.18.1",
"uglify-js": "^3.13.8"
},
"prettier": {
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true
},
"ava": {
"files": [
"src/**/tests/astring.js",
"src/**/tests/performance.js"
],
"require": [
"esm"
]
},
"esm": "auto"
}