Skip to content

Commit 5b0d90c

Browse files
committed
Fix: set non-enumerable to scope variable
1 parent 0036d67 commit 5b0d90c

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

src/template/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ import {ExpressionParseResult, parseExpression, parseVForExpression, parseVOnExp
1717
function extractScopeVariables(references: Reference[], outVariables: Variable[]): void {
1818
let reference: Reference | undefined
1919
while ((reference = references.shift()) != null) {
20-
reference.id.parent = null
21-
outVariables.push({
20+
const variable: Variable = {
2221
id: reference.id,
2322
kind: "scope",
2423
references: [],
25-
})
24+
}
25+
Object.defineProperty(variable, "references", {enumerable: false})
26+
reference.id.parent = null
27+
28+
outVariables.push(variable)
2629
}
2730
}
2831

test/variables-references.js

+73-10
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,26 @@ describe("[variables] elements", () => {
149149
})
150150
})
151151

152-
describe("References and variables", () => {
152+
describe("Variables of v-for and references", () => {
153153
const code = "<template><div v-for=\"x of xs\" :key=\"x\">{{x + y}}<div>{{x}}</div></div>{{x}}</template>"
154-
let ast = null
154+
let variables = null
155+
let vForReferences = null
156+
let vBindKeyReferences = null
157+
let mustacheReferences1 = null
158+
let mustacheReferences2 = null
159+
let mustacheReferences3 = null
155160

156161
before(() => {
157-
ast = parse(code, Object.assign({filePath: "test.vue"}, PARSER_OPTIONS)).ast
162+
const ast = parse(code, Object.assign({filePath: "test.vue"}, PARSER_OPTIONS)).ast
163+
variables = ast.templateBody.children[0].variables
164+
vForReferences = ast.templateBody.children[0].startTag.attributes[0].value.references
165+
vBindKeyReferences = ast.templateBody.children[0].startTag.attributes[1].value.references
166+
mustacheReferences1 = ast.templateBody.children[0].children[0].references
167+
mustacheReferences2 = ast.templateBody.children[0].children[1].children[0].references
168+
mustacheReferences3 = ast.templateBody.children[1].references
158169
})
159170

160171
it("should have relationship each other", () => {
161-
const variables = ast.templateBody.children[0].variables
162-
const vForReferences = ast.templateBody.children[0].startTag.attributes[0].value.references
163-
const vBindKeyReferences = ast.templateBody.children[0].startTag.attributes[1].value.references
164-
const mustacheReferences1 = ast.templateBody.children[0].children[0].references
165-
const mustacheReferences2 = ast.templateBody.children[0].children[1].children[0].references
166-
const mustacheReferences3 = ast.templateBody.children[1].references
167-
168172
assert(variables.length === 1)
169173
assert(vForReferences.length === 1)
170174
assert(vBindKeyReferences.length === 1)
@@ -182,4 +186,63 @@ describe("References and variables", () => {
182186
assert(mustacheReferences2[0].variable === variables[0])
183187
assert(mustacheReferences3[0].variable === null)
184188
})
189+
190+
it("`Variable#references` should be non-enumerable", () => {
191+
for (const variable of variables) {
192+
assert(Object.getOwnPropertyDescriptor(variable, "references").enumerable === false)
193+
}
194+
})
195+
196+
it("`Reference#variable` should be non-enumerable", () => {
197+
for (const reference of [].concat(vForReferences, vBindKeyReferences, mustacheReferences1, mustacheReferences2, mustacheReferences3)) {
198+
assert(Object.getOwnPropertyDescriptor(reference, "variable").enumerable === false)
199+
}
200+
})
201+
})
202+
203+
describe("Variables of template-scope and references", () => {
204+
const code = "<template><template scope=\"x\" :key=\"x\">{{x + y}}<div>{{x}}</div></template>{{x}}</template>"
205+
let variables = null
206+
let vBindKeyReferences = null
207+
let mustacheReferences1 = null
208+
let mustacheReferences2 = null
209+
let mustacheReferences3 = null
210+
211+
before(() => {
212+
const ast = parse(code, Object.assign({filePath: "test.vue"}, PARSER_OPTIONS)).ast
213+
variables = ast.templateBody.children[0].variables
214+
vBindKeyReferences = ast.templateBody.children[0].startTag.attributes[1].value.references
215+
mustacheReferences1 = ast.templateBody.children[0].children[0].references
216+
mustacheReferences2 = ast.templateBody.children[0].children[1].children[0].references
217+
mustacheReferences3 = ast.templateBody.children[1].references
218+
})
219+
220+
it("should have relationship each other", () => {
221+
assert(variables.length === 1)
222+
assert(vBindKeyReferences.length === 1)
223+
assert(mustacheReferences1.length === 2)
224+
assert(mustacheReferences2.length === 1)
225+
assert(mustacheReferences3.length === 1)
226+
assert(variables[0].references.length === 3)
227+
assert(variables[0].references[0] === vBindKeyReferences[0])
228+
assert(variables[0].references[1] === mustacheReferences1[0])
229+
assert(variables[0].references[2] === mustacheReferences2[0])
230+
assert(vBindKeyReferences[0].variable === variables[0])
231+
assert(mustacheReferences1[0].variable === variables[0])
232+
assert(mustacheReferences1[1].variable === null)
233+
assert(mustacheReferences2[0].variable === variables[0])
234+
assert(mustacheReferences3[0].variable === null)
235+
})
236+
237+
it("`Variable#references` should be non-enumerable", () => {
238+
for (const variable of variables) {
239+
assert(Object.getOwnPropertyDescriptor(variable, "references").enumerable === false)
240+
}
241+
})
242+
243+
it("`Reference#variable` should be non-enumerable", () => {
244+
for (const reference of [].concat(vBindKeyReferences, mustacheReferences1, mustacheReferences2, mustacheReferences3)) {
245+
assert(Object.getOwnPropertyDescriptor(reference, "variable").enumerable === false)
246+
}
247+
})
185248
})

0 commit comments

Comments
 (0)