Skip to content

Commit

Permalink
fix: v-for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 22, 2021
1 parent a81cda4 commit cc9ea92
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export function parseVueSFC(code: string, id?: string): ParseResult {
if (!value)
return
if (key.startsWith('v-') || key.startsWith('@') || key.startsWith(':')) {
if (key === 'v-if')
expressions.add(`for (let ${value}) {}`)
if (key === 'v-for')
// we strip out delectations for v-for before `in` or `of`
expressions.add(`(${value.replace(/^.*?\w(?:in|of)\w/, '')})`)
else
expressions.add(`(${value})`)
}
Expand Down
3 changes: 2 additions & 1 deletion test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports[`transform fixture VFor.vue 1`] = `
"<template>
<div>
<div v-for=\\"(item, index) in items\\" :key=\\"item\\">
{{ index }}: {{ item }}
{{ item }}
</div>
</div>
</template>
Expand All @@ -108,6 +108,7 @@ __sfc_main.setup = (__props, __ctx) => {
name: 'Item 4',
value: 4
}];
const index = 0;
return {
items
};
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/VFor.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
<div v-for="(item, index) in items" :key="item">
{{ index }}: {{ item }}
{{ item }}
</div>
</div>
</template>
Expand All @@ -13,4 +13,6 @@ const items = [
{ name: 'Item 3', value: 3 },
{ name: 'Item 4', value: 4 },
]
const index = 0
</script>

0 comments on commit cc9ea92

Please sign in to comment.