Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
*.DS_Store
node_modules/
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- 0.12
- 4
- 6
- 7
- 8
- 9

49 changes: 34 additions & 15 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,40 @@ var escodegenOptionDefaults = {
}
};

function Tree(source, escodegenOptions, acornOptions) {
this.acornOptionDefaults = _.extend({}, acornOptionDefaults, acornOptions);
this.comments = [];
this.tokens = [];
this.acornOptionDefaults.onComment = this.comments;
this.acornOptionDefaults.onToken = this.tokens;
this.tree = acorn.parse(source.toString(), this.acornOptionDefaults);
this.tree = escodegen.attachComments(this.tree, this.comments, this.tokens);
this.body = new Body(this.tree.body, this.acornOptionDefaults);
this.escodegenOptions = _.extend({}, escodegenOptionDefaults, escodegenOptions);
}
var Tree = module.exports = function Tree() {
this.acornOptions = undefined;
this.comments = undefined;
this.tree = undefined;
this.body = undefined;
this.escodegenOptions = undefined;
};

Tree.fromSource = function(source, escodegenOptions, acornOptions) {
var instance = new Tree();
instance.acornOptions = _.extend({}, acornOptionDefaults, acornOptions);
instance.comments = [];
instance.tokens = [];
instance.acornOptions.onComment = instance.comments;
instance.acornOptions.onToken = instance.tokens;
instance.tree = acorn.parse(source.toString(), instance.acornOptions);
instance.tree = escodegen.attachComments(instance.tree, instance.comments, instance.tokens);
instance.body = new Body(instance.tree.body, instance.acornOptions);
instance.escodegenOptions = _.extend({}, escodegenOptionDefaults, escodegenOptions);
return instance;
};

Tree.fromTree = function(tree, escodegenOptions, acornOptions) {
var instance = new Tree();
instance.acornOptions = _.extend({}, acornOptionDefaults, acornOptions);
instance.comments = [];
instance.tokens = [];
instance.acornOptions.onComment = instance.comments;
instance.acornOptions.onToken = instance.tokens;
instance.tree = tree;
instance.body = new Body(instance.tree.body, instance.acornOptions);
instance.escodegenOptions = _.extend({}, escodegenOptionDefaults, escodegenOptions);
return instance;
};

/**
* Return the regenerated code string
Expand Down Expand Up @@ -110,7 +133,3 @@ Tree.prototype.verbatim = function (body) {
Body.verbatims[token] = body;
return '\'' + token + '\';';
};

module.exports = function (source, escodegenOptions, acornOptions) {
return new Tree(source, escodegenOptions, acornOptions);
};
Loading