Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#67979 [css-tree] Add types for walk.break …
Browse files Browse the repository at this point in the history
…and walk.skip by @jdufresne

These properties are documented at:

https://github.com/csstree/csstree/blob/master/docs/traversal.md

> walk.break or this.break – stops traversal, i.e. no visitor function will be invoked once this value is returned by a
> visitor;

> walk.skip or this.skip – prevent current node from being iterated, i.e. no visitor function will be invoked for its
> properties or children nodes;

They can also be seen in the library code at:
https://github.com/csstree/csstree/blob/ba6dfd8bb0e33055c05f13803d04825d98dd2d8d/lib/walker/create.js#L242-L243

Co-authored-by: Seth Falco <seth@falco.fun>
  • Loading branch information
jdufresne and SethFalco committed Jan 8, 2024
1 parent 7836c1f commit 9d07a5f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions types/css-tree/css-tree-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ csstree.walk(ast, {});

csstree.walk(ast, {
enter(node, item, list) {
this.break; // $ExpectType symbol
this.skip; // $ExpectType symbol
this.root; // $ExpectType CssNode
this.stylesheet; // $ExpectType StyleSheet | null
node; // $ExpectType ClassSelector
Expand All @@ -24,6 +26,8 @@ csstree.walk(ast, {
this.atrule; // $ExpectType Atrule | null
},
leave(node, item, list) {
this.break; // $ExpectType symbol
this.skip; // $ExpectType symbol
this.root; // $ExpectType CssNode
this.stylesheet; // $ExpectType StyleSheet | null
node; // $ExpectType ClassSelector
Expand All @@ -35,6 +39,9 @@ csstree.walk(ast, {
reverse: false,
});

csstree.walk.break; // $ExpectType symbol
csstree.walk.skip; // $ExpectType symbol

const findResult = csstree.find(ast, (node, item, list) => {
node; // $ExpectType CssNode
item; // $ExpectType ListItem<CssNode>
Expand Down
26 changes: 25 additions & 1 deletion types/css-tree/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,18 @@ export interface GenerateOptions {
export function generate(ast: CssNode, options?: GenerateOptions): string;

export interface WalkContext {
/**
* Stops traversal. No visitor function will be invoked once this value is
* returned by a visitor.
*/
break: symbol;
/**
* Prevent the current node from being iterated. No visitor function will be
* invoked for its properties or children nodes; note that this value only
* has an effect for enter visitor as leave visitor invokes after iterating
* over all node's properties and children.
*/
skip: symbol;
root: CssNode;
stylesheet: StyleSheet | null;
atrule: Atrule | null;
Expand Down Expand Up @@ -601,7 +613,19 @@ export type WalkOptions =
| WalkOptionsVisit<WhiteSpace>
| WalkOptionsNoVisit;

export function walk(ast: CssNode, options: EnterOrLeaveFn | WalkOptions): void;
export const walk: {
(ast: CssNode, options: EnterOrLeaveFn | WalkOptions): void;
/**
* Stops traversal. No visitor function will be invoked once this value is returned by a visitor.
*/
readonly break: symbol;
/**
* Prevent the current node from being iterated. No visitor function will be invoked for its properties or children
* nodes; note that this value only has an effect for enter visitor as leave visitor invokes after iterating over
* all node's properties and children.
*/
readonly skip: symbol;
};

export type FindFn = (this: WalkContext, node: CssNode, item: ListItem<CssNode>, list: List<CssNode>) => boolean;

Expand Down

0 comments on commit 9d07a5f

Please sign in to comment.