estree-util-attach-comments
Advanced tools
Comparing version 2.0.0 to 2.0.1
/** | ||
* @typedef {import('estree').BaseNode} EstreeNode | ||
* @typedef {import('estree').Comment} EstreeComment | ||
* | ||
* @typedef State | ||
* @property {EstreeComment[]} comments | ||
* @property {number} index | ||
* | ||
* @typedef Fields | ||
* @property {boolean} leading | ||
* @property {boolean} trailing | ||
*/ | ||
/** | ||
* Attach semistandard estree comment nodes to the tree. | ||
* | ||
* @param {EstreeNode} tree | ||
* @param {EstreeComment[]} [comments] | ||
* @param {Array<EstreeComment>} [comments] | ||
*/ | ||
export function attachComments( | ||
tree: EstreeNode, | ||
comments?: EstreeComment[] | ||
comments?: import('estree').Comment[] | undefined | ||
): import('estree').BaseNode | ||
@@ -26,3 +14,3 @@ export type EstreeNode = import('estree').BaseNode | ||
export type State = { | ||
comments: EstreeComment[] | ||
comments: Array<EstreeComment> | ||
index: number | ||
@@ -29,0 +17,0 @@ } |
82
index.js
@@ -1,3 +0,1 @@ | ||
var push = [].push | ||
/** | ||
@@ -8,3 +6,3 @@ * @typedef {import('estree').BaseNode} EstreeNode | ||
* @typedef State | ||
* @property {EstreeComment[]} comments | ||
* @property {Array<EstreeComment>} comments | ||
* @property {number} index | ||
@@ -17,2 +15,4 @@ * | ||
const own = {}.hasOwnProperty | ||
/** | ||
@@ -22,7 +22,7 @@ * Attach semistandard estree comment nodes to the tree. | ||
* @param {EstreeNode} tree | ||
* @param {EstreeComment[]} [comments] | ||
* @param {Array<EstreeComment>} [comments] | ||
*/ | ||
export function attachComments(tree, comments) { | ||
var list = (comments || []).concat().sort(compare) | ||
if (list.length) walk(tree, {comments: list, index: 0}) | ||
const list = (comments || []).concat().sort(compare) | ||
if (list.length > 0) walk(tree, {comments: list, index: 0}) | ||
return tree | ||
@@ -38,13 +38,2 @@ } | ||
function walk(node, state) { | ||
/** @type {EstreeNode[]} */ | ||
var children = [] | ||
/** @type {EstreeComment[]} */ | ||
var comments = [] | ||
/** @type {string} */ | ||
var key | ||
/** @type {EstreeNode|EstreeNode[]} */ | ||
var value | ||
/** @type {number} */ | ||
var index | ||
// Done, we can quit. | ||
@@ -55,18 +44,29 @@ if (state.index === state.comments.length) { | ||
/** @type {Array<EstreeNode>} */ | ||
const children = [] | ||
/** @type {Array<EstreeComment>} */ | ||
const comments = [] | ||
/** @type {string} */ | ||
let key | ||
// Find all children of `node` | ||
for (key in node) { | ||
value = node[key] | ||
if (own.call(node, key)) { | ||
/** @type {EstreeNode|Array<EstreeNode>} */ | ||
// @ts-expect-error: indexable. | ||
const value = node[key] | ||
// Ignore comments. | ||
if (value && typeof value === 'object' && key !== 'comments') { | ||
if (Array.isArray(value)) { | ||
index = -1 | ||
// 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]) | ||
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) | ||
} | ||
} else if (typeof value.type === 'string') { | ||
children.push(value) | ||
} | ||
@@ -80,8 +80,5 @@ } | ||
// Initial comments. | ||
push.apply( | ||
comments, | ||
slice(state, node, false, {leading: true, trailing: false}) | ||
) | ||
comments.push(...slice(state, node, false, {leading: true, trailing: false})) | ||
index = -1 | ||
let index = -1 | ||
@@ -93,12 +90,11 @@ while (++index < children.length) { | ||
// Dangling or trailing comments. | ||
push.apply( | ||
comments, | ||
slice(state, node, true, { | ||
comments.push( | ||
...slice(state, node, true, { | ||
leading: false, | ||
trailing: Boolean(children.length) | ||
trailing: children.length > 0 | ||
}) | ||
) | ||
if (comments.length) { | ||
// @ts-ignore, yes, because they’re nonstandard. | ||
if (comments.length > 0) { | ||
// @ts-expect-error, yes, because they’re nonstandard. | ||
node.comments = comments | ||
@@ -115,4 +111,4 @@ } | ||
function slice(state, node, compareEnd, fields) { | ||
/** @type {EstreeComment[]} */ | ||
var result = [] | ||
/** @type {Array<EstreeComment>} */ | ||
const result = [] | ||
@@ -136,3 +132,3 @@ while ( | ||
function compare(left, right, compareEnd) { | ||
var field = compareEnd ? 'end' : 'start' | ||
const field = compareEnd ? 'end' : 'start' | ||
@@ -155,7 +151,7 @@ // Offsets. | ||
if ('start' in left && field in right) { | ||
// @ts-ignore Added by Acorn | ||
// @ts-expect-error Added by Acorn | ||
return left.start - right[field] | ||
} | ||
return NaN | ||
return Number.NaN | ||
} |
{ | ||
"name": "estree-util-attach-comments", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Attach comments to estree nodes", | ||
@@ -17,7 +17,7 @@ "license": "MIT", | ||
], | ||
"repository": "wooorm/estree-util-attach-comments", | ||
"bugs": "https://github.com/wooorm/estree-util-attach-comments/issues", | ||
"repository": "syntax-tree/estree-util-attach-comments", | ||
"bugs": "https://github.com/syntax-tree/estree-util-attach-comments/issues", | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/wooorm" | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/unified" | ||
}, | ||
@@ -37,3 +37,3 @@ "author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)", | ||
"dependencies": { | ||
"@types/estree": "^0.0.46" | ||
"@types/estree": "^0.0.51" | ||
}, | ||
@@ -47,5 +47,5 @@ "devDependencies": { | ||
"prettier": "^2.0.0", | ||
"recast": "^0.20.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"recast": "^0.21.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
@@ -55,3 +55,3 @@ "tape": "^5.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.50.0" | ||
}, | ||
@@ -77,8 +77,3 @@ "scripts": { | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off", | ||
"guard-for-in": "off", | ||
"max-depth": "off", | ||
"unicorn/explicit-length-check": "off", | ||
"unicorn/prefer-number-properties": "off" | ||
"max-depth": "off" | ||
} | ||
@@ -85,0 +80,0 @@ }, |
129
readme.md
@@ -7,6 +7,26 @@ # estree-util-attach-comments | ||
[![Size][size-badge]][size] | ||
[![Sponsors][sponsors-badge]][collective] | ||
[![Backers][backers-badge]][collective] | ||
[![Chat][chat-badge]][chat] | ||
Attach semistandard [estree][] comment nodes (such as from [espree][] or | ||
[acorn][] with a couple lines of code) to the nodes in that tree. | ||
[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 | ||
@@ -16,9 +36,16 @@ acorn) whereas other estree tools expect comments to be embedded on nodes in the | ||
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: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -28,5 +55,19 @@ npm install estree-util-attach-comments | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {attachComments} from 'https://esm.sh/estree-util-attach-comments@2' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {attachComments} from 'https://esm.sh/estree-util-attach-comments@2?bundle' | ||
</script> | ||
``` | ||
## Use | ||
Say we have this weird `code`: | ||
Say our document `index.js` contains: | ||
@@ -37,5 +78,6 @@ ```js | ||
And our script, `example.js`, looks as follows: | ||
…and our module `example.js` looks as follows: | ||
```js | ||
import fs from 'node:fs/promises' | ||
import * as acorn from 'acorn' | ||
@@ -45,4 +87,5 @@ import recast from 'recast' | ||
var comments = [] | ||
var tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments}) | ||
const code = String(await fs.readFile('index.js')) | ||
const comments = [] | ||
const tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments}) | ||
@@ -74,11 +117,11 @@ attachComments(tree, comments) | ||
Note that the lines are added by `recast` in this case. | ||
And, some of these weird comments are off, but they’re pretty close. | ||
> 👉 **Note**: the lines are added by `recast` in this case. | ||
> And, some of these weird comments are off, but they’re pretty close. | ||
## API | ||
This package exports the following identifiers: `attachComment`. | ||
This package exports the identifier `attachComments`. | ||
There is no default export. | ||
### `attachComment(tree, comments)` | ||
### `attachComments(tree, comments)` | ||
@@ -107,4 +150,26 @@ Attach semistandard estree comment nodes to the tree. | ||
`Node` — The given `tree`. | ||
The given `tree` (`Node`). | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## 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 | ||
@@ -116,9 +181,9 @@ | ||
[build-badge]: https://github.com/wooorm/estree-util-attach-comments/workflows/main/badge.svg | ||
[build-badge]: https://github.com/syntax-tree/estree-util-attach-comments/workflows/main/badge.svg | ||
[build]: https://github.com/wooorm/estree-util-attach-comments/actions | ||
[build]: https://github.com/syntax-tree/estree-util-attach-comments/actions | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/estree-util-attach-comments.svg | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/estree-util-attach-comments.svg | ||
[coverage]: https://codecov.io/github/wooorm/estree-util-attach-comments | ||
[coverage]: https://codecov.io/github/syntax-tree/estree-util-attach-comments | ||
@@ -133,4 +198,20 @@ [downloads-badge]: https://img.shields.io/npm/dm/estree-util-attach-comments.svg | ||
[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 | ||
@@ -140,2 +221,10 @@ | ||
[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 | ||
@@ -145,4 +234,2 @@ | ||
[espree]: https://github.com/eslint/espree | ||
[program]: https://github.com/estree/estree/blob/master/es5.md#programs |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
13356
225
146
+ Added@types/estree@0.0.51(transitive)
- Removed@types/estree@0.0.46(transitive)
Updated@types/estree@^0.0.51