With snapdragon v0.9.0 and higher you can use this.node() to create a new Node, whenever it makes sense.
var Node =require('snapdragon-node');var Snapdragon =require('snapdragon');var snapdragon =newSnapdragon();// example usage inside a parser visitor functionsnapdragon.parser.set('foo',function(){ // get the current "start" positionvar pos =this.position(); // returns the match if regex matches the substring // at the current position on `parser.input`var match =this.match(/foo/);if (match) { // call "pos" on the node, to set the start and end // positions, and return the node to push it onto the AST // (snapdragon will push the node onto the correct // nodes array, based on the stack)returnpos(newNode({type:'bar', val: match[0]}));}});
Returns true if node.val is an empty string, or node.nodes does not contain any non-empty text nodes.
Params
fn{Function}: (optional) Filter function that is called on node and/or child nodes. isEmpty will return false immediately when the filter function returns false on any nodes.
snapdragon: Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
var child = node.find(1); //<= index of the node to get
var child = node.find('foo'); //<= node.type of a child node
var child = node.find(/^(foo|bar)$/); //<= regex to match node.type
var child = node.find(['foo', 'bar']); //<= array of node.type(s)
var node = new Node({type: 'bar'});
cosole.log(node.isType('foo')); // false
cosole.log(node.isType(/^(foo|bar)$/)); // true
cosole.log(node.isType(['foo', 'bar'])); // true
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
foo.push(bar);
cosole.log(foo.hasType('qux')); // false
cosole.log(foo.hasType(/^(qux|bar)$/)); // true
cosole.log(foo.hasType(['qux', 'bar'])); // true
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
foo.push(bar);
foo.push(baz);
console.log(bar.siblings.length) // 2
console.log(baz.siblings.length) // 2
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
var qux = new Node({type: 'qux'});
foo.push(bar);
foo.push(baz);
foo.unshift(qux);
console.log(bar.index) // 1
console.log(baz.index) // 2
console.log(qux.index) // 0
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
foo.push(bar);
foo.push(baz);
console.log(baz.prev.type) // 'bar'
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
foo.push(bar);
foo.push(baz);
console.log(bar.siblings.length) // 2
console.log(baz.siblings.length) // 2
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
var qux = new Node({type: 'qux'});
foo.push(bar);
foo.push(baz);
foo.push(qux);
console.log(foo.first.type) // 'bar'
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
var qux = new Node({type: 'qux'});
foo.push(bar);
foo.push(baz);
foo.push(qux);
console.log(foo.last.type) // 'qux'
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
var qux = new Node({type: 'qux'});
foo.push(bar);
foo.push(baz);
foo.push(qux);
console.log(foo.last.type) // 'qux'