Skip to content

Commit bf8280f

Browse files
committed
feat: support v-if
1 parent b2425e1 commit bf8280f

File tree

6 files changed

+378
-119
lines changed

6 files changed

+378
-119
lines changed

eslint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import antfu from '@antfu/eslint-config'
22

33
export default antfu({
4+
ignores: ['playground'],
45
rules: {
6+
'curly': 'off',
57
'style/object-property-newline': 'off',
68
'style/jsx-max-props-per-line': 'off',
79
'style/jsx-curly-newline': 'off',

playground/App.vue

Lines changed: 119 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,61 @@ export default defineComponent({
66
setup() {
77
const count = ref(1)
88
9+
const Comp = () => count.value
10+
? count.value && <div>2</div>
11+
: (<div>1</div>)
12+
13+
const Comp1 = () => {
14+
return (count.value
15+
? (Array.from({ length: count.value }).map((_, index) => (
16+
index && (
17+
<span>
18+
{index}
19+
</span>
20+
)
21+
)))
22+
: null
23+
)
24+
}
25+
26+
const Comp2 = () => {
27+
return (count.value && (
28+
count.value > 9
29+
? count.value > 2 && <div>null</div>
30+
: (
31+
<div>
32+
12333
33+
</div>
34+
)
35+
))
36+
}
37+
38+
const Comp3 = () => {
39+
return count.value > 1
40+
? count.value
41+
? (
42+
<div>
43+
1
44+
</div>
45+
)
46+
: <div>null</div>
47+
: (
48+
<div>
49+
2
50+
</div>
51+
)
52+
}
53+
54+
const Comp4 = () => {
55+
return count.value > 1
56+
? (
57+
<span>
58+
{count.value}
59+
</span>
60+
)
61+
: <div>null</div>
62+
}
63+
964
return (
1065
<>
1166
<form onSubmit_prevent>
@@ -14,6 +69,18 @@ export default defineComponent({
1469
onInput={count.value = $event.target.value}
1570
/>
1671
72+
<div>
73+
v-show:
74+
<span v-show={count.value}>
75+
{count.value > 1 ? 2 : 3}
76+
</span>
77+
</div>
78+
79+
<div>
80+
v-text:
81+
<span v-text={count.value} />
82+
</div>
83+
1784
<div>
1885
v-text:
1986
<span v-text={count.value} />
@@ -47,14 +114,59 @@ export default defineComponent({
47114
48115
<div>
49116
v-for:
117+
118+
{
119+
count.value
120+
&& count.value > 1
121+
&& (count.value === 5
122+
? (
123+
<span>
124+
1
125+
</span>
126+
)
127+
: count.value === 6
128+
? (count.value && Array.from({ length: 10 }).map(() => (
129+
<span>
130+
2
131+
</span>
132+
)))
133+
: (
134+
<div>
135+
3
136+
</div>
137+
)
138+
)
139+
}
140+
141+
{
142+
Array.from({ length: 10 }).map(() => (
143+
<span>
144+
1
145+
</span>
146+
))
147+
}
148+
</div>
149+
150+
<div>
151+
v-if:
152+
{
153+
count.value > 1
154+
? <span>lg 1</span>
155+
: count.value > 2
156+
? <span>lg 2</span>
157+
: <span>lt 1</span>
158+
}
159+
{count.value && <span> 123 </span>}
160+
{count.value && Array.from({ length: 10 }).map(() => (
161+
<span>
162+
1
163+
</span>
164+
))}
50165
{
51-
Array.from({ length: 10 }).map(() => {
52-
return (
53-
<span>
54-
1
55-
</span>
56-
)
57-
})
166+
count.value && count.value > 1 && count.value > 2
167+
&& (count.value > 9
168+
? (<div>123</div>)
169+
: null)
58170
}
59171
</div>
60172
</form>

src/core/common.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import type { CallExpression, ConditionalExpression, LogicalExpression, Node } from '@babel/types'
2+
import type { MagicString } from '@vue-macros/common'
3+
4+
export function addAttribute(
5+
node: Node,
6+
str: string,
7+
s: MagicString,
8+
) {
9+
const end = node.type === 'JSXElement'
10+
? node.openingElement.name.end!
11+
: node.type === 'JSXFragment'
12+
? node.openingFragment.end! - 1
13+
: null
14+
if (end)
15+
s.appendRight(end, str)
16+
}
17+
18+
export function overwrite(
19+
start: number | undefined,
20+
end: number | undefined,
21+
content: string,
22+
s: MagicString,
23+
) {
24+
if (start === end) {
25+
s.appendRight(start!, content)
26+
}
27+
else {
28+
s.overwrite(start!, end!, content)
29+
}
30+
}
31+
32+
export function getReturnExpression(
33+
node: Node,
34+
) {
35+
if (
36+
node.type === 'FunctionExpression'
37+
|| node.type === 'ArrowFunctionExpression'
38+
) {
39+
if (node.body.type !== 'BlockStatement') {
40+
return node.body
41+
}
42+
else {
43+
for (const statement of node.body.body) {
44+
if (statement.type === 'ReturnStatement' && statement.argument)
45+
return statement.argument
46+
}
47+
}
48+
}
49+
}
50+
51+
export function isJSXElement(node?: Node): boolean {
52+
return !!node && (node.type === 'JSXElement'
53+
|| node.type === 'JSXFragment'
54+
|| isConditionalExpression(node)
55+
|| isLogicalExpression(node)
56+
|| isMapCallExpression(node)
57+
)
58+
}
59+
60+
export function isMapCallExpression(node: Node): node is CallExpression {
61+
return node.type === 'CallExpression'
62+
&& node.callee.type === 'MemberExpression'
63+
&& node.callee.property.type === 'Identifier'
64+
&& node.callee.property.name === 'map'
65+
&& isJSXElement(
66+
getReturnExpression(node.arguments[0]),
67+
)
68+
}
69+
70+
export function isConditionalExpression(node: Node): node is ConditionalExpression {
71+
return node.type === 'ConditionalExpression' && (
72+
isJSXElement(node.consequent)
73+
|| isJSXElement(node.alternate)
74+
)
75+
}
76+
77+
export function isLogicalExpression(node: Node): node is LogicalExpression {
78+
return node.type === 'LogicalExpression'
79+
&& isJSXElement(node.right)
80+
}

0 commit comments

Comments
 (0)