Skip to content
Permalink
Browse files

Usegroup addition incapsulated and added to writer, not only namespace.

  • Loading branch information...
Sevavietl committed Dec 15, 2017
1 parent ff096b8 commit 36d98342bd90334148c48be226e050a1b956e304
Showing with 44 additions and 12 deletions.
  1. +18 −0 src/helpers/usegroup.js
  2. +8 −0 src/index.js
  3. +2 −12 src/namespace.js
  4. +16 −0 test/index.js
@@ -0,0 +1,18 @@
var parser = require('php-parser');

var usegroup = {
add: function (name) {
var usegroup = parser.parseEval('use a;').children.shift();
usegroup.items[0].name = name;

var insertBefore = this.ast.children.findIndex(function (node) {
return node.kind !== 'usegroup';
});

this.ast.children.splice(insertBefore, 0, usegroup);

return this;
}
}

module.exports = usegroup;
@@ -14,6 +14,7 @@ var Class = require('./class');
var fn = require('./function');
var Interface = require('./interface');
var Trait = require('./trait');
var usegroup = require('./helpers/usegroup');

// Parser default options
var defaultOptions = {
@@ -73,6 +74,13 @@ Writer.prototype.addNamespace = function(name) {
return Namespace.locate(this.ast.children, name);
}

/**
* Add usegroup
* @param {String}
* @return {Writer}
*/
Writer.prototype.addUsegroup = usegroup.add;

/**
* Finds a namespace
* @param {String} name
@@ -13,6 +13,7 @@ var Trait = require('./trait');
var filter = require('./helpers/filter');
var editor = require('./helpers/editor');
var parser = require('php-parser');
var usegroup = require('./helpers/usegroup');

/**
* A namespace entry
@@ -38,18 +39,7 @@ Namespace.prototype.setName = function(name) {
* @param {String}
* @return {namespace}
*/
Namespace.prototype.addUsegroup = function(name) {
var usegroup = parser.parseEval('use a;').children.shift();
usegroup.items[0].name = name;

var insertBefore = this.ast.children.findIndex(function (node) {
return node.kind !== 'usegroup';
});

this.ast.children.splice(insertBefore, 0, usegroup);

return this;
}
Namespace.prototype.addUsegroup = usegroup.add;

/**
* Lookup for a class
@@ -44,6 +44,22 @@ describe('API', function () {
klass.should.be.instanceOf(Class);
});
});

describe('#addUsegroup', function () {
it('should add usergroup to file', function () {
var namespace = test.findNamespace();
var childrenCount = namespace.ast.children.length;

namespace.addUsegroup('bar');
namespace.ast.children.should.have.length(childrenCount + 1);

namespace.addUsegroup('baz');
namespace.ast.children.should.have.length(childrenCount + 2);

namespace.ast.children[0].items[0].name.should.equal('bar');
namespace.ast.children[1].items[0].name.should.equal('baz');
});
});

describe('#findNamespace', function() {
it('finds namespace by name', function () {

0 comments on commit 36d9834

Please sign in to comment.
You can’t perform that action at this time.