Comparing version 1.0.1 to 1.1.0
@@ -12,3 +12,3 @@ 'use strict'; | ||
var esprimaOptions = { | ||
var esprimaOptionDefaults = { | ||
comment: true, | ||
@@ -21,3 +21,3 @@ range: true, | ||
var escodegenOptions = { | ||
var escodegenOptionDefaults = { | ||
comment: true, | ||
@@ -31,7 +31,8 @@ format: { | ||
function Tree(source, options) { | ||
this.tree = esprima.parse(source.toString(), esprimaOptions); | ||
function Tree(source, escodegenOptions, esprimaOptions) { | ||
this.esprimaOptionDefaults = _.merge({}, esprimaOptionDefaults, esprimaOptions); | ||
this.tree = esprima.parse(source.toString(), this.esprimaOptionDefaults); | ||
this.tree = escodegen.attachComments(this.tree, this.tree.comments, this.tree.tokens); | ||
this.body = new Body(this.tree.body); | ||
this.escodegenOptions = _.merge({}, escodegenOptions, options); | ||
this.escodegenOptions = _.merge({}, escodegenOptionDefaults, escodegenOptions); | ||
} | ||
@@ -103,4 +104,4 @@ | ||
module.exports = function (source, options) { | ||
return new Tree(source, options); | ||
module.exports = function (source, escodegenOptions, esprimaOptions) { | ||
return new Tree(source, escodegenOptions, esprimaOptions); | ||
}; |
{ | ||
"name": "ast-query", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Declarative JavaScript AST modification façade", | ||
@@ -5,0 +5,0 @@ "main": "lib/tree.js", |
@@ -9,3 +9,3 @@ AST Query | ||
If you've ever worked with AST trying to edit source code, you'll know it is a bad time. AST syntax is terse and forces you to loop a three and use conditionals structure a lot. AST Query hide this complexity behind a declarative façade. | ||
If you've ever worked with AST trying to edit source code, you'll know it is a bad time. AST syntax is terse and forces you to loop a tree and use conditional structure a lot. AST Query hide these complexities behind a declarative façade. | ||
@@ -48,5 +48,6 @@ Making the simplicity choice means AST Query won't try to cover the full AST API. Rather we strive to answer commons needs. | ||
### `var tree = program( sourceCode, options )` | ||
### `var tree = program( sourceCode, escodegenOptions, esprimaOptions )` | ||
- **sourceCode** (String) - The source code to edit. | ||
- **options** (Object) _optional_ - [escodegen](https://github.com/Constellation/escodegen) option object | ||
- **escodegenOptions** (Object) _optional_ - [escodegen](https://github.com/Constellation/escodegen) option object | ||
- **esprimaOptions** (Object) _optional_ - object[esprima](http://esprima.org/doc) option | ||
@@ -53,0 +54,0 @@ Returns an AST tree you can then query as explained below: |
@@ -30,3 +30,3 @@ var assert = require('assert'); | ||
describe('created with default options', function () { | ||
describe('created with default escodegen options', function () { | ||
it('return the generated source code', function () { | ||
@@ -38,3 +38,3 @@ var tree = program('(function () {\n\tconsole.log("foo");\n\tconsole.log("bar");\n})();'); | ||
describe('created with tab formatting option', function () { | ||
describe('created with tab formatting escodegen option', function () { | ||
it('return the generated source code', function () { | ||
@@ -52,2 +52,27 @@ var tree = program('(function () {\n console.log("foo");\n console.log("bar");\n})();', { | ||
describe('created with default esprima options', function () { | ||
it('parses the source code as a script', function () { | ||
assert.doesNotThrow(function () { | ||
program('var a = 1;'); | ||
}, Error); | ||
}); | ||
it('does not parse the source code as a module', function () { | ||
assert.throws(function() { | ||
program('var a = 1;\nexport default a;'); | ||
}, Error); | ||
}); | ||
}); | ||
describe('created with es2015 module esprima options', function () { | ||
it('does not parse the module source code when the sourceType configuration is missing', function () { | ||
assert.throws(function() { | ||
program('var a = 1;\nexport default a;'); | ||
}, Error); | ||
}); | ||
it('parses the source code as a module when the sourceType configuration is present', function () { | ||
assert.doesNotThrow(function () { | ||
program('var a = 1;\nexport default a;', {}, { sourceType: 'module'}); | ||
}, Error); | ||
}); | ||
}); | ||
}); |
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
38485
872
203