Skip to content

Commit ed26551

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

File tree

8 files changed

+24
-114
lines changed

8 files changed

+24
-114
lines changed

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)