Method visits each node of passed AST in a natural way and calls handlers for each one. It takes two arguments: a root node (ast) and an object (options). In simple case, it may take a function (handler) instead of options (walk(ast, fn) is equivalent to walk(ast, { enter: fn })).
import { parse, walk } from '@eslint/css-tree';
const ast = parse('.a { color: red; }');
walk(ast, function(node) {
console.log(node.type);
});
// StyleSheet
// Rule
// SelectorList
// Selector
// ClassSelector
// Block
// Declaration
// Value
// IdentifierHow it works:
- Method uses
structurefield value of every node type to define the way how to iterate node's properties:- A function-iterator is generating for every node type.
- Node's properties are iterated in the order as defined in
structure(reverse option inverts the order). - Properties that are not defined in
structureare ignored (won't be iterated over). - An exception is possible when a tree is not following to expected structure (e.g. AST was built outside the CSSTree parser or transformed in a wrong way). In case you are not sure about correctness of the tree structure, you may use
try/catchor check the tree structure withcsstree.lexer.checkStructure(ast)before iterating.
- Only
childrenfield may contain a list of nested nodes. A list of nodes should aListinstances. SinceListclass provides API similar toArray, traversal may work in cases whenchildrenis an array, but without any guarantee. Using arrays in AST is not recommended, use it on your own risk.
Walk visitor's function may return special values to control traversal:
walk.breakorthis.break– stops traversal, i.e. no visitor function will be invoked once this value is returned by a visitor;walk.skiporthis.skip– prevent current node from being iterated, i.e. no visitor function will be invoked for its properties or children nodes; note that this value only has an effect forentervisitor asleavevisitor invokes after iterating over all node's properties and children.
NOTE:
walk.breakandwalk.skipare only possible option for arrow functions, since such functions don't have their ownthis.
csstree.walk(ast, {
enter(node) {
if (node.type === 'Block') {
return this.skip;
}
if (node.name === 'foo') {
return this.break;
}
},
leave: node => node.name === 'bar' ? csstree.walk.break : false
});Options:
Type: function or undefined
Default: undefined
Handler on node entrance, i.e. before any nested node is processed.
import { parse, walk } from '@eslint/css-tree';
const ast = parse('.a { color: red; }');
walk(ast, {
enter(node) {
console.log(node.type);
}
});
// StyleSheet
// Rule
// SelectorList
// Selector
// ClassSelector
// Block
// Declaration
// Value
// IdentifierIn case options has a single enter field, it can be replaced for the handler passed as a value for enter, i.e. walk(ast, { enter: fn }) → walk(ast, fn).
Handler receives three arguments:
node– the AST node a walker entering toitem– node wrapper, that contains references toprevandnextnodes in a list, anddatareference for the nodelist– is a reference for the list; it's useful for list operations likeremove()orinsert()
NOTE: If
childrenis an array, the last two arguments areindexandarray, like forArray#forEach()orArray#map()methods.
import { parse, walk, generate } from '@eslint/css-tree';
const ast = parse(`
.a { foo: 1; bar: 2; }
.b { bar: 3; baz: 4; }
`);
// remove declarations with `bar` property from the tree
walk(ast, (node, item, list) => {
if (node.type === 'Declaration' && node.property === 'bar' && list) {
// remove a declaration from a list it
list.remove(item);
}
});
console.log(generate(ast));
// .a{foo:1}.b{baz:4}NOTE:
itemandlistare not defined for nodes that are not in a list. EvenDeclarationcan be outside any list in case it is a root of tree or a part of@supportsprelude, e.g.@supports (bar: 123) { ... }. Therefore, it's recommended to checkitemorlistare defined before using of it (those values both are defined or both are undefined, so it's enough to test one of them)- Only
Listinstances are safe for tree transformations such as node removal. In case you perform such operations, you can ensure that allchildrenin a tree is aListinstances by callingcsstree.fromPlainObject(ast)before traversal.- It's better to use
visitoption when possible to reach better performance
Context (this) for a handler is an object with a references to the closest ancestor nodes:
root– refers to AST root node (actually it's a node passed towalk()method)stylesheet– refers toStyleSheetnode, usually it's a root nodeatrule– refers to closestAtrulenode if anyatrulePrelude– refers toAtrulePreludenode if anyrule– refers to closestRulenode if anyselector– refers toSelectorListnode if anyblock- refers to closestBlocknode if anydeclaration– refers toDeclarationnode if anyfunction– refers to closestFunction,PseudoClassSelectororPseudoElementSelectornode if current node inside one of them
import { parse, walk } from '@eslint/css-tree';
const ast = parse(`
@import url(import.css);
.foo { background: url('foo.jpg'); }
.bar { background-image: url(bar.png); }
`);
// collect all urls in declarations
const urls = [];
walk(ast, function(node) {
if (this.declaration !== null && node.type === 'Url') {
urls.push(node.value);
}
});
console.log(urls);
// [ 'foo.jpg', 'bar.png' ]Type: function or undefined
Default: undefined
The same as enter handler but invokes on node exit, i.e. after all nested nodes are processed.
import { parse, walk } from '@eslint/css-tree';
const ast = parse('.a { color: red; }');
walk(ast, {
leave(node) {
console.log(node.type);
}
});
// ClassSelector
// Selector
// SelectorList
// Identifier
// Value
// Declaration
// Block
// Rule
// StyleSheetType: string or null
Default: null
Invokes a handler for a specified node type only.
import { parse, walk } from '@eslint/css-tree';
const ast = parse('.a { color: red; } .b { color: green; }');
walk(ast, {
visit: 'ClassSelector',
enter(node) {
console.log(node.name);
}
});
// example above is equivalent to
walk(ast, {
enter(node) {
if (node.type === 'ClassSelector') {
console.log(node.name);
}
}
});The traversal for some node types can perform faster (10-15 times depending on the CSS structure), because some subtrees may to be skipped since they can't contain a node of specified type (e.g. Rule can't be used inside of Declaration, so declaration's subtree can be excluded from traversal path). Fast traversal is supported for node types:
AtruleRuleDeclaration
NOTE: When fast traversal is applied, some nodes may not be reached in case of an incorrect location in the tree. That's may happen if AST was built outside the CSSTree parser or transformed in a wrong way. If you need to be 100% sure that every node of type will be visited (even in wrong position), don't use
visitoption and test node type by your own.
Type: boolean
Default: false
Inverts the natural order of node traversing:
- node's properties are iterated in reverse order to the node's
structuredefinition - children nodes are iterated from last to first
import * as csstree from '@eslint/css-tree';
const ast = csstree.parse('.a { color: red; }');
csstree.walk(ast, {
enter(node) {
console.log(`enter ${node.type}`);
},
leave(node) {
console.log(`leave ${node.type}`);
}
});
// enter StyleSheet
// enter Rule
// enter SelectorList
// enter Selector
// enter ClassSelector
// leave ClassSelector
// leave Selector
// leave SelectorList
// enter Block
// enter Declaration
// enter Value
// enter Identifier
// leave Identifier
// leave Value
// leave Declaration
// leave Block
// leave Rule
// leave StyleSheet
csstree.walk(ast, {
reverse: true, // !!!
enter(node) {
console.log(`enter ${node.type}`);
},
leave(node) {
console.log(`leave ${node.type}`);
}
});
// enter StyleSheet
// enter Rule
// enter Block
// enter Declaration
// enter Value
// enter Identifier
// leave Identifier
// leave Value
// leave Declaration
// leave Block
// enter SelectorList
// enter Selector
// enter ClassSelector
// leave ClassSelector
// leave Selector
// leave SelectorList
// leave Rule
// leave StyleSheetReturns the first node in natural order for which fn function returns a truthy value.
import * as csstree from '@eslint/css-tree';
const ast = csstree.parse('.a { color: red; } .b { color: green; }');
const firstColorDeclaration = csstree.find(ast, (node, item, list) =>
node.type === 'Declaration' && node.property === 'color'
);
console.log(csstree.generate(firstColorDeclaration));
// color:redReturns the first node in reverse order for which fn function returns a truthy value.
import * as csstree from '@eslint/css-tree';
const ast = csstree.parse('.a { color: red; } .b { color: green; }');
const firstColorDeclaration = csstree.findLast(ast, (node, item, list) =>
node.type === 'Declaration' && node.property === 'color'
);
console.log(csstree.generate(firstColorDeclaration));
// color:greenReturns all nodes in natural order for which fn function returns a truthy value.
import * as csstree from '@eslint/css-tree';
const ast = csstree.parse('.a { color: red; } .b { color: green; }');
const colorDeclarations = csstree.findAll(ast, (node, item, list) =>
node.type === 'Declaration' && node.property === 'color'
);
console.log(colorDeclarations.map(decl => csstree.generate(decl)).join(', '));
// color:red, color:green