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

Extend estraverse rules to support JSX #830

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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