Fix hangs on invalid v-for (#12113)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-01-19 20:53:17 +08:00
1 parent 53da86c34d
commit 27e7376229
5 files changed
+111 -3

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
#### Fix hangs on invalid `v-for` (#12113 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
<template>
<div>
<div v-for=" a in ">aaaaa</div>
</div>
</template>
// Prettier stable
// Hangs
// Prettier main
<template>
<div>
<div v-for=" a in ">aaaaa</div>
</div>
</template>;
```
+16 -3
View File
@@ -40,8 +40,13 @@ function parseVueFor(value) {
if (!inMatch) {
return;
}
const res = {};
res.for = inMatch[3].trim();
if (!res.for) {
return;
}
const alias = inMatch[1].trim().replace(stripParensRE, "");
const iteratorMatch = alias.match(forIteratorRE);
if (iteratorMatch) {
@@ -54,10 +59,18 @@ function parseVueFor(value) {
res.alias = alias;
}
const left = [res.alias, res.iterator1, res.iterator2];
if (
left.some(
(part, index) =>
!part && (index === 0 || left.slice(index + 1).some(Boolean))
)
) {
return;
}
return {
left: `${[res.alias, res.iterator1, res.iterator2]
.filter(Boolean)
.join(",")}`,
left: left.filter(Boolean).join(","),
operator: inMatch[2],
right: res.for,
};
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`v-for.vue format 1`] = `
====================================options=====================================
parsers: ["vue"]
printWidth: 80
| printWidth
=====================================input======================================
<template>
<div>
<div
v-for=" _ in "
v-for=" in _ "
v-for=" in "
v-for=" _, in a "
v-for=" ,_ in a "
v-for=" a, b, in a "
v-for=" a, , c in a "
v-for=" , b, c in a "
v-for=" a, b, in a "
v-for=" , b, c in a "
v-for=" a, , c in a "
v-for=" (,a,b) of 'abcd' "
></div>
</div>
</template>
=====================================output=====================================
<template>
<div>
<div
v-for=" _ in "
v-for=" in _ "
v-for=" in "
v-for="_ in a"
v-for=" ,_ in a "
v-for="(a, b) in a"
v-for=" a, , c in a "
v-for=" , b, c in a "
v-for="(a, b) in a"
v-for=" , b, c in a "
v-for=" a, , c in a "
v-for=" (,a,b) of 'abcd' "
></div>
</div>
</template>
================================================================================
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, ["vue"]);
+21
View File
@@ -0,0 +1,21 @@
<template>
<div>
<div
v-for=" _ in "
v-for=" in _ "
v-for=" in "
v-for=" _, in a "
v-for=" ,_ in a "
v-for=" a, b, in a "
v-for=" a, , c in a "
v-for=" , b, c in a "
v-for=" a, b, in a "
v-for=" , b, c in a "
v-for=" a, , c in a "
v-for=" (,a,b) of 'abcd' "
></div>
</div>
</template>