estree-util-visit
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,3 +1,7 @@ | ||
/** | ||
* @param {string} d | ||
* @returns {string} | ||
*/ | ||
export function color(d) { | ||
return d | ||
} |
@@ -0,3 +1,7 @@ | ||
/** | ||
* @param {string} d | ||
* @returns {string} | ||
*/ | ||
export function color(d) { | ||
return '\u001B[33m' + d + '\u001B[39m' | ||
} |
119
index.js
@@ -0,1 +1,32 @@ | ||
/** | ||
* @typedef {import('estree-jsx').Node} EstreeNode | ||
* @typedef {import('unist').Node} UnistNode | ||
* @typedef {EstreeNode|UnistNode} Node | ||
* | ||
* @typedef {CONTINUE|SKIP|EXIT} Action Union of the action types | ||
* @typedef {number} Index Move to the sibling at index next (after node itself | ||
* is completely traversed). | ||
* Useful if mutating the tree, such as removing the node the visitor is | ||
* currently on, or any of its previous siblings. | ||
* Results less than 0 or greater than or equal to `children.length` stop | ||
* traversing the parent | ||
* @typedef {[(Action|null|undefined|void)?, (Index|null|undefined)?]} ActionTuple | ||
* List with one or two values, the first an action, the second an index. | ||
*/ | ||
/** | ||
* @callback Visitor | ||
* @param {Node} node Found node | ||
* @param {string?} key Field at which `node` lives | ||
* @param {number?} index Position at which `node` lives | ||
* @param {Array.<Node>} ancestors Ancestors of node | ||
* @returns {null|undefined|Action|Index|ActionTuple|void} | ||
*/ | ||
/** | ||
* @typedef Visitors | ||
* @property {Visitor} [enter] | ||
* @property {Visitor} [leave] | ||
*/ | ||
import {color} from './color.js' | ||
@@ -5,11 +36,30 @@ | ||
export var CONTINUE = Symbol('continue') | ||
export var SKIP = Symbol('skip') | ||
export var EXIT = Symbol('exit') | ||
/** | ||
* Continue traversing as normal | ||
*/ | ||
export const CONTINUE = Symbol('continue') | ||
/** | ||
* Do not traverse this node’s children | ||
*/ | ||
export const SKIP = Symbol('skip') | ||
/** | ||
* Stop traversing immediately | ||
*/ | ||
export const EXIT = Symbol('exit') | ||
/** | ||
* Visit children of tree which pass a test | ||
* | ||
* @param {Node} tree Abstract syntax tree to walk | ||
* @param {Visitor|Visitors} [visitor] Function to run for each node | ||
*/ | ||
export function visit(tree, visitor) { | ||
var enter = visitor | ||
/** @type {Visitor} */ | ||
var enter | ||
/** @type {Visitor} */ | ||
var leave | ||
if (visitor && typeof visitor === 'object') { | ||
if (typeof visitor === 'function') { | ||
enter = visitor | ||
} else if (visitor && typeof visitor === 'object') { | ||
enter = visitor.enter | ||
@@ -21,2 +71,8 @@ leave = visitor.leave | ||
/** | ||
* @param {Node} node | ||
* @param {string?} key | ||
* @param {number?} index | ||
* @param {Array.<Node>} parents | ||
*/ | ||
function build(node, key, index, parents) { | ||
@@ -30,7 +86,16 @@ if (nodelike(node)) { | ||
function visit() { | ||
/** @type {ActionTuple} */ | ||
var result = enter ? toResult(enter(node, key, index, parents)) : [] | ||
/** @type {string} */ | ||
var cKey | ||
/** @type {number} */ | ||
var cIndex | ||
var cParents | ||
var cResult | ||
/** @type {Array.<Node>} */ | ||
var grandparents | ||
/** @type {ActionTuple} */ | ||
var subresult | ||
/** @type {unknown} */ | ||
var value | ||
/** @type {unknown} */ | ||
var subvalue | ||
@@ -50,13 +115,19 @@ if (result[0] === EXIT) { | ||
) { | ||
cParents = parents.concat(node) | ||
value = node[cKey] | ||
if (Array.isArray(node[cKey])) { | ||
grandparents = parents.concat(node) | ||
if (Array.isArray(value)) { | ||
cIndex = 0 | ||
while (cIndex > -1 && cIndex < node[cKey].length) { | ||
if (nodelike(node[cKey][cIndex])) { | ||
cResult = build(node[cKey][cIndex], cKey, cIndex, cParents)() | ||
if (cResult[0] === EXIT) return cResult | ||
// type-coverage:ignore-next-line Looks like `unknown[]`. | ||
while (cIndex > -1 && cIndex < value.length) { | ||
// type-coverage:ignore-next-line Looks like `unknown[]`. | ||
subvalue = value[cIndex] | ||
if (nodelike(subvalue)) { | ||
subresult = build(subvalue, cKey, cIndex, grandparents)() | ||
if (subresult[0] === EXIT) return subresult | ||
cIndex = | ||
typeof cResult[1] === 'number' ? cResult[1] : cIndex + 1 | ||
typeof subresult[1] === 'number' ? subresult[1] : cIndex + 1 | ||
} else { | ||
@@ -66,5 +137,5 @@ cIndex++ | ||
} | ||
} else if (nodelike(node[cKey])) { | ||
cResult = build(node[cKey], cKey, null, cParents)() | ||
if (cResult[0] === EXIT) return cResult | ||
} else if (nodelike(value)) { | ||
subresult = build(value, cKey, null, grandparents)() | ||
if (subresult[0] === EXIT) return subresult | ||
} | ||
@@ -80,4 +151,8 @@ } | ||
/** | ||
* @param {null|undefined|void|Action|Index|ActionTuple} value | ||
* @returns {ActionTuple} | ||
*/ | ||
function toResult(value) { | ||
if (value !== null && typeof value === 'object' && 'length' in value) { | ||
if (Array.isArray(value)) { | ||
return value | ||
@@ -93,2 +168,6 @@ } | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is Node} | ||
*/ | ||
function nodelike(value) { | ||
@@ -98,5 +177,7 @@ return Boolean( | ||
typeof value === 'object' && | ||
// @ts-ignore Looks like a node. | ||
typeof value.type === 'string' && | ||
value.type.length | ||
// @ts-ignore Looks like a node. | ||
value.type.length > 0 | ||
) | ||
} |
{ | ||
"name": "estree-util-visit", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "esast (and estree) utility to visit nodes", | ||
@@ -32,4 +32,6 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"module": "./index.js", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"browser": { | ||
@@ -42,22 +44,33 @@ "./color.js": "./color.browser.js" | ||
"files": [ | ||
"color.d.ts", | ||
"color.js", | ||
"color.browser.d.ts", | ||
"color.browser.js", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": {}, | ||
"dependencies": { | ||
"@types/estree-jsx": "^0.0.1", | ||
"@types/unist": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"acorn": "^8.0.0", | ||
"c8": "^7.5.0", | ||
"nyc": "^15.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"xo": "^0.37.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.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": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --experimental-modules 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" | ||
}, | ||
@@ -74,9 +87,7 @@ "prettier": { | ||
"prettier": true, | ||
"ignores": [], | ||
"rules": { | ||
"import/no-mutable-exports": "off", | ||
"capitalized-comments": "off", | ||
"max-depth": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off", | ||
"unicorn/explicit-length-check": "off" | ||
"prefer-arrow-callback": "off" | ||
} | ||
@@ -88,3 +99,8 @@ }, | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -19,2 +19,5 @@ # estree-util-visit | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm][]: | ||
@@ -26,4 +29,2 @@ | ||
Note that this package is ESM only: it must be imported instead of required. | ||
## Use | ||
@@ -61,5 +62,4 @@ | ||
`estree-util-visit` exports the following identifiers: | ||
[`visit`](#visittree-visitorvisitors), [`EXIT`](#action), [`CONTINUE`](#action), | ||
and [`SKIP`](#action). | ||
This package exports the following identifiers: `visit`, `EXIT`, `CONTINUE`, and | ||
`SKIP`. | ||
There is no default export. | ||
@@ -66,0 +66,0 @@ |
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
16494
9
229
2
11
1
+ Added@types/estree-jsx@^0.0.1
+ Added@types/unist@^2.0.0
+ Added@types/estree@1.0.6(transitive)
+ Added@types/estree-jsx@0.0.1(transitive)
+ Added@types/unist@2.0.11(transitive)