-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Sevavietl/master
Added namespaces and traits functionality.
- Loading branch information
Showing
15 changed files
with
655 additions
and
170 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
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,67 @@ | ||
var parser = require('php-parser'); | ||
|
||
var traitPredicate = function (node) { | ||
return node.kind === 'traituse'; | ||
}; | ||
|
||
var findTraitsChild = function (subject) { | ||
return subject.body.find(traitPredicate); | ||
}; | ||
|
||
var findTraitsChildIndex = function (subject) { | ||
return subject.body.findIndex(traitPredicate); | ||
}; | ||
|
||
var traits = { | ||
setTraits: function (names) { | ||
if (Array.isArray(names)) { | ||
names = names.join(', '); | ||
} | ||
|
||
if (!names) { | ||
var index = findTraitsChildIndex(this.ast); | ||
|
||
if (index !== -1) { | ||
this.ast.body.splice(index, 1); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
var ast = parser | ||
.parseEval('class a { use ' + names + '; }') | ||
.children | ||
.shift() | ||
.body | ||
.shift(); | ||
|
||
this.ast.body.unshift(ast); | ||
|
||
return this; | ||
}, | ||
|
||
addTrait: function (name) { | ||
var traits = this.getTraits(); | ||
|
||
if (traits.indexOf(name) === -1) { | ||
traits.push(name); | ||
this.setTraits(traits); | ||
} | ||
|
||
return this; | ||
}, | ||
|
||
getTraits: function (name) { | ||
var traituse = findTraitsChild(this.ast); | ||
|
||
if (!traituse) { | ||
return []; | ||
} | ||
|
||
return traituse.traits.map(function (trait) { | ||
return trait.name; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = traits; |
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,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; |
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
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
Oops, something went wrong.