pax_global_header00006660000000000000000000000064130511222470014507gustar00rootroot0000000000000052 comment=f53bb87e762536695c2440de55aa64f51cef7fb0 snapdragon-node-1.0.6/000077500000000000000000000000001305112224700145725ustar00rootroot00000000000000snapdragon-node-1.0.6/.editorconfig000066400000000000000000000004051305112224700172460ustar00rootroot00000000000000root = true [*] indent_style = space end_of_line = lf charset = utf-8 indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true [{**/{actual,fixtures,expected,templates}/**,*.md}] trim_trailing_whitespace = false insert_final_newline = falsesnapdragon-node-1.0.6/.eslintrc.json000066400000000000000000000071251305112224700173730ustar00rootroot00000000000000{ "ecmaFeatures": { "modules": true, "experimentalObjectRestSpread": true }, "env": { "browser": false, "es6": true, "node": true, "mocha": true }, "globals": { "document": false, "navigator": false, "window": false }, "rules": { "accessor-pairs": 2, "arrow-spacing": [2, { "before": true, "after": true }], "block-spacing": [2, "always"], "brace-style": [2, "1tbs", { "allowSingleLine": true }], "comma-dangle": [2, "never"], "comma-spacing": [2, { "before": false, "after": true }], "comma-style": [2, "last"], "constructor-super": 2, "curly": [2, "multi-line"], "dot-location": [2, "property"], "eol-last": 2, "eqeqeq": [2, "allow-null"], "generator-star-spacing": [2, { "before": true, "after": true }], "handle-callback-err": [2, "^(err|error)$" ], "indent": [2, 2, { "SwitchCase": 1 }], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "keyword-spacing": [2, { "before": true, "after": true }], "new-cap": [2, { "newIsCap": true, "capIsNew": false }], "new-parens": 2, "no-array-constructor": 2, "no-caller": 2, "no-class-assign": 2, "no-cond-assign": 2, "no-const-assign": 2, "no-control-regex": 2, "no-debugger": 2, "no-delete-var": 2, "no-dupe-args": 2, "no-dupe-class-members": 2, "no-dupe-keys": 2, "no-duplicate-case": 2, "no-empty-character-class": 2, "no-eval": 2, "no-ex-assign": 2, "no-extend-native": 2, "no-extra-bind": 2, "no-extra-boolean-cast": 2, "no-extra-parens": [2, "functions"], "no-fallthrough": 2, "no-floating-decimal": 2, "no-func-assign": 2, "no-implied-eval": 2, "no-inner-declarations": [2, "functions"], "no-invalid-regexp": 2, "no-irregular-whitespace": 2, "no-iterator": 2, "no-label-var": 2, "no-labels": 2, "no-lone-blocks": 2, "no-mixed-spaces-and-tabs": 2, "no-multi-spaces": 2, "no-multi-str": 2, "no-multiple-empty-lines": [2, { "max": 1 }], "no-native-reassign": 0, "no-negated-in-lhs": 2, "no-new": 2, "no-new-func": 2, "no-new-object": 2, "no-new-require": 2, "no-new-wrappers": 2, "no-obj-calls": 2, "no-octal": 2, "no-octal-escape": 2, "no-proto": 0, "no-redeclare": 2, "no-regex-spaces": 2, "no-return-assign": 2, "no-self-compare": 2, "no-sequences": 2, "no-shadow-restricted-names": 2, "no-spaced-func": 2, "no-sparse-arrays": 2, "no-this-before-super": 2, "no-throw-literal": 2, "no-trailing-spaces": 0, "no-undef": 2, "no-undef-init": 2, "no-unexpected-multiline": 2, "no-unneeded-ternary": [2, { "defaultAssignment": false }], "no-unreachable": 2, "no-unused-vars": [2, { "vars": "all", "args": "none" }], "no-useless-call": 0, "no-with": 2, "one-var": [0, { "initialized": "never" }], "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], "padded-blocks": [0, "never"], "quotes": [2, "single", "avoid-escape"], "radix": 2, "semi": [2, "always"], "semi-spacing": [2, { "before": false, "after": true }], "space-before-blocks": [2, "always"], "space-before-function-paren": [2, "never"], "space-in-parens": [2, "never"], "space-infix-ops": 2, "space-unary-ops": [2, { "words": true, "nonwords": false }], "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], "use-isnan": 2, "valid-typeof": 2, "wrap-iife": [2, "any"], "yoda": [2, "never"] } } snapdragon-node-1.0.6/.gitattributes000066400000000000000000000002001305112224700174550ustar00rootroot00000000000000# Enforce Unix newlines * text eol=lf # binaries *.ai binary *.psd binary *.jpg binary *.gif binary *.png binary *.jpeg binary snapdragon-node-1.0.6/.github/000077500000000000000000000000001305112224700161325ustar00rootroot00000000000000snapdragon-node-1.0.6/.github/contributing.md000066400000000000000000000063411305112224700211670ustar00rootroot00000000000000# Contributing to snapdragon-node First and foremost, thank you! We appreciate that you want to contribute to snapdragon-node, your time is valuable, and your contributions mean a lot to us. **What does "contributing" mean?** Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following: - Updating or correcting documentation - Feature requests - Bug reports If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information. **Showing support for snapdragon-node** Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use. Don't have time to contribute? No worries, here are some other ways to show your support for snapdragon-node: - star the [project](https://github.com/jonschlinkert/snapdragon-node) - tweet your support for snapdragon-node ## Issues ### Before creating an issue Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues. Try to follow these guidelines - **Investigate the issue**: - **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue. - Create the issue in the appropriate repository. ### Creating an issue Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue: - **version**: please note the version of snapdragon-node are you using - **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using - **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/) ## Above and beyond Here are some tips for creating idiomatic issues. Taking just a little bit extra time will make your issue easier to read, easier to resolve, more likely to be found by others who have the same or similar issue in the future. - read the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) - take some time to learn basic markdown. This [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601) is super helpful, as is the GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/). - Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/). - use backticks to wrap code. This ensures that code will retain its format, making it much more readable to others - use syntax highlighting by adding the correct language name after the first "code fence" [node-glob]: https://github.com/isaacs/node-glob [micromatch]: https://github.com/jonschlinkert/micromatch [so]: http://stackoverflow.com/questions/tagged/snapdragon-nodesnapdragon-node-1.0.6/.gitignore000066400000000000000000000004171305112224700165640ustar00rootroot00000000000000# always ignore files *.DS_Store *.sublime-* # test related, or directories generated by tests test/actual actual coverage .nyc* # npm node_modules npm-debug.log # yarn yarn.lock yarn-error.log # misc _gh_pages _draft _drafts bower_components vendor temp tmp TODO.md snapdragon-node-1.0.6/.travis.yml000066400000000000000000000002071305112224700167020ustar00rootroot00000000000000sudo: false os: - linux - osx language: node_js node_js: - node - '6' - '4.7' matrix: allow_failures: - node_js: '4.7' snapdragon-node-1.0.6/.verb.md000066400000000000000000000015341305112224700161330ustar00rootroot00000000000000## Usage With [snapdragon][] v0.9.0 and higher you can use `this.node()` to create a new `Node`, whenever it makes sense. ```js var Node = require('{%= name %}'); var Snapdragon = require('snapdragon'); var snapdragon = new Snapdragon(); // example usage inside a parser visitor function snapdragon.parser.set('foo', function() { var pos = this.position(); // if the regex matches the substring at the current position // on `this.input`, return the match var match = this.match(/foo/); if (match) { // if node.type is not defined on the node, the parser // will automatically add it var node = pos(new Node(match[0])); // or, explictly pass a type var node = pos(new Node(match[0], 'bar')); // or var node = pos(new Node({type: 'bar', val: match[0]})); return node; } }); ``` ## API {%= apidocs("index.js") %} snapdragon-node-1.0.6/LICENSE000066400000000000000000000020711305112224700155770ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2017 Jon Schlinkert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. snapdragon-node-1.0.6/README.md000066400000000000000000000222631305112224700160560ustar00rootroot00000000000000# snapdragon-node [![NPM version](https://img.shields.io/npm/v/snapdragon-node.svg?style=flat)](https://www.npmjs.com/package/snapdragon-node) [![NPM monthly downloads](https://img.shields.io/npm/dm/snapdragon-node.svg?style=flat)](https://npmjs.org/package/snapdragon-node) [![NPM total downloads](https://img.shields.io/npm/dt/snapdragon-node.svg?style=flat)](https://npmjs.org/package/snapdragon-node) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/snapdragon-node.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/snapdragon-node) > Snapdragon utility for creating a new AST node in custom code, such as plugins. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save snapdragon-node ``` ## Usage With [snapdragon](https://github.com/jonschlinkert/snapdragon) v0.9.0 and higher you can use `this.node()` to create a new `Node`, whenever it makes sense. ```js var Node = require('snapdragon-node'); var Snapdragon = require('snapdragon'); var snapdragon = new Snapdragon(); // example usage inside a parser visitor function snapdragon.parser.set('foo', function() { var pos = this.position(); // if the regex matches the substring at the current position // on `this.input`, return the match var match = this.match(/foo/); if (match) { // if node.type is not defined on the node, the parser // will automatically add it var node = pos(new Node(match[0])); // or, explictly pass a type var node = pos(new Node(match[0], 'bar')); // or var node = pos(new Node({type: 'bar', val: match[0]})); return node; } }); ``` ## API ### [Node](index.js#L20) Create a new AST `Node` with the given `val` and `type`. **Params** * `val` **{String|Object}**: Pass a matched substring, or an object to merge onto the node. * `type` **{String}**: The node type to use when `val` is a string. * `returns` **{Object}**: node instance **Example** ```js var node = new Node('*', 'Star'); var node = new Node({type: 'star', val: '*'}); ``` ### [.define](index.js#L50) Define a non-enumberable property on the node instance. **Params** * `name` **{String}** * `val` **{any}** * `returns` **{Object}**: returns the node instance **Example** ```js var node = new Node(); node.define('foo', 'something non-enumerable'); ``` ### [.pushNode](index.js#L70) Given node `foo` and node `bar`, push node `bar` onto `foo.nodes`, and set `foo` as `bar.parent`. **Params** * `node` **{Object}** * `returns` **{undefined}** **Example** ```js var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); foo.pushNode(bar); ``` ### [.addNode](index.js#L82) Alias for [pushNode](#pushNode) for backwards compatibility with 0.1.0. ### [.unshiftNode](index.js#L101) Given node `foo` and node `bar`, unshift node `bar` onto `foo.nodes`, and set `foo` as `bar.parent`. **Params** * `node` **{Object}** * `returns` **{undefined}** **Example** ```js var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); foo.unshiftNode(bar); ``` ### [.getNode](index.js#L123) Get the first child node from `node.nodes` that matches the given `type`. If `type` is a number, the child node at that index is returned. **Params** * `type` **{String}** * `returns` **{Object}**: Returns a child node or undefined. **Example** ```js var child = node.getNode(1); //<= index of the node to get var child = node.getNode('foo'); var child = node.getNode(/^(foo|bar)$/); var child = node.getNode(['foo', 'bar']); ``` ### [.isType](index.js#L142) Return true if the node is the given `type`. **Params** * `type` **{String}** * `returns` **{Boolean}** **Example** ```js 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 ``` ### [.hasType](index.js#L164) Return true if the `node.nodes` has the given `type`. **Params** * `type` **{String}** * `returns` **{Boolean}** **Example** ```js var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); foo.pushNode(bar); cosole.log(foo.hasType('qux')); // false cosole.log(foo.hasType(/^(qux|bar)$/)); // true cosole.log(foo.hasType(['qux', 'bar'])); // true ``` ### [.siblings](index.js#L186) Get the siblings array, or `null` if it doesn't exist. * `returns` **{Array}** **Example** ```js var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); console.log(bar.siblings.length) // 2 console.log(baz.siblings.length) // 2 ``` ### [.prev](index.js#L209) Get the previous node from the siblings array or `null`. * `returns` **{Object}** **Example** ```js var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); console.log(baz.prev.type) // 'bar' ``` ### [.next](index.js#L235) Get the siblings array, or `null` if it doesn't exist. * `returns` **{Object}** **Example** ```js var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); console.log(bar.siblings.length) // 2 console.log(baz.siblings.length) // 2 ``` ### [.index](index.js#L265) Get the node's current index from `node.parent.nodes`. This should always be correct, even when the parent adds nodes. * `returns` **{Number}** **Example** ```js 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.pushNode(bar); foo.pushNode(baz); foo.unshiftNode(qux); console.log(bar.index) // 1 console.log(baz.index) // 2 console.log(qux.index) // 0 ``` ### [.first](index.js#L290) Get the first node from `node.nodes`. * `returns` **{Object}**: The first node, or undefiend **Example** ```js 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.pushNode(bar); foo.pushNode(baz); foo.pushNode(qux); console.log(foo.first.type) // 'bar' ``` ### [.last](index.js#L315) Get the last node from `node.nodes`. * `returns` **{Object}**: The last node, or undefiend **Example** ```js 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.pushNode(bar); foo.pushNode(baz); foo.pushNode(qux); console.log(foo.last.type) // 'qux' ``` ## About ### Related projects * [breakdance](https://www.npmjs.com/package/breakdance): Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy… [more](http://breakdance.io) | [homepage](http://breakdance.io "Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy to use. It's time for your markup to get down.") * [snapdragon-capture](https://www.npmjs.com/package/snapdragon-capture): Snapdragon plugin that adds a capture method to the parser instance. | [homepage](https://github.com/jonschlinkert/snapdragon-capture "Snapdragon plugin that adds a capture method to the parser instance.") * [snapdragon-cheerio](https://www.npmjs.com/package/snapdragon-cheerio): Snapdragon plugin for converting a cheerio AST to a snapdragon AST. | [homepage](https://github.com/jonschlinkert/snapdragon-cheerio "Snapdragon plugin for converting a cheerio AST to a snapdragon AST.") * [snapdragon-util](https://www.npmjs.com/package/snapdragon-util): Utilities for the snapdragon parser/compiler. | [homepage](https://github.com/jonschlinkert/snapdragon-util "Utilities for the snapdragon parser/compiler.") * [snapdragon](https://www.npmjs.com/package/snapdragon): Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… [more](https://github.com/jonschlinkert/snapdragon) | [homepage](https://github.com/jonschlinkert/snapdragon "Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map support.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards. ### Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` ### 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: ```sh $ npm install && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). MIT *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 15, 2017._snapdragon-node-1.0.6/index.js000066400000000000000000000160351305112224700162440ustar00rootroot00000000000000'use strict'; var getters = ['siblings', 'index', 'first', 'last', 'prev', 'next']; var utils = require('./utils'); /** * Create a new AST `Node` with the given `val` and `type`. * * ```js * var node = new Node('*', 'Star'); * var node = new Node({type: 'star', val: '*'}); * ``` * @name Node * @param {String|Object} `val` Pass a matched substring, or an object to merge onto the node. * @param {String} `type` The node type to use when `val` is a string. * @return {Object} node instance * @api public */ var Node = exports = module.exports = function Node(val, type) { this.define('isNode', true); this.type = null; if (utils.isObject(val)) { for (var key in val) { if (getters.indexOf(key) === -1) { this[key] = val[key]; } } } else { this.type = type; this.val = val; } }; /** * Define a non-enumberable property on the node instance. * * ```js * var node = new Node(); * node.define('foo', 'something non-enumerable'); * ``` * @name .define * @param {String} `name` * @param {any} `val` * @return {Object} returns the node instance * @api public */ Node.prototype.define = function(name, val) { utils.define(this, name, val); return this; }; /** * Given node `foo` and node `bar`, push node `bar` onto `foo.nodes`, and * set `foo` as `bar.parent`. * * ```js * var foo = new Node({type: 'foo'}); * var bar = new Node({type: 'bar'}); * foo.pushNode(bar); * ``` * @name .pushNode * @param {Object} `node` * @return {undefined} * @api public */ Node.prototype.pushNode = function(node) { this.nodes = this.nodes || []; utils.define(node, 'parent', this); this.nodes.push(node); }; /** * Alias for [pushNode](#pushNode) for backwards compatibility with 0.1.0. * @name .addNode * @api public */ Node.prototype.addNode = function(node) { return this.pushNode(node); }; /** * Given node `foo` and node `bar`, unshift node `bar` onto `foo.nodes`, and * set `foo` as `bar.parent`. * * ```js * var foo = new Node({type: 'foo'}); * var bar = new Node({type: 'bar'}); * foo.unshiftNode(bar); * ``` * @name .unshiftNode * @param {Object} `node` * @return {undefined} * @api public */ Node.prototype.unshiftNode = function(node) { this.nodes = this.nodes || []; utils.define(node, 'parent', this); this.nodes.unshift(node); }; /** * Get the first child node from `node.nodes` that matches the given `type`. * If `type` is a number, the child node at that index is returned. * * ```js * var child = node.getNode(1); //<= index of the node to get * var child = node.getNode('foo'); * var child = node.getNode(/^(foo|bar)$/); * var child = node.getNode(['foo', 'bar']); * ``` * @name .getNode * @param {String} `type` * @return {Object} Returns a child node or undefined. * @api public */ Node.prototype.getNode = function(type) { return utils.su.getNode(this.nodes, type); }; /** * Return true if the node is the given `type`. * * ```js * 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 * ``` * @name .isType * @param {String} `type` * @return {Boolean} * @api public */ Node.prototype.isType = function(type) { return utils.su.isType(this, type); }; /** * Return true if the `node.nodes` has the given `type`. * * ```js * var foo = new Node({type: 'foo'}); * var bar = new Node({type: 'bar'}); * foo.pushNode(bar); * * cosole.log(foo.hasType('qux')); // false * cosole.log(foo.hasType(/^(qux|bar)$/)); // true * cosole.log(foo.hasType(['qux', 'bar'])); // true * ``` * @name .hasType * @param {String} `type` * @return {Boolean} * @api public */ Node.prototype.hasType = function(type) { return utils.su.hasType(this, type); }; /** * Get the siblings array, or `null` if it doesn't exist. * * ```js * var foo = new Node({type: 'foo'}); * var bar = new Node({type: 'bar'}); * var baz = new Node({type: 'baz'}); * foo.pushNode(bar); * foo.pushNode(baz); * * console.log(bar.siblings.length) // 2 * console.log(baz.siblings.length) // 2 * ``` * @name .siblings * @return {Array} * @api public */ Object.defineProperty(Node.prototype, 'siblings', { get: function() { return this.parent ? this.parent.nodes : null; } }); /** * Get the previous node from the siblings array or `null`. * * ```js * var foo = new Node({type: 'foo'}); * var bar = new Node({type: 'bar'}); * var baz = new Node({type: 'baz'}); * foo.pushNode(bar); * foo.pushNode(baz); * * console.log(baz.prev.type) // 'bar' * ``` * @name .prev * @return {Object} * @api public */ Object.defineProperty(Node.prototype, 'prev', { get: function() { return this.parent && this.siblings ? this.siblings[this.index - 1] || this.parent.prev : null; } }); /** * Get the siblings array, or `null` if it doesn't exist. * * ```js * var foo = new Node({type: 'foo'}); * var bar = new Node({type: 'bar'}); * var baz = new Node({type: 'baz'}); * foo.pushNode(bar); * foo.pushNode(baz); * * console.log(bar.siblings.length) // 2 * console.log(baz.siblings.length) // 2 * ``` * @name .next * @return {Object} * @api public */ Object.defineProperty(Node.prototype, 'next', { get: function() { return this.parent && this.siblings ? this.siblings[this.index + 1] || this.parent.next : null; } }); /** * Get the node's current index from `node.parent.nodes`. * This should always be correct, even when the parent adds nodes. * * ```js * 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.pushNode(bar); * foo.pushNode(baz); * foo.unshiftNode(qux); * * console.log(bar.index) // 1 * console.log(baz.index) // 2 * console.log(qux.index) // 0 * ``` * @name .index * @return {Number} * @api public */ Object.defineProperty(Node.prototype, 'index', { get: function() { return this.siblings ? this.siblings.indexOf(this) : -1; } }); /** * Get the first node from `node.nodes`. * * ```js * 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.pushNode(bar); * foo.pushNode(baz); * foo.pushNode(qux); * * console.log(foo.first.type) // 'bar' * ``` * @name .first * @return {Object} The first node, or undefiend * @api public */ Object.defineProperty(Node.prototype, 'first', { get: function() { return utils.su.arrayify(this.nodes)[0]; } }); /** * Get the last node from `node.nodes`. * * ```js * 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.pushNode(bar); * foo.pushNode(baz); * foo.pushNode(qux); * * console.log(foo.last.type) // 'qux' * ``` * @name .last * @return {Object} The last node, or undefiend * @api public */ Object.defineProperty(Node.prototype, 'last', { get: function() { return utils.su.last(utils.su.arrayify(this.nodes)); } }); snapdragon-node-1.0.6/package.json000066400000000000000000000027551305112224700170710ustar00rootroot00000000000000{ "name": "snapdragon-node", "description": "Snapdragon utility for creating a new AST node in custom code, such as plugins.", "version": "1.0.6", "homepage": "https://github.com/jonschlinkert/snapdragon-node", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "repository": "jonschlinkert/snapdragon-node", "bugs": { "url": "https://github.com/jonschlinkert/snapdragon-node/issues" }, "license": "MIT", "files": [ "index.js", "utils.js" ], "main": "index.js", "engines": { "node": ">=4.7" }, "scripts": { "test": "mocha" }, "dependencies": { "define-property": "^0.2.5", "isobject": "^3.0.0", "lazy-cache": "^2.0.2", "snapdragon-util": "^1.0.3" }, "devDependencies": { "gulp-format-md": "^0.1.11", "mocha": "^3.2.0", "snapdragon": "^0.9.1", "snapdragon-capture-set": "^1.0.1" }, "keywords": [ "ast", "compile", "compiler", "convert", "node", "parse", "parser", "plugin", "render", "snapdragon", "snapdragonplugin", "token", "transform" ], "verb": { "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "related": { "list": [ "snapdragon", "snapdragon-capture", "snapdragon-util", "snapdragon-cheerio", "breakdance" ] }, "reflinks": [ "verb", "verb-generate-readme" ], "lint": { "reflinks": true } } } snapdragon-node-1.0.6/test/000077500000000000000000000000001305112224700155515ustar00rootroot00000000000000snapdragon-node-1.0.6/test/test.js000066400000000000000000000166531305112224700171010ustar00rootroot00000000000000'use strict'; require('mocha'); var assert = require('assert'); var captureSet = require('snapdragon-capture-set'); var Parser = require('snapdragon/lib/parser'); var Node = require('..'); var parser; var ast; describe('snapdragon-node', function() { beforeEach(function() { parser = new Parser() parser.use(captureSet()) .captureSet('brace', /^\{/, /^\}/) .set('text', function() { var pos = this.position(); var m = this.match(/^[^{}]/); if (!m) return; return pos({ type: 'text', val: m[0] }); }); // ensure the ast is an instance of Node ast = new Node(parser.parse('{a{b}c}')); }); describe('node', function() { it('should export a function', function() { assert.equal(typeof Node, 'function'); }); it('should create a new Node with the given object', function() { var node = new Node({val: '*', type: 'star'}); assert.equal(node.val, '*'); assert.equal(node.type, 'star'); }); it('should create a new Node with the given position', function() { var pos = parser.position(); var node = pos(new Node()); assert(node.position); assert(node.position.start); assert(node.position.start.line); assert(node.position.start.column); assert(node.position.end); assert(node.position.end.line); assert(node.position.end.column); }); it('should create a new Node with the given position and val', function() { var pos = parser.position(); var node = pos(new Node('*')); assert.equal(node.val, '*'); assert(node.position); assert(node.position.start); assert(node.position.start.line); assert(node.position.start.column); assert(node.position.end); assert(node.position.end.line); assert(node.position.end.column); }); it('should create a new Node with the given position, type, and val', function() { var pos = parser.position(); var node = pos(new Node('*', 'star')); assert.equal(node.val, '*'); assert.equal(node.type, 'star'); assert(node.position); assert(node.position.start); assert(node.position.start.line); assert(node.position.start.column); assert(node.position.end); assert(node.position.end.line); assert(node.position.end.column); }); it('should create a new Node with the given position and object', function() { var pos = parser.position(); var node = pos(new Node({val: '*', type: 'star'})); assert.equal(node.val, '*'); assert.equal(node.type, 'star'); assert(node.position); assert(node.position.start); assert(node.position.start.line); assert(node.position.start.column); assert(node.position.end); assert(node.position.end.line); assert(node.position.end.column); }); }); describe('.isType', function() { it('should return true if the node is the given type', function() { assert(ast.isType('root')); assert(ast.last.isType('eos')); }); }); describe('.hasType', function() { it('should return true if node.nodes has the given type', function() { assert(ast.hasType('brace')); assert(!ast.hasType('foo')); }); }); describe('.first', function() { it('should get the first node from `node.nodes`', function() { assert(ast.first); assert.equal(ast.first.type, 'bos'); }); }); describe('.last', function() { it('should get the last node from `node.nodes`', function() { assert(ast.last); assert.equal(ast.last.type, 'eos'); }); }); describe('.index', function() { it('should get the index of the node from `node.parent.nodes`', function() { 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.pushNode(bar); foo.pushNode(baz); foo.unshiftNode(qux); assert.equal(bar.index, 1); assert.equal(baz.index, 2); assert.equal(qux.index, 0); }); }); describe('.siblings', function() { it('should get `node.parent.nodes`', function() { 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.pushNode(bar); foo.pushNode(baz); foo.unshiftNode(qux); assert.equal(foo.siblings, null); assert.equal(bar.siblings.length, 3); assert.equal(baz.siblings.length, 3); assert.equal(qux.siblings.length, 3); }); }); describe('.next', function() { it('should get the next node from `node.nodes`', function() { var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); assert.equal(bar.next.type, 'baz'); assert.equal(baz.next, null); }); it('should get the next node from `node.parent.nodes`', function() { var parent = new Node({type: 'parent'}); var a = new Node({type: 'a'}); var z = new Node({type: 'z'}); var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); parent.pushNode(a); parent.pushNode(foo); parent.pushNode(z); assert.equal(bar.next.type, 'baz'); assert.equal(baz.next.type, 'z'); }); }); describe('.prev', function() { it('should get the prev node from `node.nodes`', function() { var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); assert.equal(bar.prev, null); assert.equal(baz.prev.type, 'bar'); }); it('should get the prev node from `node.parent.nodes`', function() { var parent = new Node({type: 'parent'}); var a = new Node({type: 'a'}); var z = new Node({type: 'z'}); var foo = new Node({type: 'foo'}); var bar = new Node({type: 'bar'}); var baz = new Node({type: 'baz'}); foo.pushNode(bar); foo.pushNode(baz); parent.pushNode(a); parent.pushNode(foo); parent.pushNode(z); assert.equal(bar.prev.type, 'a'); assert.equal(baz.prev.type, 'bar'); }); }); describe('.getNode', function() { it('should get the node from `node.nodes`', function() { var brace = ast.getNode('brace'); assert.equal(brace.type, 'brace'); var open = brace.getNode('brace.open'); assert.equal(open.type, 'brace.open'); }); it('should get the node from `node.nodes`', function() { var bos = ast.getNode(0); assert.equal(bos.type, 'bos'); var brace = ast.getNode(1); assert.equal(brace.type, 'brace'); }); }); describe('.pushNode', function() { it('should add a node to `node.nodes`', function() { var node = new Node({type: 'foo'}); ast.pushNode(node); assert.equal(ast.last.type, 'foo'); }); it('should set the parent on the given node', function() { var node = new Node({type: 'foo'}); ast.pushNode(node); assert(ast === node.parent); }); it('should set the parent.nodes as `.siblings` on the given node', function() { var node = new Node({type: 'foo'}); ast.pushNode(node); assert.equal(node.siblings.length, 4); }); }); }); snapdragon-node-1.0.6/utils.js000066400000000000000000000005021305112224700162650ustar00rootroot00000000000000'use strict'; var utils = require('lazy-cache')(require); var fn = require; require = utils; /** * Lazily required module dependencies */ require('define-property', 'define'); require('isobject', 'isObject'); require('snapdragon-util', 'su'); require = fn; /** * Expose `utils` modules */ module.exports = utils;