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

1
node_modules/estree-util-attach-comments/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1 @@
export { attachComments } from "./lib/index.js";

1
node_modules/estree-util-attach-comments/index.js generated vendored Normal file
View File

@@ -0,0 +1 @@
export {attachComments} from './lib/index.js'

View File

@@ -0,0 +1,59 @@
/**
* Attach semistandard estree comment nodes to the tree.
*
* This mutates the given `tree`.
* It takes `comments`, walks the tree, and adds comments as close as possible
* to where they originated.
*
* Comment nodes are given two boolean fields: `leading` (`true` for
* `/* a *\/ b`) and `trailing` (`true` for `a /* b *\/`).
* Both fields are `false` for dangling comments: `[/* a *\/]`.
* This is what `recast` uses too, and is somewhat similar to Babel, which is
* not estree but instead uses `leadingComments`, `trailingComments`, and
* `innerComments` arrays on nodes.
*
* The algorithm checks any node: even recent (or future) proposals or
* nonstandard syntax such as JSX, because it ducktypes to find nodes instead
* of having a list of visitor keys.
*
* The algorithm supports `loc` fields (line/column), `range` fields (offsets),
* and direct `start` / `end` fields.
*
* @template {Nodes} Tree
* Node type.
* @param {Tree} tree
* Tree to attach to.
* @param {Array<Comment> | null | undefined} [comments]
* List of comments (optional).
* @returns {undefined}
* Nothing.
*/
export function attachComments<Tree extends import("estree").Node>(tree: Tree, comments?: Array<Comment> | null | undefined): undefined;
export type Comment = import('estree').Comment;
export type Nodes = import('estree').Node;
/**
* Fields.
*/
export type Fields = {
/**
* Whether its leading.
*/
leading: boolean;
/**
* Whether its trailing.
*/
trailing: boolean;
};
/**
* Info passed around.
*/
export type State = {
/**
* Comments.
*/
comments: Array<Comment>;
/**
* Index of comment.
*/
index: number;
};

198
node_modules/estree-util-attach-comments/lib/index.js generated vendored Normal file
View File

@@ -0,0 +1,198 @@
/**
* @typedef {import('estree').Comment} Comment
* @typedef {import('estree').Node} Nodes
*/
/**
* @typedef Fields
* Fields.
* @property {boolean} leading
* Whether its leading.
* @property {boolean} trailing
* Whether its trailing.
*
* @typedef State
* Info passed around.
* @property {Array<Comment>} comments
* Comments.
* @property {number} index
* Index of comment.
*/
const own = {}.hasOwnProperty
/** @type {Array<Comment>} */
const emptyComments = []
/**
* Attach semistandard estree comment nodes to the tree.
*
* This mutates the given `tree`.
* It takes `comments`, walks the tree, and adds comments as close as possible
* to where they originated.
*
* Comment nodes are given two boolean fields: `leading` (`true` for
* `/* a *\/ b`) and `trailing` (`true` for `a /* b *\/`).
* Both fields are `false` for dangling comments: `[/* a *\/]`.
* This is what `recast` uses too, and is somewhat similar to Babel, which is
* not estree but instead uses `leadingComments`, `trailingComments`, and
* `innerComments` arrays on nodes.
*
* The algorithm checks any node: even recent (or future) proposals or
* nonstandard syntax such as JSX, because it ducktypes to find nodes instead
* of having a list of visitor keys.
*
* The algorithm supports `loc` fields (line/column), `range` fields (offsets),
* and direct `start` / `end` fields.
*
* @template {Nodes} Tree
* Node type.
* @param {Tree} tree
* Tree to attach to.
* @param {Array<Comment> | null | undefined} [comments]
* List of comments (optional).
* @returns {undefined}
* Nothing.
*/
export function attachComments(tree, comments) {
const list = comments ? [...comments].sort(compare) : emptyComments
if (list.length > 0) walk(tree, {comments: list, index: 0})
}
/**
* Attach semistandard estree comment nodes to the tree.
*
* @param {Nodes} node
* Node.
* @param {State} state
* Info passed around.
* @returns {undefined}
* Nothing.
*/
function walk(node, state) {
// Done, we can quit.
if (state.index === state.comments.length) {
return
}
/** @type {Array<Nodes>} */
const children = []
/** @type {Array<Comment>} */
const comments = []
/** @type {string} */
let key
// Find all children of `node`
for (key in node) {
if (own.call(node, key)) {
/** @type {Array<Nodes> | Nodes} */
// @ts-expect-error: indexable.
const value = node[key]
// Ignore comments.
if (value && typeof value === 'object' && key !== 'comments') {
if (Array.isArray(value)) {
let index = -1
while (++index < value.length) {
if (value[index] && typeof value[index].type === 'string') {
children.push(value[index])
}
}
} else if (typeof value.type === 'string') {
children.push(value)
}
}
}
}
// Sort the children.
children.sort(compare)
// Initial comments.
comments.push(...slice(state, node, false, {leading: true, trailing: false}))
let index = -1
while (++index < children.length) {
walk(children[index], state)
}
// Dangling or trailing comments.
comments.push(
...slice(state, node, true, {
leading: false,
trailing: children.length > 0
})
)
if (comments.length > 0) {
// @ts-expect-error, yes, because theyre nonstandard.
node.comments = comments
}
}
/**
* @param {State} state
* Info passed around.
* @param {Nodes} node
* Node.
* @param {boolean} compareEnd
* Whether to compare on the end (default is on start).
* @param {Fields} fields
* Fields.
* @returns {Array<Comment>}
* Slice from `state.comments`.
*/
function slice(state, node, compareEnd, fields) {
/** @type {Array<Comment>} */
const result = []
while (
state.comments[state.index] &&
compare(state.comments[state.index], node, compareEnd) < 1
) {
result.push(Object.assign({}, state.comments[state.index++], fields))
}
return result
}
/**
* Sort two nodes (or comments).
*
* @param {Comment | Nodes} left
* A node.
* @param {Comment | Nodes} right
* The other node.
* @param {boolean | undefined} [compareEnd=false]
* Compare on `end` of `right`, default is to compare on `start` (default:
* `false`).
* @returns {number}
* Sorting.
*/
function compare(left, right, compareEnd) {
const field = compareEnd ? 'end' : 'start'
// Offsets.
if (left.range && right.range) {
return left.range[0] - right.range[compareEnd ? 1 : 0]
}
// Points.
if (left.loc && left.loc.start && right.loc && right.loc[field]) {
return (
left.loc.start.line - right.loc[field].line ||
left.loc.start.column - right.loc[field].column
)
}
// Just `start` (and `end`) on nodes.
// Default in most parsers.
if ('start' in left && field in right) {
// @ts-expect-error Added by Acorn
return left.start - right[field]
}
return Number.NaN
}

22
node_modules/estree-util-attach-comments/license generated vendored Normal file
View File

@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>
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.

85
node_modules/estree-util-attach-comments/package.json generated vendored Normal file
View File

@@ -0,0 +1,85 @@
{
"name": "estree-util-attach-comments",
"version": "3.0.0",
"description": "Attach comments to estree nodes",
"license": "MIT",
"keywords": [
"estree",
"ast",
"ecmascript",
"javascript",
"tree",
"comment",
"acorn",
"espree",
"recast"
],
"repository": "syntax-tree/estree-util-attach-comments",
"bugs": "https://github.com/syntax-tree/estree-util-attach-comments/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"exports": "./index.js",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/estree": "^1.0.0"
},
"devDependencies": {
"@types/acorn": "^4.0.0",
"@types/node": "^20.0.0",
"acorn": "^8.0.0",
"c8": "^8.0.0",
"estree-util-visit": "^2.0.0",
"prettier": "^3.0.0",
"recast": "^0.23.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"xo": "^0.55.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --log-level warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"bracketSpacing": false,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
},
"remarkConfig": {
"plugins": [
"remark-preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"ignoreCatch": true,
"strict": true
},
"xo": {
"prettier": true,
"rules": {
"max-depth": "off"
}
}
}

242
node_modules/estree-util-attach-comments/readme.md generated vendored Normal file
View File

@@ -0,0 +1,242 @@
# estree-util-attach-comments
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[estree][] utility attach semistandard comment nodes (such as from [acorn][]) to
the nodes in that tree.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`attachComments(tree, comments)`](#attachcommentstree-comments)
* [Types](#types)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package is a utility that you can use to embed comment nodes *inside* a
tree.
This is useful because certain estree parsers give you an array (espree and
acorn) whereas other estree tools expect comments to be embedded on nodes in the
tree.
This package uses one `comments` array where each comment has `leading` and
`trailing` fields, as applied by `acorn`, but does not support the slightly
different non-standard comments made by `espree`.
## When should I use this?
You can use this package when working with comments from Acorn and later working
with a tool such as recast or Babel.
## Install
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
```sh
npm install estree-util-attach-comments
```
In Deno with [`esm.sh`][esmsh]:
```js
import {attachComments} from 'https://esm.sh/estree-util-attach-comments@3'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import {attachComments} from 'https://esm.sh/estree-util-attach-comments@3?bundle'
</script>
```
## Use
Say our document `x.js` contains:
```js
/* 1 */ function /* 2 */ a /* 3 */(/* 4 */ b) /* 5 */ {
/* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */
}
```
…and our module `example.js` looks as follows:
```js
import fs from 'node:fs/promises'
import {parse} from 'acorn'
import {attachComments} from 'estree-util-attach-comments'
import recast from 'recast'
const code = String(await fs.readFile('x.js'))
const comments = []
const tree = parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
onComment: comments
})
attachComments(tree, comments)
console.log(recast.print(tree).code)
```
Yields:
```js
/* 1 */
function /* 2 */
a(
/* 3 */
/* 4 */
b
) /* 5 */
{
/* 6 */
return (
/* 7 */
b + /* 8 */
1
);
}/* 9 */
```
> 👉 **Note**: the lines are added by `recast` in this case.
> And, some of these weird comments are off, but theyre pretty close.
## API
This package exports the identifier [`attachComments`][api-attach-comments].
There is no default export.
### `attachComments(tree, comments)`
Attach semistandard estree comment nodes to the tree.
This mutates the given [`tree`][estree].
It takes `comments`, walks the tree, and adds comments as close as possible
to where they originated.
Comment nodes are given two boolean fields: `leading` (`true` for `/* a */ b`)
and `trailing` (`true` for `a /* b */`).
Both fields are `false` for dangling comments: `[/* a */]`.
This is what `recast` uses too, and is somewhat similar to Babel, which is not
estree but instead uses `leadingComments`, `trailingComments`, and
`innerComments` arrays on nodes.
The algorithm checks any node: even recent (or future) proposals or nonstandard
syntax such as JSX, because it ducktypes to find nodes instead of having a list
of visitor keys.
The algorithm supports `loc` fields (line/column), `range` fields (offsets),
and direct `start` / `end` fields.
###### Parameters
* `tree` ([`Program`][program])
— tree to attach to
* `comments` (`Array<EstreeComment>`)
— list of comments
###### Returns
Nothing (`undefined`).
## Types
This package is fully typed with [TypeScript][].
It exports no additional types.
## Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
`estree-util-attach-comments@^3`, compatible with Node.js 16.
## Contribute
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
ways to get started.
See [`support.md`][support] for ways to get help.
This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
## License
[MIT][license] © [Titus Wormer][author]
<!-- Definitions -->
[build-badge]: https://github.com/syntax-tree/estree-util-attach-comments/workflows/main/badge.svg
[build]: https://github.com/syntax-tree/estree-util-attach-comments/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/estree-util-attach-comments.svg
[coverage]: https://codecov.io/github/syntax-tree/estree-util-attach-comments
[downloads-badge]: https://img.shields.io/npm/dm/estree-util-attach-comments.svg
[downloads]: https://www.npmjs.com/package/estree-util-attach-comments
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=estree-util-attach-comments
[size]: https://bundlejs.com/?q=estree-util-attach-comments
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/syntax-tree/unist/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[license]: license
[author]: https://wooorm.com
[health]: https://github.com/syntax-tree/.github
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[acorn]: https://github.com/acornjs/acorn
[estree]: https://github.com/estree/estree
[program]: https://github.com/estree/estree/blob/master/es5.md#programs
[api-attach-comments]: #attachcommentstree-comments