Skip to content

Commit 722d84d

Browse files
committed
Chore: add tests about registerTemplateBodyVisitor
1 parent 2f6f521 commit 722d84d

File tree

76 files changed

+6613
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+6613
-4
lines changed

src/ast/traverse.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ const KEYS: KeyMap = {
8181
WithStatement: ["object", "body"],
8282
YieldExpression: ["argument"],
8383

84+
// Legacy
85+
RestProperty: ["argument"],
86+
ExperimentalRestProperty: ["argument"],
87+
SpreadProperty: ["argument"],
88+
ExperimentalSpreadProperty: ["argument"],
89+
90+
// JSX
91+
JSXIdentifier: [],
92+
JSXMemberExpression: ["object", "property"],
93+
JSXNamespacedName: ["namespace", "name"],
94+
JSXEmptyExpression: [],
95+
JSXExpressionContainer: ["expression"],
96+
JSXSpreadChild: ["expression"],
97+
JSXOpeningElement: ["name", "attributes"],
98+
JSXClosingElement: ["name"],
99+
JSXAttribute: ["name", "value"],
100+
JSXSpreadAttribute: ["argument"],
101+
JSXText: [],
102+
JSXElement: ["openingElement", "children", "closingElement"],
103+
104+
// Vue.js
84105
VAttribute: ["key", "value"],
85106
VDirectiveKey: [],
86107
VDocumentFragment: ["children"],
@@ -90,6 +111,7 @@ const KEYS: KeyMap = {
90111
VForExpression: ["left", "right"],
91112
VIdentifier: [],
92113
VLiteral: [],
114+
VOnExpression: ["body"],
93115
VStartTag: ["attributes"],
94116
VText: [],
95117
}

test/ast.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ const assert = require("assert")
1313
const fs = require("fs")
1414
const path = require("path")
1515
const parser = require("..")
16+
const linter = require("./fixtures/eslint").linter
1617

1718
//------------------------------------------------------------------------------
1819
// Helpers
1920
//------------------------------------------------------------------------------
2021

22+
const PARSER = path.resolve(__dirname, "..")
2123
const ROOT = path.join(__dirname, "fixtures/ast")
2224
const TARGETS = fs.readdirSync(ROOT)
2325
const PARSER_OPTIONS = {
@@ -62,6 +64,48 @@ function getAllTokens(ast) {
6264
return Array.prototype.concat.apply([], tokenArrays)
6365
}
6466

67+
/**
68+
* Create simple tree.
69+
* @param {string} source The source code.
70+
* @param {ASTNode} ast The root node.
71+
* @returns {object} Simple tree.
72+
*/
73+
function getTree(source) {
74+
const stack = []
75+
const root = {children: []}
76+
let current = root
77+
78+
linter.reset()
79+
linter.defineRule("maketree", (ruleContext) => {
80+
ruleContext.parserServices.registerTemplateBodyVisitor(ruleContext, {
81+
"*"(node) {
82+
stack.push(current)
83+
current.children.push(current = {
84+
type: node.type,
85+
text: source.slice(node.range[0], node.range[1]),
86+
children: [],
87+
})
88+
},
89+
"*:exit"() {
90+
current = stack.pop()
91+
},
92+
})
93+
return {}
94+
})
95+
linter.verify(
96+
source,
97+
{
98+
parser: PARSER,
99+
parserOptions: {ecmaVersion: 2017},
100+
rules: {maketree: "error"},
101+
},
102+
undefined,
103+
true
104+
)
105+
106+
return root.children
107+
}
108+
65109
//------------------------------------------------------------------------------
66110
// Main
67111
//------------------------------------------------------------------------------
@@ -121,6 +165,15 @@ describe("Template AST", () => {
121165
)
122166
}
123167
})
168+
169+
it("should traverse AST in the correct order.", () => {
170+
const resultPath = path.join(ROOT, `${name}/tree.json`)
171+
const expectedText = fs.readFileSync(resultPath, "utf8")
172+
const tokens = getTree(source)
173+
const actualText = JSON.stringify(tokens, null, 4)
174+
175+
assert.strictEqual(actualText, expectedText)
176+
})
124177
})
125178
}
126179
})
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
[
2+
{
3+
"type": "VElement",
4+
"text": "<template>\n <div a></div>\n <div a=></div>\n <div a=b></div>\n <div a=b ></div>\n <div a=\"b\"></div>\n <div a='b'></div>\n</template>",
5+
"children": [
6+
{
7+
"type": "VStartTag",
8+
"text": "<template>",
9+
"children": []
10+
},
11+
{
12+
"type": "VText",
13+
"text": "\n ",
14+
"children": []
15+
},
16+
{
17+
"type": "VElement",
18+
"text": "<div a></div>",
19+
"children": [
20+
{
21+
"type": "VStartTag",
22+
"text": "<div a>",
23+
"children": [
24+
{
25+
"type": "VAttribute",
26+
"text": "a",
27+
"children": [
28+
{
29+
"type": "VIdentifier",
30+
"text": "a",
31+
"children": []
32+
}
33+
]
34+
}
35+
]
36+
},
37+
{
38+
"type": "VEndTag",
39+
"text": "</div>",
40+
"children": []
41+
}
42+
]
43+
},
44+
{
45+
"type": "VText",
46+
"text": "\n ",
47+
"children": []
48+
},
49+
{
50+
"type": "VElement",
51+
"text": "<div a=></div>",
52+
"children": [
53+
{
54+
"type": "VStartTag",
55+
"text": "<div a=>",
56+
"children": [
57+
{
58+
"type": "VAttribute",
59+
"text": "a=",
60+
"children": [
61+
{
62+
"type": "VIdentifier",
63+
"text": "a",
64+
"children": []
65+
}
66+
]
67+
}
68+
]
69+
},
70+
{
71+
"type": "VEndTag",
72+
"text": "</div>",
73+
"children": []
74+
}
75+
]
76+
},
77+
{
78+
"type": "VText",
79+
"text": "\n ",
80+
"children": []
81+
},
82+
{
83+
"type": "VElement",
84+
"text": "<div a=b></div>",
85+
"children": [
86+
{
87+
"type": "VStartTag",
88+
"text": "<div a=b>",
89+
"children": [
90+
{
91+
"type": "VAttribute",
92+
"text": "a=b",
93+
"children": [
94+
{
95+
"type": "VIdentifier",
96+
"text": "a",
97+
"children": []
98+
},
99+
{
100+
"type": "VLiteral",
101+
"text": "b",
102+
"children": []
103+
}
104+
]
105+
}
106+
]
107+
},
108+
{
109+
"type": "VEndTag",
110+
"text": "</div>",
111+
"children": []
112+
}
113+
]
114+
},
115+
{
116+
"type": "VText",
117+
"text": "\n ",
118+
"children": []
119+
},
120+
{
121+
"type": "VElement",
122+
"text": "<div a=b ></div>",
123+
"children": [
124+
{
125+
"type": "VStartTag",
126+
"text": "<div a=b >",
127+
"children": [
128+
{
129+
"type": "VAttribute",
130+
"text": "a=b",
131+
"children": [
132+
{
133+
"type": "VIdentifier",
134+
"text": "a",
135+
"children": []
136+
},
137+
{
138+
"type": "VLiteral",
139+
"text": "b",
140+
"children": []
141+
}
142+
]
143+
}
144+
]
145+
},
146+
{
147+
"type": "VEndTag",
148+
"text": "</div>",
149+
"children": []
150+
}
151+
]
152+
},
153+
{
154+
"type": "VText",
155+
"text": "\n ",
156+
"children": []
157+
},
158+
{
159+
"type": "VElement",
160+
"text": "<div a=\"b\"></div>",
161+
"children": [
162+
{
163+
"type": "VStartTag",
164+
"text": "<div a=\"b\">",
165+
"children": [
166+
{
167+
"type": "VAttribute",
168+
"text": "a=\"b\"",
169+
"children": [
170+
{
171+
"type": "VIdentifier",
172+
"text": "a",
173+
"children": []
174+
},
175+
{
176+
"type": "VLiteral",
177+
"text": "\"b\"",
178+
"children": []
179+
}
180+
]
181+
}
182+
]
183+
},
184+
{
185+
"type": "VEndTag",
186+
"text": "</div>",
187+
"children": []
188+
}
189+
]
190+
},
191+
{
192+
"type": "VText",
193+
"text": "\n ",
194+
"children": []
195+
},
196+
{
197+
"type": "VElement",
198+
"text": "<div a='b'></div>",
199+
"children": [
200+
{
201+
"type": "VStartTag",
202+
"text": "<div a='b'>",
203+
"children": [
204+
{
205+
"type": "VAttribute",
206+
"text": "a='b'",
207+
"children": [
208+
{
209+
"type": "VIdentifier",
210+
"text": "a",
211+
"children": []
212+
},
213+
{
214+
"type": "VLiteral",
215+
"text": "'b'",
216+
"children": []
217+
}
218+
]
219+
}
220+
]
221+
},
222+
{
223+
"type": "VEndTag",
224+
"text": "</div>",
225+
"children": []
226+
}
227+
]
228+
},
229+
{
230+
"type": "VText",
231+
"text": "\n",
232+
"children": []
233+
},
234+
{
235+
"type": "VEndTag",
236+
"text": "</template>",
237+
"children": []
238+
}
239+
]
240+
}
241+
]

0 commit comments

Comments
 (0)