@unified-latex/unified-latex-util-visit
Advanced tools
Comparing version 1.6.1 to 1.7.0
129
index.d.ts
@@ -1,12 +0,125 @@ | ||
export * from "./libs/visit"; | ||
import * as Ast from '@unified-latex/unified-latex-types'; | ||
declare type Action = typeof CONTINUE | typeof SKIP | typeof EXIT; | ||
declare type ActionTuple = [Action] | [typeof SKIP, Index] | [typeof CONTINUE, Index]; | ||
/** | ||
* ## What is this? | ||
* Continue traversing as normal | ||
*/ | ||
export declare const CONTINUE: unique symbol; | ||
/** | ||
* Stop traversing immediately | ||
*/ | ||
export declare const EXIT: unique symbol; | ||
declare type GetGuard<T> = T extends (x: any, ...y: any[]) => x is infer R ? R : never; | ||
/** | ||
* Extracts the guard type from the `test` function provided in a | ||
* `VisitOptions` argument. | ||
*/ | ||
declare type GuardFromOptions<Opts extends VisitOptions, PossibleTypes = Ast.Ast> = Opts extends { | ||
test: infer R; | ||
} ? R extends (x: any, ...y: any[]) => boolean ? Extract<PossibleTypes, GuardTypeOf<R>> : PossibleTypes : PossibleTypes; | ||
/** | ||
* Gets the type that a type-guard function is guarding. If | ||
* the guard type cannot be determined, the input type is returned. | ||
*/ | ||
declare type GuardTypeOf<T extends (x: any, ...y: any[]) => boolean> = GetGuard<T> extends never ? T extends (x: infer A) => any ? A : never : GetGuard<T>; | ||
declare type Index = number; | ||
/** | ||
* Narrow the type `T` based on the `VisitOptions` supplied. If `{includeArrays: false}` | ||
* is specified in the `VisitOptions`, then arrays are excluded from `T`. | ||
*/ | ||
declare type NarrowArraysBasedOnOptions<T, Opts extends VisitOptions> = Opts extends { | ||
includeArrays: infer A; | ||
} ? A extends true ? T : Exclude<T, any[]> : Exclude<T, any[]>; | ||
/** | ||
* Do not traverse this node’s children | ||
*/ | ||
export declare const SKIP: unique symbol; | ||
/** | ||
* Visit children of tree which pass a test | ||
* | ||
* Functions to traverse a `unified-latex` Abstract Syntax Tree (AST). `visit` is | ||
* very similar to [estree-util-visit](https://github.com/syntax-tree/estree-util-visit). | ||
* @param {Node} tree Abstract syntax tree to walk | ||
* @param {Visitor|Visitors} [visitor] Function to run for each node | ||
*/ | ||
export declare function visit<Opts extends VisitOptions>(tree: Ast.Ast, visitor: Visitor<VisitorTypeFromOptions<Opts>> | Visitors<VisitorTypeFromOptions<Opts>>, options?: Opts): void; | ||
export declare type VisitInfo = { | ||
/** | ||
* If the element was accessed via an attribute, the attribute key is specified. | ||
*/ | ||
readonly key: string | undefined; | ||
/** | ||
* If the element was accessed in an array, the index is specified. | ||
*/ | ||
readonly index: number | undefined; | ||
/** | ||
* A list of ancestor nodes, `[parent, grandparent, great-grandparent, ...]` | ||
*/ | ||
readonly parents: (Ast.Node | Ast.Argument)[]; | ||
/** | ||
* If the element was accessed in an array, the array that it is part of. | ||
*/ | ||
readonly containingArray: (Ast.Node | Ast.Argument)[] | undefined; | ||
/** | ||
* The LaTeX context of the current match. | ||
*/ | ||
readonly context: VisitorContext; | ||
}; | ||
declare type VisitOptions = { | ||
startingContext?: VisitorContext; | ||
/** | ||
* Type guard for types that are passed to the `visitor` function. | ||
*/ | ||
test?: (node: Ast.Ast, info: VisitInfo) => boolean; | ||
/** | ||
* Whether arrays will be sent to the `visitor` function. If falsy, | ||
* only nodes will be past to `visitor`. | ||
*/ | ||
includeArrays?: boolean; | ||
}; | ||
/** | ||
* A visitor takes a `node`, `key`, `index`, and ... | ||
* | ||
* ## When should I use this? | ||
* | ||
* If you want to recursively replace particular AST nodes. | ||
* @param key - The key of the parent that we were accessed through. | ||
*/ | ||
//# sourceMappingURL=index.d.ts.map | ||
declare type Visitor<T> = (node: T, info: VisitInfo) => null | undefined | Action | Index | ActionTuple | void; | ||
export declare type VisitorContext = { | ||
/** | ||
* Whether the node is being processed in math mode. | ||
* | ||
* This happens when the node is a director or indirect child | ||
* of a math environment (e.g. `$abc$`), but not when an environment | ||
* re-establishes text mode (e.g. `$\text{abc}$`) | ||
*/ | ||
inMathMode?: boolean; | ||
/** | ||
* Whether the node has any ancestor that is processed in math mode. | ||
*/ | ||
hasMathModeAncestor?: boolean; | ||
}; | ||
declare type Visitors<T> = { | ||
enter?: Visitor<T>; | ||
leave?: Visitor<T>; | ||
}; | ||
/** | ||
* Get the type of the parameter to the `Visitor` function based on the | ||
* `VisitOptions` that are supplied. | ||
*/ | ||
declare type VisitorTypeFromOptions<Opts extends VisitOptions> = NarrowArraysBasedOnOptions<GuardFromOptions<Opts>, Opts>; | ||
export { } |
13
index.js
@@ -1,2 +0,1 @@ | ||
// libs/list-math-children.ts | ||
import { match } from "@unified-latex/unified-latex-util-match"; | ||
@@ -31,8 +30,6 @@ function listMathChildren(node) { | ||
} | ||
// libs/visit.ts | ||
var CONTINUE = Symbol("continue"); | ||
var SKIP = Symbol("skip"); | ||
var EXIT = Symbol("exit"); | ||
var DEFAULT_CONTEXT = { | ||
const CONTINUE = Symbol("continue"); | ||
const SKIP = Symbol("skip"); | ||
const EXIT = Symbol("exit"); | ||
const DEFAULT_CONTEXT = { | ||
inMathMode: false, | ||
@@ -116,4 +113,2 @@ hasMathModeAncestor: false | ||
break; | ||
default: | ||
break; | ||
} | ||
@@ -120,0 +115,0 @@ const mathModeProps = listMathChildren(node); |
{ | ||
"name": "@unified-latex/unified-latex-util-visit", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Functions for traversing a unified-latex AST", | ||
@@ -8,4 +8,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"@unified-latex/unified-latex-types": "^1.6.1", | ||
"@unified-latex/unified-latex-util-match": "^1.6.1" | ||
"@unified-latex/unified-latex-types": "^1.7.0", | ||
"@unified-latex/unified-latex-util-match": "^1.7.0" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
48294
7
443