Socket
Socket
Sign inDemoInstall

estree-util-attach-comments

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

estree-util-attach-comments - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

index.d.ts

60

index.js

@@ -1,17 +0,44 @@

'use strict'
var push = [].push
module.exports = attachComments
/**
* @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
*/
var push = [].push
function attachComments(tree, comments) {
walk(tree, {comments: comments.concat().sort(compare), index: 0})
/**
* Attach semistandard estree comment nodes to the tree.
*
* @param {EstreeNode} tree
* @param {EstreeComment[]} [comments]
*/
export function attachComments(tree, comments) {
var list = (comments || []).concat().sort(compare)
if (list.length) 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) {
/** @type {EstreeNode[]} */
var children = []
/** @type {EstreeComment[]} */
var comments = []
/** @type {string} */
var key
/** @type {EstreeNode|EstreeNode[]} */
var value
/** @type {number} */
var index

@@ -30,5 +57,3 @@

if (value && typeof value === 'object' && key !== 'comments') {
if (typeof value.type === 'string') {
children.push(value)
} else if (Array.isArray(value)) {
if (Array.isArray(value)) {
index = -1

@@ -41,2 +66,4 @@

}
} else if (typeof value.type === 'string') {
children.push(value)
}

@@ -71,2 +98,3 @@ }

if (comments.length) {
// @ts-ignore, yes, because they’re nonstandard.
node.comments = comments

@@ -76,3 +104,10 @@ }

/**
* @param {State} state
* @param {EstreeNode} node
* @param {boolean} compareEnd
* @param {Fields} fields
*/
function slice(state, node, compareEnd, fields) {
/** @type {EstreeComment[]} */
var result = []

@@ -90,2 +125,8 @@

/**
* @param {EstreeNode|EstreeComment} left
* @param {EstreeNode|EstreeComment} right
* @param {boolean} [compareEnd]
* @returns {number}
*/
function compare(left, right, compareEnd) {

@@ -110,2 +151,3 @@ var field = compareEnd ? 'end' : 'start'

if ('start' in left && field in right) {
// @ts-ignore Added by Acorn
return left.start - right[field]

@@ -112,0 +154,0 @@ }

44

package.json
{
"name": "estree-util-attach-comments",
"version": "1.0.0",
"version": "2.0.0",
"description": "Attach comments to estree nodes",

@@ -27,10 +27,19 @@ "license": "MIT",

],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {},
"dependencies": {
"@types/estree": "^0.0.46"
},
"devDependencies": {
"@types/acorn": "^4.0.0",
"@types/tape": "^4.0.0",
"acorn": "^8.0.0",
"estree-walker": "^2.0.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"estree-util-visit": "^1.0.0",
"prettier": "^2.0.0",

@@ -40,10 +49,15 @@ "recast": "^0.20.0",

"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"xo": "^0.36.0"
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run test-coverage"
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run build && npm run format && npm run test-coverage"
},

@@ -60,4 +74,5 @@ "prettier": {

"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"guard-for-in": "off",

@@ -69,8 +84,2 @@ "max-depth": "off",

},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"remarkConfig": {

@@ -80,3 +89,8 @@ "plugins": [

]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}

@@ -17,2 +17,5 @@ # estree-util-attach-comments

This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.
[npm][]:

@@ -35,5 +38,5 @@

```js
var acorn = require('acorn')
var recast = require('recast')
var attachComments = require('estree-util-attach-comments')
import * as acorn from 'acorn'
import recast from 'recast'
import {attachComments} from 'estree-util-attach-comments'

@@ -73,2 +76,5 @@ var comments = []

This package exports the following identifiers: `attachComment`.
There is no default export.
### `attachComment(tree, comments)`

@@ -75,0 +81,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc