Skip to content

Commit

Permalink
add a new generic editor helper (used on function & namespace)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Dec 19, 2016
1 parent da653d9 commit 6833e8b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
9 changes: 6 additions & 3 deletions src/function.js
Expand Up @@ -4,15 +4,17 @@
* @url http://glayzzle.com/php-writer
*/

var filter = require('./helpers/filter');
var parser = require('php-parser');
var filter = require('./helpers/filter');
var editor = require('./helpers/editor');

/**
* @constructor Function
*/
var fn = function Function(ast) {
this.ast = ast;
};
editor(fn, 6);

/**
* Changing the function name
Expand All @@ -23,17 +25,18 @@ fn.prototype.setName = function(name) {
};

/**
* Changing the function name
* Changing the function arguments
*/
fn.prototype.setArgs = function(args) {
var ast = parser.parseEval('function a('+args+') {}');
this.ast[2] = ast[1][0][2];
return this;
};


/**
* Locate the node in the specified ast
* AST Offsets :
* name, params, isRef, use, returnType, body
*/
fn.locate = function(ast, name) {
return filter(ast, 'function', function(node) {
Expand Down
43 changes: 43 additions & 0 deletions src/helpers/editor.js
@@ -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;
};

};
31 changes: 3 additions & 28 deletions src/namespace.js
Expand Up @@ -4,15 +4,17 @@
* @url http://glayzzle.com/php-writer
*/

var parser = require('php-parser');
var Class = require('./class');
var fn = require('./function');
var filter = require('./helpers/filter');
var editor = require('./helpers/editor');

var Namespace = function Namespace(ast) {
this.ast = ast;
};

editor(Namespace, 2);

/**
* Change the current namespace name
* @param {String}
Expand Down Expand Up @@ -42,33 +44,6 @@ Namespace.prototype.findInterface = function(name) {

};

/**
* Prepends some code at the ast body
*/
Namespace.prototype.prependCode = function(code) {
var ast = parser.parseEval(code);
this.ast[2] = ast[1].concat(this.ast[2]);
return this;
};

/**
* Appends some code at the end of the namespace body
*/
Namespace.prototype.appendCode = function(code) {
var ast = parser.parseEval(code);
this.ast[2] = this.ast[2].concat(ast[1]);
return this;
};

/**
* Replace the current namespace body
*/
Namespace.prototype.setCode = function(code) {
var ast = parser.parseEval(code);
this.ast[2] = ast[1];
return this;
};

/**
* Locate a namespace
*/
Expand Down
2 changes: 2 additions & 0 deletions test/function.js
Expand Up @@ -45,5 +45,7 @@ describe('Function', function() {
fn.ast[2][1][2].should.be.deepEqual(['constant', 'true']);
});




});

0 comments on commit 6833e8b

Please sign in to comment.