Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a new generic editor helper (used on function & namespace)
- Loading branch information
Showing
4 changed files
with
54 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/*! | ||
* Copyright (C) 2016 Glayzzle (BSD3 License) | ||
* @authors https://github.com/glayzzle/php-writer/graphs/contributors | ||
* @url http://glayzzle.com/php-writer | ||
*/ | ||
var parser = require('php-parser'); | ||
|
||
/** | ||
* Adds some generic manipulation helpers over the body | ||
* @param {Function} obj | ||
* @param {Integer} bodyIndex | ||
* @return void | ||
*/ | ||
module.exports = function(obj, bodyIndex) { | ||
|
||
/** | ||
* Prepends some code at the ast body | ||
*/ | ||
obj.prototype.prependCode = function(code) { | ||
var ast = parser.parseEval(code); | ||
this.ast[bodyIndex] = ast[1].concat(this.ast[bodyIndex]); | ||
return this; | ||
}; | ||
|
||
/** | ||
* Appends some code at the end of the namespace body | ||
*/ | ||
obj.prototype.appendCode = function(code) { | ||
var ast = parser.parseEval(code); | ||
this.ast[bodyIndex] = this.ast[bodyIndex].concat(ast[1]); | ||
return this; | ||
}; | ||
|
||
/** | ||
* Replace the current namespace body | ||
*/ | ||
obj.prototype.setCode = function(code) { | ||
var ast = parser.parseEval(code); | ||
this.ast[bodyIndex] = ast[1]; | ||
return this; | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,7 @@ describe('Function', function() { | |
fn.ast[2][1][2].should.be.deepEqual(['constant', 'true']); | ||
}); | ||
|
||
|
||
|
||
|
||
}); |