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

Commit 37329e1

Browse files
yannickcrmarkelog
authored andcommitted
Iterator: extend estraverse rules to support JSX
Closes gh-830
1 parent 65f27dd commit 37329e1

3 files changed

Lines changed: 195 additions & 0 deletions

File tree

lib/tree-iterator.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ function iterate(node, cb) {
3030
if (cb(node, parent, parentCollection) === false) {
3131
return estraverse.VisitorOption.Skip;
3232
}
33+
},
34+
keys: {
35+
XJSIdentifier: [],
36+
XJSNamespacedName: ['namespace', 'name'],
37+
XJSMemberExpression: ['object', 'property'],
38+
XJSEmptyExpression: [],
39+
XJSExpressionContainer: ['expression'],
40+
XJSElement: ['openingElement', 'closingElement', 'children'],
41+
XJSClosingElement: ['name'],
42+
XJSOpeningElement: ['name', 'attributes'],
43+
XJSAttribute: ['name', 'value'],
44+
XJSSpreadAttribute: ['argument'],
45+
XJSText: null
3346
}
3447
});
3548
}

test/data/tree-iterator/jsx-ast.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* AST for:
3+
* <div id="wow" disabled>
4+
* <Component:Test {...x}>{this.props.children}</Component:Test><Component.Test>{}</Component.Test>
5+
* </div>;
6+
*/
7+
module.exports = {
8+
"type":"Program",
9+
"body":[
10+
{
11+
"type":"ExpressionStatement",
12+
"expression":{
13+
"type":"XJSElement",
14+
"openingElement":{
15+
"type":"XJSOpeningElement",
16+
"name":{
17+
"type":"XJSIdentifier",
18+
"name":"div"
19+
},
20+
"selfClosing":false,
21+
"attributes":[
22+
{
23+
"type":"XJSAttribute",
24+
"name":{
25+
"type":"XJSIdentifier",
26+
"name":"id"
27+
},
28+
"value":{
29+
"type":"Literal",
30+
"value":"wow",
31+
"raw":"\"wow\""
32+
}
33+
},
34+
{
35+
"type":"XJSAttribute",
36+
"name":{
37+
"type":"XJSIdentifier",
38+
"name":"disabled"
39+
},
40+
"value":null
41+
}
42+
]
43+
},
44+
"closingElement":{
45+
"type":"XJSClosingElement",
46+
"name":{
47+
"type":"XJSIdentifier",
48+
"name":"div"
49+
}
50+
},
51+
"children":[
52+
{
53+
"type":"Literal",
54+
"value":"\n",
55+
"raw":"\n"
56+
},
57+
{
58+
"type":"XJSElement",
59+
"openingElement":{
60+
"type":"XJSOpeningElement",
61+
"name":{
62+
"type":"XJSNamespacedName",
63+
"namespace":{
64+
"type":"XJSIdentifier",
65+
"name":"Component"
66+
},
67+
"name":{
68+
"type":"XJSIdentifier",
69+
"name":"Test"
70+
}
71+
},
72+
"selfClosing":false,
73+
"attributes":[
74+
{
75+
"type":"XJSSpreadAttribute",
76+
"argument":{
77+
"type":"Identifier",
78+
"name":"x"
79+
}
80+
}
81+
]
82+
},
83+
"closingElement":{
84+
"type":"XJSClosingElement",
85+
"name":{
86+
"type":"XJSNamespacedName",
87+
"namespace":{
88+
"type":"XJSIdentifier",
89+
"name":"Component"
90+
},
91+
"name":{
92+
"type":"XJSIdentifier",
93+
"name":"Test"
94+
}
95+
}
96+
},
97+
"children":[
98+
{
99+
"type":"XJSExpressionContainer",
100+
"expression":{
101+
"type":"MemberExpression",
102+
"computed":false,
103+
"object":{
104+
"type":"MemberExpression",
105+
"computed":false,
106+
"object":{
107+
"type":"ThisExpression"
108+
},
109+
"property":{
110+
"type":"Identifier",
111+
"name":"props"
112+
}
113+
},
114+
"property":{
115+
"type":"Identifier",
116+
"name":"children"
117+
}
118+
}
119+
}
120+
]
121+
},
122+
{
123+
"type":"XJSElement",
124+
"openingElement":{
125+
"type":"XJSOpeningElement",
126+
"name":{
127+
"type":"XJSMemberExpression",
128+
"object":{
129+
"type":"XJSIdentifier",
130+
"name":"Component"
131+
},
132+
"property":{
133+
"type":"XJSIdentifier",
134+
"name":"Test"
135+
}
136+
},
137+
"selfClosing":false,
138+
"attributes":[
139+
140+
]
141+
},
142+
"closingElement":{
143+
"type":"XJSClosingElement",
144+
"name":{
145+
"type":"XJSMemberExpression",
146+
"object":{
147+
"type":"XJSIdentifier",
148+
"name":"Component"
149+
},
150+
"property":{
151+
"type":"XJSIdentifier",
152+
"name":"Test"
153+
}
154+
}
155+
},
156+
"children":[
157+
{
158+
"type":"XJSExpressionContainer",
159+
"expression":{
160+
"type":"XJSEmptyExpression"
161+
}
162+
}
163+
]
164+
},
165+
{
166+
"type":"Literal",
167+
"value":"\n",
168+
"raw":"\n"
169+
}
170+
]
171+
}
172+
}
173+
]
174+
}

test/tree-iterator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ describe('modules/tree-iterator', function() {
2121
assert.equal(spy.callCount, 0);
2222
});
2323

24+
it('should not fail for XJS nodes', function() {
25+
var spy = sinon.spy();
26+
assert.doesNotThrow(function() {
27+
iterate(require('./data/tree-iterator/jsx-ast'), spy);
28+
});
29+
assert.equal(spy.callCount, 42);
30+
});
31+
2432
it('should exit thread on false result', function() {
2533
var spy = sinon.spy(function() {
2634
return false;

0 commit comments

Comments
 (0)