Skip to content

Commit 66c0e82

Browse files
committed
refactor(compiler-vapor): remove template ir node
1 parent ba29b4c commit 66c0e82

File tree

9 files changed

+24
-192
lines changed

9 files changed

+24
-192
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vModel.spec.ts.snap

-78
Original file line numberDiff line numberDiff line change
@@ -156,81 +156,3 @@ export function render(_ctx) {
156156
return n0
157157
}"
158158
`;
159-
160-
exports[`compiler: vModel transform > should work with input (checkbox) 1`] = `
161-
"import { children as _children, vModelCheckbox as _vModelCheckbox, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor';
162-
const t0 = _template("<input type=\\"checkbox\\">")
163-
164-
export function render(_ctx) {
165-
const n0 = t0()
166-
const n1 = _children(n0, 0)
167-
_withDirectives(n1, [[_vModelCheckbox, () => _ctx.model]])
168-
_on(n1, "update:modelValue", $event => (_ctx.model = $event))
169-
return n0
170-
}"
171-
`;
172-
173-
exports[`compiler: vModel transform > should work with input (dynamic type) 1`] = `
174-
"import { children as _children, vModelDynamic as _vModelDynamic, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor';
175-
const t0 = _template("<input>")
176-
177-
export function render(_ctx) {
178-
const n0 = t0()
179-
const n1 = _children(n0, 0)
180-
_withDirectives(n1, [[_vModelDynamic, () => _ctx.model]])
181-
_on(n1, "update:modelValue", $event => (_ctx.model = $event))
182-
return n0
183-
}"
184-
`;
185-
186-
exports[`compiler: vModel transform > should work with input (radio) 1`] = `
187-
"import { children as _children, vModelRadio as _vModelRadio, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor';
188-
const t0 = _template("<input type=\\"radio\\">")
189-
190-
export function render(_ctx) {
191-
const n0 = t0()
192-
const n1 = _children(n0, 0)
193-
_withDirectives(n1, [[_vModelRadio, () => _ctx.model]])
194-
_on(n1, "update:modelValue", $event => (_ctx.model = $event))
195-
return n0
196-
}"
197-
`;
198-
199-
exports[`compiler: vModel transform > should work with input (text) 1`] = `
200-
"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor';
201-
const t0 = _template("<input type=\\"text\\">")
202-
203-
export function render(_ctx) {
204-
const n0 = t0()
205-
const n1 = _children(n0, 0)
206-
_withDirectives(n1, [[_vModelText, () => _ctx.model]])
207-
_on(n1, "update:modelValue", $event => (_ctx.model = $event))
208-
return n0
209-
}"
210-
`;
211-
212-
exports[`compiler: vModel transform > should work with select 1`] = `
213-
"import { children as _children, vModelSelect as _vModelSelect, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor';
214-
const t0 = _template("<select></select>")
215-
216-
export function render(_ctx) {
217-
const n0 = t0()
218-
const n1 = _children(n0, 0)
219-
_withDirectives(n1, [[_vModelSelect, () => _ctx.model]])
220-
_on(n1, "update:modelValue", $event => (_ctx.model = $event))
221-
return n0
222-
}"
223-
`;
224-
225-
exports[`compiler: vModel transform > should work with simple expression 1`] = `
226-
"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor';
227-
const t0 = _template("<input>")
228-
229-
export function render(_ctx) {
230-
const n0 = t0()
231-
const n1 = _children(n0, 0)
232-
_withDirectives(n1, [[_vModelText, () => _ctx.model]])
233-
_on(n1, "update:modelValue", $event => (_ctx.model = $event))
234-
return n0
235-
}"
236-
`;

packages/compiler-vapor/__tests__/transforms/vBind.spec.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ describe('compiler v-bind', () => {
2222
id: 1,
2323
flags: DynamicFlag.REFERENCED,
2424
})
25-
expect(ir.template[0]).toMatchObject({
26-
type: IRNodeTypes.TEMPLATE_FACTORY,
27-
template: '<div></div>',
28-
})
25+
expect(ir.template).toEqual(['<div></div>'])
2926
expect(ir.effect).lengthOf(1)
3027
expect(ir.effect[0].expressions).lengthOf(1)
3128
expect(ir.effect[0].operations).lengthOf(1)
@@ -243,10 +240,7 @@ describe('compiler v-bind', () => {
243240
end: { line: 1, column: 19 },
244241
},
245242
})
246-
expect(ir.template[0]).toMatchObject({
247-
type: IRNodeTypes.TEMPLATE_FACTORY,
248-
template: '<div arg></div>',
249-
})
243+
expect(ir.template).toEqual(['<div arg></div>'])
250244

251245
expect(code).matchSnapshot()
252246
expect(code).contains(JSON.stringify('<div arg></div>'))

packages/compiler-vapor/__tests__/transforms/vFor.spec.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ describe('compiler: v-for', () => {
2727
expect(code).matchSnapshot()
2828
expect(vaporHelpers).contains('createFor')
2929
expect(helpers.size).toBe(0)
30-
expect(ir.template).lengthOf(1)
31-
expect(ir.template).toMatchObject([
32-
{
33-
template: '<div></div>',
34-
type: IRNodeTypes.TEMPLATE_FACTORY,
35-
},
36-
])
30+
expect(ir.template).toEqual(['<div></div>'])
3731
expect(ir.operation).toMatchObject([
3832
{
3933
type: IRNodeTypes.FOR,

packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('v-html', () => {
6363
expect(helpers.size).toBe(0)
6464

6565
// children should have been removed
66-
expect(ir.template).toMatchObject([{ template: '<div></div>' }])
66+
expect(ir.template).toEqual(['<div></div>'])
6767

6868
expect(ir.operation).toEqual([])
6969
expect(ir.effect).toMatchObject([

packages/compiler-vapor/__tests__/transforms/vIf.spec.ts

+12-76
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ describe('compiler: v-if', () => {
3131
expect(vaporHelpers).contains('createIf')
3232
expect(helpers.size).toBe(0)
3333

34-
expect(ir.template).lengthOf(1)
35-
expect(ir.template).toMatchObject([
36-
{
37-
template: '<div></div>',
38-
type: IRNodeTypes.TEMPLATE_FACTORY,
39-
},
40-
])
34+
expect(ir.template).toEqual(['<div></div>'])
4135
expect(ir.operation).toMatchObject([
4236
{
4337
type: IRNodeTypes.IF,
@@ -72,13 +66,7 @@ describe('compiler: v-if', () => {
7266
)
7367
expect(code).matchSnapshot()
7468

75-
expect(ir.template).lengthOf(1)
76-
expect(ir.template).toMatchObject([
77-
{
78-
template: '<div></div>hello<p></p>',
79-
type: IRNodeTypes.TEMPLATE_FACTORY,
80-
},
81-
])
69+
expect(ir.template).toEqual(['<div></div>hello<p></p>'])
8270

8371
expect(ir.effect).toEqual([])
8472
expect((ir.operation[0] as IfIRNode).positive.effect).toMatchObject([
@@ -109,13 +97,7 @@ describe('compiler: v-if', () => {
10997
`<div v-if="ok">hello</div><div v-if="ok">hello</div>`,
11098
)
11199
expect(code).matchSnapshot()
112-
expect(ir.template).lengthOf(1)
113-
expect(ir.template).toMatchObject([
114-
{
115-
template: '<div>hello</div>',
116-
type: 2,
117-
},
118-
])
100+
expect(ir.template).toEqual(['<div>hello</div>'])
119101
expect(ir.returns).toEqual([1, 3])
120102
})
121103

@@ -127,17 +109,7 @@ describe('compiler: v-if', () => {
127109
`<div v-if="ok"/><p v-else/>`,
128110
)
129111
expect(code).matchSnapshot()
130-
expect(ir.template).lengthOf(2)
131-
expect(ir.template).toMatchObject([
132-
{
133-
template: '<div></div>',
134-
type: IRNodeTypes.TEMPLATE_FACTORY,
135-
},
136-
{
137-
template: '<p></p>',
138-
type: IRNodeTypes.TEMPLATE_FACTORY,
139-
},
140-
])
112+
expect(ir.template).toEqual(['<div></div>', '<p></p>'])
141113

142114
expect(vaporHelpers).contains('createIf')
143115
expect(ir.effect).lengthOf(0)
@@ -169,17 +141,7 @@ describe('compiler: v-if', () => {
169141
`<div v-if="ok"/><p v-else-if="orNot"/>`,
170142
)
171143
expect(code).matchSnapshot()
172-
expect(ir.template).lengthOf(2)
173-
expect(ir.template).toMatchObject([
174-
{
175-
template: '<div></div>',
176-
type: IRNodeTypes.TEMPLATE_FACTORY,
177-
},
178-
{
179-
template: '<p></p>',
180-
type: IRNodeTypes.TEMPLATE_FACTORY,
181-
},
182-
])
144+
expect(ir.template).toEqual(['<div></div>', '<p></p>'])
183145

184146
expect(ir.operation).toMatchObject([
185147
{
@@ -216,21 +178,7 @@ describe('compiler: v-if', () => {
216178
`<div v-if="ok"/><p v-else-if="orNot"/><template v-else>fine</template>`,
217179
)
218180
expect(code).matchSnapshot()
219-
expect(ir.template).lengthOf(3)
220-
expect(ir.template).toMatchObject([
221-
{
222-
template: '<div></div>',
223-
type: IRNodeTypes.TEMPLATE_FACTORY,
224-
},
225-
{
226-
template: '<p></p>',
227-
type: IRNodeTypes.TEMPLATE_FACTORY,
228-
},
229-
{
230-
template: 'fine',
231-
type: IRNodeTypes.TEMPLATE_FACTORY,
232-
},
233-
])
181+
expect(ir.template).toEqual(['<div></div>', '<p></p>', 'fine'])
234182

235183
expect(ir.returns).toEqual([1])
236184
expect(ir.operation).toMatchObject([
@@ -266,24 +214,12 @@ describe('compiler: v-if', () => {
266214
<input v-text="text" />
267215
`)
268216
expect(code).matchSnapshot()
269-
expect(ir.template).lengthOf(4)
270-
expect(ir.template).toMatchObject([
271-
{
272-
template: '<div></div>',
273-
type: IRNodeTypes.TEMPLATE_FACTORY,
274-
},
275-
{
276-
template: '<!--foo--><p></p>',
277-
type: IRNodeTypes.TEMPLATE_FACTORY,
278-
},
279-
{
280-
template: '<!--bar-->fine',
281-
type: IRNodeTypes.TEMPLATE_FACTORY,
282-
},
283-
{
284-
type: IRNodeTypes.TEMPLATE_FACTORY,
285-
template: '<input>',
286-
},
217+
expect(ir.template).toEqual([
218+
'<div></div>',
219+
'<!--foo--><p></p>',
220+
'<!--bar-->fine',
221+
222+
'<input>',
287223
])
288224
})
289225

packages/compiler-vapor/__tests__/transforms/vText.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('v-text', () => {
6363
])
6464

6565
// children should have been removed
66-
expect(ir.template).toMatchObject([{ template: '<div></div>' }])
66+
expect(ir.template).toEqual(['<div></div>'])
6767

6868
expect(ir.effect).toMatchObject([
6969
{

packages/compiler-vapor/src/generators/template.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import type { CodegenContext } from '../generate'
2-
import {
3-
DynamicFlag,
4-
type IRDynamicInfo,
5-
type TemplateFactoryIRNode,
6-
} from '../ir'
2+
import { DynamicFlag, type IRDynamicInfo } from '../ir'
73
import { NEWLINE, buildCodeFragment, genCall } from './utils'
84

95
export function genTemplates(
10-
templates: TemplateFactoryIRNode[],
6+
templates: string[],
117
{ vaporHelper }: CodegenContext,
128
) {
139
return templates
1410
.map(
1511
(template, i) =>
16-
`const t${i} = ${vaporHelper('template')}(${JSON.stringify(template.template)})\n`,
12+
`const t${i} = ${vaporHelper('template')}(${JSON.stringify(template)})\n`,
1713
)
1814
.join('')
1915
}

packages/compiler-vapor/src/ir.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export enum IRNodeTypes {
1717
ROOT,
1818
BLOCK_FUNCTION,
1919

20-
TEMPLATE_FACTORY,
21-
2220
SET_PROP,
2321
SET_DYNAMIC_PROPS,
2422
SET_TEXT,
@@ -58,7 +56,7 @@ export interface RootIRNode extends Omit<BlockFunctionIRNode, 'type'> {
5856
type: IRNodeTypes.ROOT
5957
node: RootNode
6058
source: string
61-
template: Array<TemplateFactoryIRNode>
59+
template: string[]
6260
}
6361

6462
export interface IfIRNode extends BaseIRNode {
@@ -80,11 +78,6 @@ export interface ForIRNode extends BaseIRNode {
8078
render: BlockFunctionIRNode
8179
}
8280

83-
export interface TemplateFactoryIRNode extends BaseIRNode {
84-
type: IRNodeTypes.TEMPLATE_FACTORY
85-
template: string
86-
}
87-
8881
export interface IRProp extends Omit<DirectiveTransformResult, 'value'> {
8982
values: SimpleExpressionNode[]
9083
}
@@ -177,7 +170,7 @@ export interface WithDirectiveIRNode extends BaseIRNode {
177170
builtin?: VaporHelper
178171
}
179172

180-
export type IRNode = OperationNode | RootIRNode | TemplateFactoryIRNode
173+
export type IRNode = OperationNode | RootIRNode
181174
export type OperationNode =
182175
| SetPropIRNode
183176
| SetDynamicPropsIRNode

packages/compiler-vapor/src/transform.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,13 @@ function createRootContext(
178178
}
179179

180180
const existing = root.template.findIndex(
181-
t => t.template === this.template,
181+
template => template === this.template,
182182
)
183183
if (existing !== -1) {
184184
return (this.block.templateIndex = existing)
185185
}
186186

187-
root.template.push({
188-
type: IRNodeTypes.TEMPLATE_FACTORY,
189-
template: this.template,
190-
})
187+
root.template.push(this.template)
191188
return (this.block.templateIndex = root.template.length - 1)
192189
},
193190
registerOperation(...node) {

0 commit comments

Comments
 (0)