Improve types for visitor keys (#18095)
This commit is contained in:
1 parent
eca05c1750
commit
6a3797983a
3 files changed
+30
-19
No files matched your search
@@ -15,9 +15,9 @@ import isNodeMatches from "./is-node-matches.js";
|
|||||||
import isTsKeywordType from "./is-ts-keyword-type.js";
|
import isTsKeywordType from "./is-ts-keyword-type.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @import * as Estree from "../types/estree.js"
|
@import * as Estree from "../types/estree.js"
|
||||||
* @import AstPath from "../../common/ast-path.js"
|
@import AstPath from "../../common/ast-path.js"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Estree.Node} node
|
* @param {Estree.Node} node
|
||||||
|
|||||||
+15
-16
@@ -1,15 +1,14 @@
|
|||||||
import isObject from "./is-object.js";
|
import isObject from "./is-object.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {NonNullable<object>} Node
|
@import {GetVisitorKeys, Node} from "../utils/create-get-visitor-keys.js"
|
||||||
* @typedef {(unknown) => string[]} GetVisitorKeys
|
@typedef {(node: Node) => boolean} Predicate
|
||||||
* @typedef {(unknown) => boolean} Predicate
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Node} node
|
@param {Node} node
|
||||||
* @param {{getVisitorKeys: GetVisitorKeys, filter?: Predicate}} options
|
@param {{getVisitorKeys: GetVisitorKeys, filter?: Predicate}} options
|
||||||
*/
|
*/
|
||||||
function* getChildren(node, options) {
|
function* getChildren(node, options) {
|
||||||
const { getVisitorKeys, filter = () => true } = options;
|
const { getVisitorKeys, filter = () => true } = options;
|
||||||
const isMatchedNode = (node) => isObject(node) && filter(node);
|
const isMatchedNode = (node) => isObject(node) && filter(node);
|
||||||
@@ -30,9 +29,9 @@ function* getChildren(node, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Node} node
|
@param {Node} node
|
||||||
* @param {{getVisitorKeys: GetVisitorKeys, filter?: Predicate}} options
|
@param {{getVisitorKeys: GetVisitorKeys, filter?: Predicate}} options
|
||||||
*/
|
*/
|
||||||
function* getDescendants(node, options) {
|
function* getDescendants(node, options) {
|
||||||
const queue = [node];
|
const queue = [node];
|
||||||
for (let index = 0; index < queue.length; index++) {
|
for (let index = 0; index < queue.length; index++) {
|
||||||
@@ -46,17 +45,17 @@ function* getDescendants(node, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Node} node
|
@param {Node} node
|
||||||
* @param {{getVisitorKeys: GetVisitorKeys}} options
|
@param {{getVisitorKeys: GetVisitorKeys}} options
|
||||||
*/
|
*/
|
||||||
function isLeaf(node, options) {
|
function isLeaf(node, options) {
|
||||||
return getChildren(node, options).next().done;
|
return getChildren(node, options).next().done;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Node} node
|
@param {Node} node
|
||||||
* @param {{getVisitorKeys: GetVisitorKeys, predicate: Predicate}} options
|
@param {{getVisitorKeys: GetVisitorKeys, predicate: Predicate}} options
|
||||||
*/
|
*/
|
||||||
function hasDescendant(node, { getVisitorKeys, predicate }) {
|
function hasDescendant(node, { getVisitorKeys, predicate }) {
|
||||||
for (const descendant of getDescendants(node, { getVisitorKeys })) {
|
for (const descendant of getDescendants(node, { getVisitorKeys })) {
|
||||||
if (predicate(descendant)) {
|
if (predicate(descendant)) {
|
||||||
|
|||||||
@@ -1,8 +1,20 @@
|
|||||||
import toFastProperties from "to-fast-properties";
|
import toFastProperties from "to-fast-properties";
|
||||||
|
|
||||||
|
/**
|
||||||
|
@typedef {(node: Node) => VisitorKeys} GetVisitorKeys
|
||||||
|
@typedef {NonNullable<object>} Node
|
||||||
|
@typedef {readonly string[]} VisitorKeys
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
@param {Record<string, VisitorKeys>} visitorKeys
|
||||||
|
@param {string} [typeProperty="type"]
|
||||||
|
@returns {GetVisitorKeys}
|
||||||
|
*/
|
||||||
function createGetVisitorKeys(visitorKeys, typeProperty = "type") {
|
function createGetVisitorKeys(visitorKeys, typeProperty = "type") {
|
||||||
toFastProperties(visitorKeys);
|
toFastProperties(visitorKeys);
|
||||||
|
|
||||||
|
/** @type {GetVisitorKeys} */
|
||||||
function getVisitorKeys(node) {
|
function getVisitorKeys(node) {
|
||||||
const type = node[typeProperty];
|
const type = node[typeProperty];
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user