estree-util-attach-comments
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -1,20 +0,1 @@ | ||
/** | ||
* Attach semistandard estree comment nodes to the tree. | ||
* | ||
* @param {EstreeNode} tree | ||
* @param {Array<EstreeComment>} [comments] | ||
*/ | ||
export function attachComments( | ||
tree: EstreeNode, | ||
comments?: import('estree').Comment[] | undefined | ||
): import('estree').BaseNode | ||
export type EstreeNode = import('estree').BaseNode | ||
export type EstreeComment = import('estree').Comment | ||
export type State = { | ||
comments: Array<EstreeComment> | ||
index: number | ||
} | ||
export type Fields = { | ||
leading: boolean | ||
trailing: boolean | ||
} | ||
export {attachComments} from './lib/index.js' |
148
index.js
@@ -1,147 +0,1 @@ | ||
/** | ||
* @typedef {import('estree').BaseNode} EstreeNode | ||
* @typedef {import('estree').Comment} EstreeComment | ||
* | ||
* @typedef State | ||
* @property {Array<EstreeComment>} comments | ||
* @property {number} index | ||
* | ||
* @typedef Fields | ||
* @property {boolean} leading | ||
* @property {boolean} trailing | ||
*/ | ||
const own = {}.hasOwnProperty | ||
/** | ||
* Attach semistandard estree comment nodes to the tree. | ||
* | ||
* @param {EstreeNode} tree | ||
* @param {Array<EstreeComment>} [comments] | ||
*/ | ||
export function attachComments(tree, comments) { | ||
const list = (comments || []).concat().sort(compare) | ||
if (list.length > 0) walk(tree, {comments: list, index: 0}) | ||
return tree | ||
} | ||
/** | ||
* Attach semistandard estree comment nodes to the tree. | ||
* | ||
* @param {EstreeNode} node | ||
* @param {State} state | ||
*/ | ||
function walk(node, state) { | ||
// Done, we can quit. | ||
if (state.index === state.comments.length) { | ||
return | ||
} | ||
/** @type {Array<EstreeNode>} */ | ||
const children = [] | ||
/** @type {Array<EstreeComment>} */ | ||
const comments = [] | ||
/** @type {string} */ | ||
let key | ||
// Find all children of `node` | ||
for (key in node) { | ||
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)) { | ||
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 they’re nonstandard. | ||
node.comments = comments | ||
} | ||
} | ||
/** | ||
* @param {State} state | ||
* @param {EstreeNode} node | ||
* @param {boolean} compareEnd | ||
* @param {Fields} fields | ||
*/ | ||
function slice(state, node, compareEnd, fields) { | ||
/** @type {Array<EstreeComment>} */ | ||
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 | ||
} | ||
/** | ||
* @param {EstreeNode|EstreeComment} left | ||
* @param {EstreeNode|EstreeComment} right | ||
* @param {boolean} [compareEnd] | ||
* @returns {number} | ||
*/ | ||
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 | ||
} | ||
export {attachComments} from './lib/index.js' |
{ | ||
"name": "estree-util-attach-comments", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Attach comments to estree nodes", | ||
@@ -32,2 +32,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -41,3 +42,3 @@ "index.js" | ||
"@types/acorn": "^4.0.0", | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"acorn": "^8.0.0", | ||
@@ -47,17 +48,15 @@ "c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"recast": "^0.21.0", | ||
"recast": "^0.22.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.51.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -64,0 +63,0 @@ }, |
@@ -47,3 +47,3 @@ # estree-util-attach-comments | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]: | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
@@ -118,3 +118,3 @@ ```sh | ||
This package exports the identifier `attachComments`. | ||
This package exports the identifier [`attachComments`][attachcomments]. | ||
There is no default export. | ||
@@ -126,3 +126,3 @@ | ||
This mutates the given [`tree`][estree] ([`Program`][program]). | ||
This mutates the given [`tree`][estree]. | ||
It takes `comments`, walks the tree, and adds comments as close as possible | ||
@@ -145,5 +145,12 @@ to where they originated. | ||
###### Parameters | ||
* `tree` ([`Program`][program]) | ||
— tree to attach to | ||
* `comments` (`Array<EstreeComment>`) | ||
— list of comments | ||
###### Returns | ||
The given `tree` (`Node`). | ||
The given `tree` ([`Program`][program]). | ||
@@ -159,3 +166,3 @@ ## Types | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
@@ -230,1 +237,3 @@ | ||
[program]: https://github.com/estree/estree/blob/master/es5.md#programs | ||
[attachcomments]: #attachcommentstree-comments |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
16174
12
7
225
234
1