Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Iterator: extend estraverse rules to support JSX
Browse files Browse the repository at this point in the history
Closes gh-830
  • Loading branch information
yannickcr authored and markelog committed Dec 15, 2014
1 parent 65f27dd commit 37329e1
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/tree-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ function iterate(node, cb) {
if (cb(node, parent, parentCollection) === false) {
return estraverse.VisitorOption.Skip;
}
},
keys: {
XJSIdentifier: [],
XJSNamespacedName: ['namespace', 'name'],
XJSMemberExpression: ['object', 'property'],
XJSEmptyExpression: [],
XJSExpressionContainer: ['expression'],
XJSElement: ['openingElement', 'closingElement', 'children'],
XJSClosingElement: ['name'],
XJSOpeningElement: ['name', 'attributes'],
XJSAttribute: ['name', 'value'],
XJSSpreadAttribute: ['argument'],
XJSText: null
}
});
}
Expand Down
174 changes: 174 additions & 0 deletions test/data/tree-iterator/jsx-ast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* AST for:
* <div id="wow" disabled>
* <Component:Test {...x}>{this.props.children}</Component:Test><Component.Test>{}</Component.Test>
* </div>;
*/
module.exports = {
"type":"Program",
"body":[
{
"type":"ExpressionStatement",
"expression":{
"type":"XJSElement",
"openingElement":{
"type":"XJSOpeningElement",
"name":{
"type":"XJSIdentifier",
"name":"div"
},
"selfClosing":false,
"attributes":[
{
"type":"XJSAttribute",
"name":{
"type":"XJSIdentifier",
"name":"id"
},
"value":{
"type":"Literal",
"value":"wow",
"raw":"\"wow\""
}
},
{
"type":"XJSAttribute",
"name":{
"type":"XJSIdentifier",
"name":"disabled"
},
"value":null
}
]
},
"closingElement":{
"type":"XJSClosingElement",
"name":{
"type":"XJSIdentifier",
"name":"div"
}
},
"children":[
{
"type":"Literal",
"value":"\n",
"raw":"\n"
},
{
"type":"XJSElement",
"openingElement":{
"type":"XJSOpeningElement",
"name":{
"type":"XJSNamespacedName",
"namespace":{
"type":"XJSIdentifier",
"name":"Component"
},
"name":{
"type":"XJSIdentifier",
"name":"Test"
}
},
"selfClosing":false,
"attributes":[
{
"type":"XJSSpreadAttribute",
"argument":{
"type":"Identifier",
"name":"x"
}
}
]
},
"closingElement":{
"type":"XJSClosingElement",
"name":{
"type":"XJSNamespacedName",
"namespace":{
"type":"XJSIdentifier",
"name":"Component"
},
"name":{
"type":"XJSIdentifier",
"name":"Test"
}
}
},
"children":[
{
"type":"XJSExpressionContainer",
"expression":{
"type":"MemberExpression",
"computed":false,
"object":{
"type":"MemberExpression",
"computed":false,
"object":{
"type":"ThisExpression"
},
"property":{
"type":"Identifier",
"name":"props"
}
},
"property":{
"type":"Identifier",
"name":"children"
}
}
}
]
},
{
"type":"XJSElement",
"openingElement":{
"type":"XJSOpeningElement",
"name":{
"type":"XJSMemberExpression",
"object":{
"type":"XJSIdentifier",
"name":"Component"
},
"property":{
"type":"XJSIdentifier",
"name":"Test"
}
},
"selfClosing":false,
"attributes":[

]
},
"closingElement":{
"type":"XJSClosingElement",
"name":{
"type":"XJSMemberExpression",
"object":{
"type":"XJSIdentifier",
"name":"Component"
},
"property":{
"type":"XJSIdentifier",
"name":"Test"
}
}
},
"children":[
{
"type":"XJSExpressionContainer",
"expression":{
"type":"XJSEmptyExpression"
}
}
]
},
{
"type":"Literal",
"value":"\n",
"raw":"\n"
}
]
}
}
]
}
8 changes: 8 additions & 0 deletions test/tree-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe('modules/tree-iterator', function() {
assert.equal(spy.callCount, 0);
});

it('should not fail for XJS nodes', function() {
var spy = sinon.spy();
assert.doesNotThrow(function() {
iterate(require('./data/tree-iterator/jsx-ast'), spy);
});
assert.equal(spy.callCount, 42);
});

it('should exit thread on false result', function() {
var spy = sinon.spy(function() {
return false;
Expand Down

0 comments on commit 37329e1

Please sign in to comment.