Skip to content

Commit b9ca202

Browse files
authoredMay 30, 2024··
fix(compiler-core): v-for expression missing source with spaces should emit error (#5821)
close #5819
1 parent a3e8aaf commit b9ca202

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
 

‎packages/compiler-core/__tests__/transforms/vFor.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ describe('compiler: v-for', () => {
202202
expect(forNode.valueAlias).toBeUndefined()
203203
expect((forNode.source as SimpleExpressionNode).content).toBe('items')
204204
})
205+
206+
test('source containing string expression with spaces', () => {
207+
const { node: forNode } = parseWithForTransform(
208+
`<span v-for="item in state ['my items']" />`,
209+
)
210+
expect(forNode.keyAlias).toBeUndefined()
211+
expect(forNode.objectIndexAlias).toBeUndefined()
212+
expect((forNode.valueAlias as SimpleExpressionNode).content).toBe('item')
213+
expect((forNode.source as SimpleExpressionNode).content).toBe(
214+
"state ['my items']",
215+
)
216+
})
205217
})
206218

207219
describe('errors', () => {
@@ -253,6 +265,18 @@ describe('compiler: v-for', () => {
253265
)
254266
})
255267

268+
test('missing source and have multiple spaces with', () => {
269+
const onError = vi.fn()
270+
parseWithForTransform('<span v-for="item in " />', { onError })
271+
272+
expect(onError).toHaveBeenCalledTimes(1)
273+
expect(onError).toHaveBeenCalledWith(
274+
expect.objectContaining({
275+
code: ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION,
276+
}),
277+
)
278+
})
279+
256280
test('missing value', () => {
257281
const onError = vi.fn()
258282
parseWithForTransform('<span v-for="in items" />', { onError })

‎packages/compiler-core/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,4 @@ export function getMemoedVNodeCall(node: BlockCodegenNode | MemoExpression) {
499499
}
500500
}
501501

502-
export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
502+
export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+(\S[\s\S]*)/

0 commit comments

Comments
 (0)
Please sign in to comment.