node-source-walk
Advanced tools
Comparing version 3.0.3 to 3.1.0
25
index.js
@@ -5,6 +5,15 @@ var babylon = require('babylon'); | ||
* @param {Object} options - Options to configure parser | ||
* @param {Object} options.parser - An object with a parse method that returns an AST | ||
*/ | ||
module.exports = function(options) { | ||
this.options = options || {}; | ||
this.options.plugins = this.options.plugins || [ | ||
options = options || {}; | ||
this.parser = options.parser || babylon; | ||
if (options.parser) { | ||
// We don't want to send that down to the actual parser | ||
delete options.parser; | ||
} | ||
this.options = options; | ||
this.options.plugins = options.plugins || [ | ||
'jsx', | ||
@@ -26,3 +35,4 @@ 'flow', | ||
this.options.sourceType = this.options.sourceType || 'module'; | ||
this.options.allowHashBang = options.allowHashBang || true; | ||
this.options.sourceType = options.sourceType || 'module'; | ||
@@ -39,4 +49,5 @@ // We use global state to stop the recursive traversal of the AST | ||
module.exports.prototype.parse = function(src, options) { | ||
options = options || {}; | ||
options = options || this.options; | ||
// Keep around for consumers of parse that supply their own options | ||
if (typeof options.allowHashBang === 'undefined') { | ||
@@ -46,3 +57,3 @@ options.allowHashBang = true; | ||
return babylon.parse(src, options); | ||
return this.parser.parse(src, options); | ||
}; | ||
@@ -90,5 +101,3 @@ | ||
var ast = typeof src === 'object' ? | ||
src : | ||
this.parse(src, this.options); | ||
var ast = typeof src === 'object' ? src : this.parse(src); | ||
@@ -95,0 +104,0 @@ this.traverse(ast, cb); |
{ | ||
"name": "node-source-walk", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "Execute a callback on every node of a source code's AST and stop walking when you see fit", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,2 +44,15 @@ ### node-source-walk [![npm](http://img.shields.io/npm/v/node-source-walk.svg)](https://npmjs.org/package/node-source-walk) [![npm](http://img.shields.io/npm/dm/node-source-walk.svg)](https://npmjs.org/package/node-source-walk) | ||
### Swap out the parser | ||
If you want to supply your own parser, you can do: | ||
```js | ||
var walker = new Walker({ | ||
parser: mySweetParser | ||
}); | ||
``` | ||
* The custom parser must have a `.parse` method that takes in a string and returns an object/AST. | ||
* All of the other options supplied to the Walker constructor will be passed along as parser options to your chosen parser. | ||
### Public Members | ||
@@ -61,1 +74,6 @@ | ||
* Callback should expect the first argument to be an AST node, similar to `walk`'s callback. | ||
`parse(src)` | ||
* Uses the options supplied to Walker to parse the given source code string and return its AST | ||
using the configured parser (or babylon by default). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5990
90
78