Skip to content

Commit 88545be

Browse files
committed
Fix: bugs about the parent property of nodes
1 parent 722d84d commit 88545be

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/script/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ export function parseVForExpression(code: string, locationCalculator: LocationCa
271271
right,
272272
}
273273

274+
// Modify parent.
275+
for (const l of left) {
276+
l.parent = expression
277+
}
278+
right.parent = expression
279+
274280
// Remvoe `for` `(` `let` `)` `;`.
275281
tokens.shift()
276282
tokens.shift()
@@ -331,6 +337,11 @@ export function parseVOnExpression(code: string, locationCalculator: LocationCal
331337
const tokens = ast.tokens || []
332338
const comments = ast.comments || []
333339

340+
// Modify parent.
341+
for (const b of body) {
342+
b.parent = expression
343+
}
344+
334345
// Remvoe braces.
335346
tokens.shift()
336347
tokens.pop()

src/template/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export function convertToDirective(code: string, parserOptions: any, locationCal
292292
expression: ret.expression,
293293
references: ret.references,
294294
}
295-
directive.value.parent = directive
295+
ret.expression.parent = directive.value
296296

297297
for (const variable of ret.variables) {
298298
node.parent.parent.variables.push(variable)
@@ -367,6 +367,7 @@ export function processMustache(parserOptions: any, globalLocationCalculator: Lo
367367

368368
node.expression = ret.expression
369369
node.references = ret.references
370+
ret.expression.parent = node
370371

371372
replaceTokens(document, {range}, ret.tokens)
372373
insertComments(document, ret.comments)

test/ast.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
const assert = require("assert")
1313
const fs = require("fs")
1414
const path = require("path")
15+
const lodash = require("lodash")
1516
const parser = require("..")
1617
const linter = require("./fixtures/eslint").linter
1718

@@ -67,7 +68,6 @@ function getAllTokens(ast) {
6768
/**
6869
* Create simple tree.
6970
* @param {string} source The source code.
70-
* @param {ASTNode} ast The root node.
7171
* @returns {object} Simple tree.
7272
*/
7373
function getTree(source) {
@@ -106,6 +106,41 @@ function getTree(source) {
106106
return root.children
107107
}
108108

109+
/**
110+
* Validate the parent property of every node.
111+
* @param {string} source The source code.
112+
* @returns {void}
113+
*/
114+
function validateParent(source) {
115+
const stack = []
116+
117+
linter.reset()
118+
linter.defineRule("validateparent", (ruleContext) => {
119+
ruleContext.parserServices.registerTemplateBodyVisitor(ruleContext, {
120+
"*"(node) {
121+
if (stack.length >= 1) {
122+
assert(node.parent === lodash.last(stack))
123+
}
124+
stack.push(node)
125+
},
126+
"*:exit"() {
127+
stack.pop()
128+
},
129+
})
130+
return {}
131+
})
132+
linter.verify(
133+
source,
134+
{
135+
parser: PARSER,
136+
parserOptions: {ecmaVersion: 2017},
137+
rules: {validateparent: "error"},
138+
},
139+
undefined,
140+
true
141+
)
142+
}
143+
109144
//------------------------------------------------------------------------------
110145
// Main
111146
//------------------------------------------------------------------------------
@@ -174,6 +209,10 @@ describe("Template AST", () => {
174209

175210
assert.strictEqual(actualText, expectedText)
176211
})
212+
213+
it("should have correct parent properties.", () => {
214+
validateParent(source)
215+
})
177216
})
178217
}
179218
})

0 commit comments

Comments
 (0)