From 0e0ee5b85e64c37f33ca7e7b0a569ebe4e9b756b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Thu, 22 Feb 2024 11:30:29 +0800 Subject: [PATCH] refactor(compiler-vapor): split block & root ir node --- .../transforms/transformElement.spec.ts | 16 ++--- .../__tests__/transforms/vBind.spec.ts | 38 ++++++------ .../__tests__/transforms/vFor.spec.ts | 12 ++-- .../__tests__/transforms/vHtml.spec.ts | 8 +-- .../__tests__/transforms/vIf.spec.ts | 48 +++++++-------- .../__tests__/transforms/vOn.spec.ts | 58 +++++++++---------- .../__tests__/transforms/vOnce.spec.ts | 16 ++--- .../__tests__/transforms/vText.spec.ts | 6 +- packages/compiler-vapor/src/generate.ts | 2 +- .../compiler-vapor/src/generators/block.ts | 17 +----- packages/compiler-vapor/src/generators/if.ts | 2 +- packages/compiler-vapor/src/ir.ts | 17 +++--- packages/compiler-vapor/src/transform.ts | 22 ++++--- .../compiler-vapor/src/transforms/vFor.ts | 6 +- packages/compiler-vapor/src/transforms/vIf.ts | 8 +-- 15 files changed, 134 insertions(+), 142 deletions(-) diff --git a/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts b/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts index 4ac9489f6..23decf4e1 100644 --- a/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts @@ -24,13 +24,13 @@ describe('compiler: element transform', () => { ) expect(code).toMatchSnapshot() expect(code).contains('
"') - expect(ir.effect.length).toBe(0) + expect(ir.block.effect).lengthOf(0) }) test('v-bind="obj"', () => { const { code, ir } = compileWithElementTransform(`
`) expect(code).toMatchSnapshot() - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -62,7 +62,7 @@ describe('compiler: element transform', () => { `
`, ) expect(code).toMatchSnapshot() - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -110,7 +110,7 @@ describe('compiler: element transform', () => { `
`, ) expect(code).toMatchSnapshot() - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -158,7 +158,7 @@ describe('compiler: element transform', () => { `
`, ) expect(code).toMatchSnapshot() - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -225,7 +225,7 @@ describe('compiler: element transform', () => { ) expect(code).toMatchSnapshot() - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, element: 1, @@ -261,7 +261,7 @@ describe('compiler: element transform', () => { ) expect(code).toMatchSnapshot() - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -306,7 +306,7 @@ describe('compiler: element transform', () => { expect(code).toMatchSnapshot() - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { diff --git a/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts b/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts index 204bab38e..68bf140f8 100644 --- a/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts @@ -18,15 +18,15 @@ describe('compiler v-bind', () => { test('basic', () => { const { ir, code } = compileWithVBind(`
`) - expect(ir.dynamic.children[0]).toMatchObject({ + expect(ir.block.dynamic.children[0]).toMatchObject({ id: 1, flags: DynamicFlag.REFERENCED, }) expect(ir.template).toEqual(['
']) - expect(ir.effect).lengthOf(1) - expect(ir.effect[0].expressions).lengthOf(1) - expect(ir.effect[0].operations).lengthOf(1) - expect(ir.effect[0]).toMatchObject({ + expect(ir.block.effect).lengthOf(1) + expect(ir.block.effect[0].expressions).lengthOf(1) + expect(ir.block.effect[0].operations).lengthOf(1) + expect(ir.block.effect[0]).toMatchObject({ expressions: [ { type: NodeTypes.SIMPLE_EXPRESSION, @@ -80,7 +80,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_PROP, prop: { key: { @@ -110,7 +110,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_PROP, prop: { key: { @@ -133,7 +133,7 @@ describe('compiler v-bind', () => { `
`, ) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_DYNAMIC_PROPS, element: 1, props: [ @@ -179,7 +179,7 @@ describe('compiler v-bind', () => { `
`, ) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_DYNAMIC_PROPS, element: 1, props: [ @@ -267,7 +267,7 @@ describe('compiler v-bind', () => { test('.camel modifier', () => { const { ir, code } = compileWithVBind(`
`) - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `fooBar`, @@ -292,7 +292,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `fooBar`, @@ -315,7 +315,7 @@ describe('compiler v-bind', () => { test('.camel modifier w/ dynamic arg', () => { const { ir, code } = compileWithVBind(`
`) - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_DYNAMIC_PROPS, props: [ [ @@ -350,7 +350,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `fooBar`, @@ -374,7 +374,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `fooBar`, @@ -398,7 +398,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_DYNAMIC_PROPS, props: [ [ @@ -431,7 +431,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `fooBar`, @@ -455,7 +455,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `fooBar`, @@ -479,7 +479,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `foo-bar`, @@ -503,7 +503,7 @@ describe('compiler v-bind', () => { const { ir, code } = compileWithVBind(`
`) expect(code).matchSnapshot() - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ prop: { key: { content: `foo-bar`, diff --git a/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts b/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts index 0ed9fc1d9..768ce03be 100644 --- a/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts @@ -28,7 +28,7 @@ describe('compiler: v-for', () => { expect(vaporHelpers).contains('createFor') expect(helpers.size).toBe(0) expect(ir.template).toEqual(['
']) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.FOR, id: 1, @@ -43,7 +43,7 @@ describe('compiler: v-for', () => { key: undefined, index: undefined, render: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 0, }, keyProperty: { @@ -52,13 +52,13 @@ describe('compiler: v-for', () => { }, }, ]) - expect(ir.returns).toEqual([1]) - expect(ir.dynamic).toMatchObject({ + expect(ir.block.returns).toEqual([1]) + expect(ir.block.dynamic).toMatchObject({ id: 0, children: { 0: { id: 1 } }, }) - expect(ir.effect).toEqual([]) - expect((ir.operation[0] as ForIRNode).render.effect).lengthOf(1) + expect(ir.block.effect).toEqual([]) + expect((ir.block.operation[0] as ForIRNode).render.effect).lengthOf(1) }) test('multi effect', () => { diff --git a/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts b/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts index c24990214..456356c2c 100644 --- a/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts @@ -23,8 +23,8 @@ describe('v-html', () => { expect(vaporHelpers).contains('setHtml') expect(helpers.size).toBe(0) - expect(ir.operation).toEqual([]) - expect(ir.effect).toMatchObject([ + expect(ir.block.operation).toEqual([]) + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -65,8 +65,8 @@ describe('v-html', () => { // children should have been removed expect(ir.template).toEqual(['
']) - expect(ir.operation).toEqual([]) - expect(ir.effect).toMatchObject([ + expect(ir.block.operation).toEqual([]) + expect(ir.block.effect).toMatchObject([ { expressions: [ { diff --git a/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts b/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts index 89fab97ee..b0875db09 100644 --- a/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts @@ -32,7 +32,7 @@ describe('compiler: v-if', () => { expect(helpers.size).toBe(0) expect(ir.template).toEqual(['
']) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.IF, id: 1, @@ -42,20 +42,20 @@ describe('compiler: v-if', () => { isStatic: false, }, positive: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 0, }, }, ]) - expect(ir.returns).toEqual([1]) + expect(ir.block.returns).toEqual([1]) - expect(ir.dynamic).toMatchObject({ + expect(ir.block.dynamic).toMatchObject({ id: 0, children: { 0: { id: 1 } }, }) - expect(ir.effect).toEqual([]) - expect((ir.operation[0] as IfIRNode).positive.effect).lengthOf(1) + expect(ir.block.effect).toEqual([]) + expect((ir.block.operation[0] as IfIRNode).positive.effect).lengthOf(1) expect(code).matchSnapshot() }) @@ -68,8 +68,8 @@ describe('compiler: v-if', () => { expect(ir.template).toEqual(['
hello

']) - expect(ir.effect).toEqual([]) - expect((ir.operation[0] as IfIRNode).positive.effect).toMatchObject([ + expect(ir.block.effect).toEqual([]) + expect((ir.block.operation[0] as IfIRNode).positive.effect).toMatchObject([ { operations: [ { @@ -86,7 +86,7 @@ describe('compiler: v-if', () => { ], }, ]) - expect((ir.operation[0] as IfIRNode).positive.dynamic).toMatchObject({ + expect((ir.block.operation[0] as IfIRNode).positive.dynamic).toMatchObject({ id: 2, children: { 2: { id: 3 } }, }) @@ -98,7 +98,7 @@ describe('compiler: v-if', () => { ) expect(code).matchSnapshot() expect(ir.template).toEqual(['
hello
']) - expect(ir.returns).toEqual([1, 3]) + expect(ir.block.returns).toEqual([1, 3]) }) test.todo('v-if with v-once') @@ -112,9 +112,9 @@ describe('compiler: v-if', () => { expect(ir.template).toEqual(['
', '

']) expect(vaporHelpers).contains('createIf') - expect(ir.effect).lengthOf(0) + expect(ir.block.effect).lengthOf(0) expect(helpers).lengthOf(0) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.IF, id: 1, @@ -124,16 +124,16 @@ describe('compiler: v-if', () => { isStatic: false, }, positive: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 0, }, negative: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 1, }, }, ]) - expect(ir.returns).toEqual([1]) + expect(ir.block.returns).toEqual([1]) }) test('v-if + v-else-if', () => { @@ -143,7 +143,7 @@ describe('compiler: v-if', () => { expect(code).matchSnapshot() expect(ir.template).toEqual(['
', '

']) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.IF, id: 1, @@ -153,7 +153,7 @@ describe('compiler: v-if', () => { isStatic: false, }, positive: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 0, }, negative: { @@ -164,13 +164,13 @@ describe('compiler: v-if', () => { isStatic: false, }, positive: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 1, }, }, }, ]) - expect(ir.returns).toEqual([1]) + expect(ir.block.returns).toEqual([1]) }) test('v-if + v-else-if + v-else', () => { @@ -180,23 +180,23 @@ describe('compiler: v-if', () => { expect(code).matchSnapshot() expect(ir.template).toEqual(['
', '

', 'fine']) - expect(ir.returns).toEqual([1]) - expect(ir.operation).toMatchObject([ + expect(ir.block.returns).toEqual([1]) + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.IF, id: 1, positive: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 0, }, negative: { type: IRNodeTypes.IF, positive: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 1, }, negative: { - type: IRNodeTypes.BLOCK_FUNCTION, + type: IRNodeTypes.BLOCK, templateIndex: 2, }, }, diff --git a/packages/compiler-vapor/__tests__/transforms/vOn.spec.ts b/packages/compiler-vapor/__tests__/transforms/vOn.spec.ts index 6903bbc75..1ef7efcaf 100644 --- a/packages/compiler-vapor/__tests__/transforms/vOn.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vOn.spec.ts @@ -22,9 +22,9 @@ describe('v-on', () => { expect(vaporHelpers).contains('on') expect(helpers.size).toBe(0) - expect(ir.effect).toEqual([]) + expect(ir.block.effect).toEqual([]) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, element: 1, @@ -87,9 +87,9 @@ describe('v-on', () => { expect(vaporHelpers).contains('on') expect(vaporHelpers).contains('renderEffect') expect(helpers.size).toBe(0) - expect(ir.operation).toEqual([]) + expect(ir.block.operation).toEqual([]) - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_EVENT, element: 1, key: { @@ -126,9 +126,9 @@ describe('v-on', () => { expect(vaporHelpers).contains('on') expect(vaporHelpers).contains('renderEffect') expect(helpers.size).toBe(0) - expect(ir.operation).toEqual([]) + expect(ir.block.operation).toEqual([]) - expect(ir.effect[0].operations[0]).toMatchObject({ + expect(ir.block.effect[0].operations[0]).toMatchObject({ type: IRNodeTypes.SET_EVENT, element: 1, key: { @@ -152,9 +152,9 @@ describe('v-on', () => { expect(vaporHelpers).contains('on') expect(helpers.size).toBe(0) - expect(ir.effect).toEqual([]) + expect(ir.block.effect).toEqual([]) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, element: 1, @@ -198,7 +198,7 @@ describe('v-on', () => { test('should handle multiple inline statement', () => { const { ir, code } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: 'foo();bar()' }, @@ -217,7 +217,7 @@ describe('v-on', () => { test('should handle multi-line statement', () => { const { code, ir } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: '\nfoo();\nbar()\n' }, @@ -238,7 +238,7 @@ describe('v-on', () => { prefixIdentifiers: true, }) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: 'foo($event)' }, @@ -257,7 +257,7 @@ describe('v-on', () => { prefixIdentifiers: true, }) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: 'foo($event);bar()' }, @@ -274,7 +274,7 @@ describe('v-on', () => { test('should NOT wrap as function if expression is already function expression', () => { const { code, ir } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: '$event => foo($event)' }, @@ -291,7 +291,7 @@ describe('v-on', () => { { expressionPlugins: ['typescript'] }, ) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: '(e: any): any => foo(e)' }, @@ -313,7 +313,7 @@ describe('v-on', () => { "/>`, ) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { @@ -337,7 +337,7 @@ describe('v-on', () => { }, ) - expect(ir.operation[0]).toMatchObject({ + expect(ir.block.operation[0]).toMatchObject({ type: IRNodeTypes.SET_EVENT, value: { content: '$event => {i++;foo($event)}' }, }) @@ -347,7 +347,7 @@ describe('v-on', () => { test('should NOT wrap as function if expression is complex member expression', () => { const { ir, code } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: `a['b' + c]` }, @@ -359,7 +359,7 @@ describe('v-on', () => { test('complex member expression w/ prefixIdentifiers: true', () => { const { ir, code } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: `a['b' + c]` }, @@ -375,7 +375,7 @@ describe('v-on', () => { prefixIdentifiers: true, }) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { content: `e => foo(e)` }, @@ -419,7 +419,7 @@ describe('v-on', () => { ) expect(vaporHelpers).contains('on') - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, value: { @@ -449,7 +449,7 @@ describe('v-on', () => { }, ) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, key: { @@ -504,7 +504,7 @@ describe('v-on', () => { }, ) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, element: 1, @@ -533,7 +533,7 @@ describe('v-on', () => { const { code, ir } = compileWithVOn(`
`, { prefixIdentifiers: true, }) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, modifiers: { nonKeys: ['exact'] }, @@ -548,7 +548,7 @@ describe('v-on', () => { prefixIdentifiers: true, }) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, modifiers: { @@ -567,7 +567,7 @@ describe('v-on', () => { prefixIdentifiers: true, }) - expect(ir.effect[0].operations).toMatchObject([ + expect(ir.block.effect[0].operations).toMatchObject([ { type: IRNodeTypes.SET_EVENT, key: { @@ -588,7 +588,7 @@ describe('v-on', () => { test('should transform click.right', () => { const { code, ir } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, key: { @@ -608,7 +608,7 @@ describe('v-on', () => { const { code: code2, ir: ir2 } = compileWithVOn( `
`, ) - expect(ir2.effect[0].operations).toMatchObject([ + expect(ir2.block.effect[0].operations).toMatchObject([ { type: IRNodeTypes.SET_EVENT, key: { @@ -629,7 +629,7 @@ describe('v-on', () => { test('should transform click.middle', () => { const { code, ir } = compileWithVOn(`
`) - expect(ir.operation).toMatchObject([ + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_EVENT, key: { @@ -650,7 +650,7 @@ describe('v-on', () => { `
`, ) - expect(ir2.effect[0].operations).toMatchObject([ + expect(ir2.block.effect[0].operations).toMatchObject([ { type: IRNodeTypes.SET_EVENT, key: { diff --git a/packages/compiler-vapor/__tests__/transforms/vOnce.spec.ts b/packages/compiler-vapor/__tests__/transforms/vOnce.spec.ts index 1ce024284..dc97d5b62 100644 --- a/packages/compiler-vapor/__tests__/transforms/vOnce.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vOnce.spec.ts @@ -26,8 +26,8 @@ describe('compiler: v-once', () => { expect(code).toMatchSnapshot() expect(helpers).lengthOf(0) - expect(ir.effect).lengthOf(0) - expect(ir.operation).toMatchObject([ + expect(ir.block.effect).lengthOf(0) + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.CREATE_TEXT_NODE, id: 1, @@ -79,8 +79,8 @@ describe('compiler: v-once', () => { expect(code).toMatchSnapshot() expect(helpers).lengthOf(0) - expect(ir.effect).lengthOf(0) - expect(ir.operation).toMatchObject([ + expect(ir.block.effect).lengthOf(0) + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_PROP, element: 1, @@ -110,8 +110,8 @@ describe('compiler: v-once', () => { expect(code).toMatchSnapshot() expect(helpers).lengthOf(0) - expect(ir.effect).lengthOf(0) - expect(ir.operation).toMatchObject([ + expect(ir.block.effect).lengthOf(0) + expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.SET_PROP, element: 1, @@ -144,8 +144,8 @@ describe('compiler: v-once', () => { expect(code).toMatchSnapshot() expect(helpers).lengthOf(0) - expect(ir.effect).lengthOf(0) - expect(ir.operation).lengthOf(0) + expect(ir.block.effect).lengthOf(0) + expect(ir.block.operation).lengthOf(0) }) test.todo('with hoistStatic: true') diff --git a/packages/compiler-vapor/__tests__/transforms/vText.spec.ts b/packages/compiler-vapor/__tests__/transforms/vText.spec.ts index 3e9cfccf2..b3cb882a0 100644 --- a/packages/compiler-vapor/__tests__/transforms/vText.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vText.spec.ts @@ -23,9 +23,9 @@ describe('v-text', () => { expect(vaporHelpers).contains('setText') expect(helpers.size).toBe(0) - expect(ir.operation).toEqual([]) + expect(ir.block.operation).toEqual([]) - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { @@ -65,7 +65,7 @@ describe('v-text', () => { // children should have been removed expect(ir.template).toEqual(['
']) - expect(ir.effect).toMatchObject([ + expect(ir.block.effect).toMatchObject([ { expressions: [ { diff --git a/packages/compiler-vapor/src/generate.ts b/packages/compiler-vapor/src/generate.ts index 79bf9889a..3d212a0c8 100644 --- a/packages/compiler-vapor/src/generate.ts +++ b/packages/compiler-vapor/src/generate.ts @@ -118,7 +118,7 @@ export function generate( } push(INDENT_START) - push(...genBlockFunctionContent(ir, context)) + push(...genBlockFunctionContent(ir.block, context)) push(INDENT_END, NEWLINE) if (isSetupInlined) { diff --git a/packages/compiler-vapor/src/generators/block.ts b/packages/compiler-vapor/src/generators/block.ts index 1b2188341..0b7f0e2ee 100644 --- a/packages/compiler-vapor/src/generators/block.ts +++ b/packages/compiler-vapor/src/generators/block.ts @@ -1,9 +1,4 @@ -import { - type BlockFunctionIRNode, - IRNodeTypes, - type RootIRNode, - type WithDirectiveIRNode, -} from '../ir' +import { type BlockIRNode, IRNodeTypes, type WithDirectiveIRNode } from '../ir' import { type CodeFragment, INDENT_END, @@ -18,7 +13,7 @@ import { genChildren } from './template' import { genMulti } from './utils' export function genBlockFunction( - oper: BlockFunctionIRNode, + oper: BlockIRNode, context: CodegenContext, args: CodeFragment[] = [], customReturns?: (returns: CodeFragment[]) => CodeFragment[], @@ -36,13 +31,7 @@ export function genBlockFunction( } export function genBlockFunctionContent( - { - dynamic, - effect, - operation, - templateIndex, - returns, - }: BlockFunctionIRNode | RootIRNode, + { dynamic, effect, operation, templateIndex, returns }: BlockIRNode, context: CodegenContext, customReturns?: (returns: CodeFragment[]) => CodeFragment[], ): CodeFragment[] { diff --git a/packages/compiler-vapor/src/generators/if.ts b/packages/compiler-vapor/src/generators/if.ts index 7f960f145..ce92e6601 100644 --- a/packages/compiler-vapor/src/generators/if.ts +++ b/packages/compiler-vapor/src/generators/if.ts @@ -23,7 +23,7 @@ export function genIf( let negativeArg: false | CodeFragment[] = false if (negative) { - if (negative.type === IRNodeTypes.BLOCK_FUNCTION) { + if (negative.type === IRNodeTypes.BLOCK) { negativeArg = genBlockFunction(negative, context) } else { negativeArg = ['() => ', ...genIf(negative!, context, true)] diff --git a/packages/compiler-vapor/src/ir.ts b/packages/compiler-vapor/src/ir.ts index fdcf5ca3b..70307cbef 100644 --- a/packages/compiler-vapor/src/ir.ts +++ b/packages/compiler-vapor/src/ir.ts @@ -15,7 +15,7 @@ import type { export enum IRNodeTypes { ROOT, - BLOCK_FUNCTION, + BLOCK, SET_PROP, SET_DYNAMIC_PROPS, @@ -42,8 +42,8 @@ export interface BaseIRNode { export type VaporHelper = keyof typeof import('@vue/runtime-vapor') -export interface BlockFunctionIRNode extends BaseIRNode { - type: IRNodeTypes.BLOCK_FUNCTION +export interface BlockIRNode extends BaseIRNode { + type: IRNodeTypes.BLOCK node: RootNode | TemplateChildNode templateIndex: number dynamic: IRDynamicInfo @@ -52,19 +52,20 @@ export interface BlockFunctionIRNode extends BaseIRNode { returns: number[] } -export interface RootIRNode extends Omit { +export interface RootIRNode { type: IRNodeTypes.ROOT node: RootNode source: string template: string[] + block: BlockIRNode } export interface IfIRNode extends BaseIRNode { type: IRNodeTypes.IF id: number condition: SimpleExpressionNode - positive: BlockFunctionIRNode - negative?: BlockFunctionIRNode | IfIRNode + positive: BlockIRNode + negative?: BlockIRNode | IfIRNode } export interface ForIRNode extends BaseIRNode { @@ -75,7 +76,7 @@ export interface ForIRNode extends BaseIRNode { key?: SimpleExpressionNode index?: SimpleExpressionNode keyProperty?: SimpleExpressionNode - render: BlockFunctionIRNode + render: BlockIRNode } export interface IRProp extends Omit { @@ -187,8 +188,6 @@ export type OperationNode = | IfIRNode | ForIRNode -export type BlockIRNode = RootIRNode | BlockFunctionIRNode - export enum DynamicFlag { NONE = 0, /** diff --git a/packages/compiler-vapor/src/transform.ts b/packages/compiler-vapor/src/transform.ts index c42acb343..1aacc1ee3 100644 --- a/packages/compiler-vapor/src/transform.ts +++ b/packages/compiler-vapor/src/transform.ts @@ -118,7 +118,7 @@ function createRootContext( parent: null, index: 0, root: null!, // set later - block: root, + block: root.block, enterBlock(ir) { const { block, template, dynamic, childrenTemplate } = this this.block = ir @@ -134,7 +134,7 @@ function createRootContext( } }, options: extend({}, defaultOptions, options), - dynamic: root.dynamic, + dynamic: root.block.dynamic, inVOnce: false, increaseId: () => globalId++, @@ -222,13 +222,17 @@ export function transform( node: root, source: root.source, template: [], - templateIndex: -1, - dynamic: extend(genDefaultDynamic(), { - flags: DynamicFlag.REFERENCED, - } satisfies Partial), - effect: [], - operation: [], - returns: [], + block: { + type: IRNodeTypes.BLOCK, + node: root, + templateIndex: -1, + dynamic: extend(genDefaultDynamic(), { + flags: DynamicFlag.REFERENCED, + } satisfies Partial), + effect: [], + operation: [], + returns: [], + }, } const context = createRootContext(ir, root, options) diff --git a/packages/compiler-vapor/src/transforms/vFor.ts b/packages/compiler-vapor/src/transforms/vFor.ts index 0e54f94d2..fab676a67 100644 --- a/packages/compiler-vapor/src/transforms/vFor.ts +++ b/packages/compiler-vapor/src/transforms/vFor.ts @@ -9,7 +9,7 @@ import { createStructuralDirectiveTransform, } from '../transform' import { - type BlockFunctionIRNode, + type BlockIRNode, DynamicFlag, type IRDynamicInfo, IRNodeTypes, @@ -50,8 +50,8 @@ export function processFor( context.node = node = wrapTemplate(node, ['for']) context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT const id = context.reference() - const render: BlockFunctionIRNode = { - type: IRNodeTypes.BLOCK_FUNCTION, + const render: BlockIRNode = { + type: IRNodeTypes.BLOCK, node, templateIndex: -1, dynamic: extend(genDefaultDynamic(), { diff --git a/packages/compiler-vapor/src/transforms/vIf.ts b/packages/compiler-vapor/src/transforms/vIf.ts index fd080ddcd..05ae0553d 100644 --- a/packages/compiler-vapor/src/transforms/vIf.ts +++ b/packages/compiler-vapor/src/transforms/vIf.ts @@ -11,7 +11,7 @@ import { createStructuralDirectiveTransform, } from '../transform' import { - type BlockFunctionIRNode, + type BlockIRNode, DynamicFlag, type IRDynamicInfo, IRNodeTypes, @@ -140,11 +140,11 @@ export function processIf( export function createIfBranch( node: ElementNode, context: TransformContext, -): [BlockFunctionIRNode, () => void] { +): [BlockIRNode, () => void] { context.node = node = wrapTemplate(node, ['if', 'else-if', 'else']) - const branch: BlockFunctionIRNode = { - type: IRNodeTypes.BLOCK_FUNCTION, + const branch: BlockIRNode = { + type: IRNodeTypes.BLOCK, node, templateIndex: -1, dynamic: extend(genDefaultDynamic(), {