Skip to content

Commit 505c5a8

Browse files
committed
fix: genExpression
1 parent f644ed4 commit 505c5a8

File tree

3 files changed

+78
-71
lines changed

3 files changed

+78
-71
lines changed

packages/compiler-vapor/__tests__/__snapshots__/compile.test.ts.snap

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export function render(_ctx) {
77
const t0 = _template(\\"<div>count is <!>.</div>\\")
88
const n0 = t0()
99
const { 0: [n3, { 1: [n2],}],} = _children(n0)
10-
const n1 = _createTextNode(count.value)
10+
const n1 = _createTextNode(_ctx.count)
1111
_insert(n1, n3, n2)
1212
_effect(() => {
13-
_setText(n1, undefined, count.value)
13+
_setText(n1, undefined, _ctx.count)
1414
})
1515
return n0
1616
}"
@@ -24,7 +24,7 @@ export function render(_ctx) {
2424
const n0 = t0()
2525
const { 0: [n1],} = _children(n0)
2626
_effect(() => {
27-
_setAttr(n1, \\"id\\", undefined, id.value)
27+
_setAttr(n1, \\"id\\", undefined, _ctx.id)
2828
})
2929
return n0
3030
}"
@@ -48,7 +48,7 @@ export function render(_ctx) {
4848
const n0 = t0()
4949
const { 0: [n1],} = _children(n0)
5050
_effect(() => {
51-
_setHtml(n1, undefined, test)
51+
_setHtml(n1, undefined, _ctx.test)
5252
})
5353
return n0
5454
}"
@@ -74,7 +74,7 @@ export function render(_ctx) {
7474
const n0 = t0()
7575
const { 0: [n1],} = _children(n0)
7676
_effect(() => {
77-
_setHtml(n1, undefined, code.value)
77+
_setHtml(n1, undefined, _ctx.code)
7878
})
7979
return n0
8080
}"
@@ -87,28 +87,28 @@ export function render(_ctx) {
8787
const t0 = _template(\\"<a></a><form></form><a></a><div></div><div></div><a></a><div></div><input><input><input><input><input><input><input><input><input><input><input><input><input><input><input>\\")
8888
const n0 = t0()
8989
const { 0: [n1], 1: [n2], 2: [n3], 3: [n4], 4: [n5], 5: [n6], 6: [n7], 7: [n8], 8: [n9], 9: [n10], 10: [n11], 11: [n12], 12: [n13], 13: [n14], 14: [n15], 15: [n16], 16: [n17], 17: [n18], 18: [n19], 19: [n20], 20: [n21], 21: [n22],} = _children(n0)
90-
_on(n1, \\"click\\", _withModifiers(handleEvent, [\\"stop\\"]))
91-
_on(n2, \\"submit\\", _withModifiers(handleEvent, [\\"prevent\\"]))
92-
_on(n3, \\"click\\", _withModifiers(handleEvent, [\\"stop\\", \\"prevent\\"]))
93-
_on(n4, \\"click\\", _withModifiers(handleEvent, [\\"self\\"]))
94-
_on(n5, \\"click\\", handleEvent, { capture: true })
95-
_on(n6, \\"click\\", handleEvent, { once: true })
96-
_on(n7, \\"scroll\\", handleEvent, { passive: true })
97-
_on(n8, \\"contextmenu\\", _withModifiers(handleEvent, [\\"right\\"]))
98-
_on(n9, \\"click\\", _withModifiers(handleEvent, [\\"left\\"]))
99-
_on(n10, \\"mouseup\\", _withModifiers(handleEvent, [\\"middle\\"]))
100-
_on(n11, \\"contextmenu\\", _withModifiers(handleEvent, [\\"right\\"]))
101-
_on(n12, \\"keyup\\", _withKeys(handleEvent, [\\"enter\\"]))
102-
_on(n13, \\"keyup\\", _withKeys(handleEvent, [\\"tab\\"]))
103-
_on(n14, \\"keyup\\", _withKeys(handleEvent, [\\"delete\\"]))
104-
_on(n15, \\"keyup\\", _withKeys(handleEvent, [\\"esc\\"]))
105-
_on(n16, \\"keyup\\", _withKeys(handleEvent, [\\"space\\"]))
106-
_on(n17, \\"keyup\\", _withKeys(handleEvent, [\\"up\\"]))
107-
_on(n18, \\"keyup\\", _withKeys(handleEvent, [\\"down\\"]))
108-
_on(n19, \\"keyup\\", _withKeys(handleEvent, [\\"left\\"]))
109-
_on(n20, \\"keyup\\", _withModifiers(submit, [\\"middle\\"]))
110-
_on(n21, \\"keyup\\", _withModifiers(submit, [\\"middle\\", \\"self\\"]))
111-
_on(n22, \\"keyup\\", _withKeys(_withModifiers(handleEvent, [\\"self\\"]), [\\"enter\\"]))
90+
_on(n1, \\"click\\", _withModifiers(_ctx.handleEvent, [\\"stop\\"]))
91+
_on(n2, \\"submit\\", _withModifiers(_ctx.handleEvent, [\\"prevent\\"]))
92+
_on(n3, \\"click\\", _withModifiers(_ctx.handleEvent, [\\"stop\\", \\"prevent\\"]))
93+
_on(n4, \\"click\\", _withModifiers(_ctx.handleEvent, [\\"self\\"]))
94+
_on(n5, \\"click\\", _ctx.handleEvent, { capture: true })
95+
_on(n6, \\"click\\", _ctx.handleEvent, { once: true })
96+
_on(n7, \\"scroll\\", _ctx.handleEvent, { passive: true })
97+
_on(n8, \\"contextmenu\\", _withModifiers(_ctx.handleEvent, [\\"right\\"]))
98+
_on(n9, \\"click\\", _withModifiers(_ctx.handleEvent, [\\"left\\"]))
99+
_on(n10, \\"mouseup\\", _withModifiers(_ctx.handleEvent, [\\"middle\\"]))
100+
_on(n11, \\"contextmenu\\", _withModifiers(_ctx.handleEvent, [\\"right\\"]))
101+
_on(n12, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"enter\\"]))
102+
_on(n13, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"tab\\"]))
103+
_on(n14, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"delete\\"]))
104+
_on(n15, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"esc\\"]))
105+
_on(n16, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"space\\"]))
106+
_on(n17, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"up\\"]))
107+
_on(n18, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"down\\"]))
108+
_on(n19, \\"keyup\\", _withKeys(_ctx.handleEvent, [\\"left\\"]))
109+
_on(n20, \\"keyup\\", _withModifiers(_ctx.submit, [\\"middle\\"]))
110+
_on(n21, \\"keyup\\", _withModifiers(_ctx.submit, [\\"middle\\", \\"self\\"]))
111+
_on(n22, \\"keyup\\", _withKeys(_withModifiers(_ctx.handleEvent, [\\"self\\"]), [\\"enter\\"]))
112112
return n0
113113
}"
114114
`;
@@ -120,7 +120,7 @@ export function render(_ctx) {
120120
const t0 = _template(\\"<div></div>\\")
121121
const n0 = t0()
122122
const { 0: [n1],} = _children(n0)
123-
_on(n1, \\"click\\", handleClick)
123+
_on(n1, \\"click\\", _ctx.handleClick)
124124
return n0
125125
}"
126126
`;
@@ -132,7 +132,7 @@ export function render(_ctx) {
132132
const t0 = _template(\\"<div></div>\\")
133133
const n0 = t0()
134134
const { 0: [n1],} = _children(n0)
135-
_setAttr(n1, \\"id\\", undefined, foo)
135+
_setAttr(n1, \\"id\\", undefined, _ctx.foo)
136136
return n0
137137
}"
138138
`;
@@ -144,9 +144,9 @@ export function render(_ctx) {
144144
const t0 = _template(\\"<div> <span></span></div>\\")
145145
const n0 = t0()
146146
const { 0: [n3, { 1: [n2],}],} = _children(n0)
147-
const n1 = _createTextNode(msg.value)
148-
_setText(n1, undefined, msg.value)
149-
_setAttr(n2, \\"class\\", undefined, clz.value)
147+
const n1 = _createTextNode(_ctx.msg)
148+
_setText(n1, undefined, _ctx.msg)
149+
_setAttr(n2, \\"class\\", undefined, _ctx.clz)
150150
_prepend(n3, n1)
151151
return n0
152152
}"
@@ -169,13 +169,13 @@ export function render(_ctx) {
169169
const t0 = _template(\\"<div></div><div><Comp></Comp></div>\\")
170170
const n0 = t0()
171171
const { 1: [n2],} = _children(n0)
172-
const n1 = _createTextNode(bar)
172+
const n1 = _createTextNode(_ctx.bar)
173173
_append(n2, n1)
174174
_effect(() => {
175-
_setText(n1, undefined, bar)
175+
_setText(n1, undefined, _ctx.bar)
176176
})
177177
_effect(() => {
178-
_setAttr(n2, \\"id\\", undefined, foo)
178+
_setAttr(n2, \\"id\\", undefined, _ctx.foo)
179179
})
180180
return n0
181181
}"
@@ -188,13 +188,13 @@ export function render(_ctx) {
188188
const t0 = _template(\\"<div :id=\\\\\\"foo\\\\\\"><Comp></Comp>{{ bar }}</div><div><Comp></Comp></div>\\")
189189
const n0 = t0()
190190
const { 1: [n2],} = _children(n0)
191-
const n1 = _createTextNode(bar.value)
191+
const n1 = _createTextNode(_ctx.bar)
192192
_append(n2, n1)
193193
_effect(() => {
194-
_setText(n1, undefined, bar.value)
194+
_setText(n1, undefined, _ctx.bar)
195195
})
196196
_effect(() => {
197-
_setAttr(n2, \\"id\\", undefined, foo.value)
197+
_setAttr(n2, \\"id\\", undefined, _ctx.foo)
198198
})
199199
return n0
200200
}"
@@ -220,7 +220,7 @@ export function render(_ctx) {
220220
const n0 = t0()
221221
const { 0: [n1],} = _children(n0)
222222
_effect(() => {
223-
_setText(n1, undefined, str.value)
223+
_setText(n1, undefined, _ctx.str)
224224
})
225225
return n0
226226
}"
@@ -253,24 +253,24 @@ export function render(_ctx) {
253253
const t0 = _template(\\"<button>foo<!>foo</button>\\")
254254
const n0 = t0()
255255
const { 0: [n4, { 1: [n5],}],} = _children(n0)
256-
const n1 = _createTextNode(count)
257-
const n2 = _createTextNode(count)
258-
const n3 = _createTextNode(count)
256+
const n1 = _createTextNode(_ctx.count)
257+
const n2 = _createTextNode(_ctx.count)
258+
const n3 = _createTextNode(_ctx.count)
259259
_prepend(n4, n1)
260260
_insert(n2, n4, n5)
261261
_append(n4, n3)
262-
_on(n4, \\"click\\", handleClick)
262+
_on(n4, \\"click\\", _ctx.handleClick)
263263
_effect(() => {
264-
_setText(n1, undefined, count)
264+
_setText(n1, undefined, _ctx.count)
265265
})
266266
_effect(() => {
267-
_setText(n2, undefined, count)
267+
_setText(n2, undefined, _ctx.count)
268268
})
269269
_effect(() => {
270-
_setText(n3, undefined, count)
270+
_setText(n3, undefined, _ctx.count)
271271
})
272272
_effect(() => {
273-
_setAttr(n4, \\"id\\", undefined, count)
273+
_setAttr(n4, \\"id\\", undefined, _ctx.count)
274274
})
275275
return n0
276276
}"

packages/compiler-vapor/__tests__/compile.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
import { type CompilerOptions, compile as _compile } from '../src'
88

99
function compile(template: string | RootNode, options: CompilerOptions = {}) {
10-
let { code } = _compile(template, options)
10+
let { code } = _compile(template, {
11+
...options,
12+
mode: 'module',
13+
prefixIdentifiers: true,
14+
})
1115
return code
1216
}
1317

packages/compiler-vapor/src/generate.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,10 @@ function genExpression(node: IRExpression, context: CodegenContext): void {
502502
// there was a parsing error
503503
ast === false
504504
) {
505-
return push(rawExpr, NewlineType.None, node.loc)
505+
return push(rawExpr, NewlineType.None, loc)
506506
}
507507
if (isStatic) {
508-
// TODO
509-
return push(JSON.stringify(rawExpr))
508+
return push(JSON.stringify(rawExpr), NewlineType.None, loc)
510509
}
511510

512511
if (ast === null) {
@@ -522,27 +521,31 @@ function genExpression(node: IRExpression, context: CodegenContext): void {
522521
},
523522
true,
524523
)
525-
ids.sort((a, b) => a.start! - b.start!)
526-
ids.forEach((id, i) => {
527-
// range is offset by -1 due to the wrapping parens when parsed
528-
const start = id.start! - 1
529-
const end = id.end! - 1
530-
const last = ids[i - 1]
531-
532-
const leadingText = rawExpr.slice(last ? last.end! - 1 : 0, start)
533-
if (leadingText.length) push(leadingText, NewlineType.Unknown)
534-
535-
const source = rawExpr.slice(start, end)
536-
genIdentifier(source, context, {
537-
start: advancePositionWithClone(node.loc.start, source, start),
538-
end: advancePositionWithClone(node.loc.start, source, end),
539-
source,
524+
if (ids.length) {
525+
ids.sort((a, b) => a.start! - b.start!)
526+
ids.forEach((id, i) => {
527+
// range is offset by -1 due to the wrapping parens when parsed
528+
const start = id.start! - 1
529+
const end = id.end! - 1
530+
const last = ids[i - 1]
531+
532+
const leadingText = rawExpr.slice(last ? last.end! - 1 : 0, start)
533+
if (leadingText.length) push(leadingText, NewlineType.Unknown)
534+
535+
const source = rawExpr.slice(start, end)
536+
genIdentifier(source, context, {
537+
start: advancePositionWithClone(node.loc.start, source, start),
538+
end: advancePositionWithClone(node.loc.start, source, end),
539+
source,
540+
})
541+
542+
if (i === ids.length - 1 && end < rawExpr.length) {
543+
push(rawExpr.slice(end), NewlineType.Unknown)
544+
}
540545
})
541-
542-
if (i === ids.length - 1 && end < rawExpr.length) {
543-
push(rawExpr.slice(end), NewlineType.Unknown)
544-
}
545-
})
546+
} else {
547+
push(rawExpr, NewlineType.Unknown)
548+
}
546549
}
547550

548551
function genIdentifier(

0 commit comments

Comments
 (0)