Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler/runtime-vapor): implement v-slots + v-for / v-if #207

Merged
merged 21 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b51c735
feat(compiler-vapor): static v-slot
LittleSound May 7, 2024
0c0a131
test: improve the test
LittleSound May 7, 2024
8ed97a3
feat(compiler-vapor): impl dynamic slot name
Doctor-wu May 8, 2024
4683a64
refactor(compiler-vapor): impl dynamic slot name + add error detection
LittleSound May 8, 2024
0852d62
refactor
sxzz May 10, 2024
15fb060
Merge branch 'main' into feat/static-v-slot
Doctor-wu May 11, 2024
2e99a89
feat(compiler-vapor, runtime-vapor): implement slots + v-for
Doctor-wu May 11, 2024
fd0a773
feat(compiler-vapor): remove unused import
Doctor-wu May 11, 2024
7297cae
test(compiler-vapor): add test case
Doctor-wu May 11, 2024
bc1b230
feat(compiler-vapor, runtime-vapor): implement v-slot + v-if
Doctor-wu May 12, 2024
84cac2d
Merge branch 'main' into feature-dynamic-for-slot
Doctor-wu May 12, 2024
77e6755
chore(compiler-vapor): use interface instead of type
Doctor-wu May 12, 2024
c780263
chore(compiler-vapor): simplify implement
Doctor-wu May 12, 2024
02d8ec9
Merge remote-tracking branch 'origin' into feature-dynamic-for-slot
Doctor-wu May 13, 2024
f516921
Merge branch 'main' into feature-dynamic-for-slot
Doctor-wu May 14, 2024
a6f7f55
Merge branch 'main' into feature-dynamic-for-slot
Doctor-wu May 18, 2024
2f4af7e
refactor
sxzz May 19, 2024
e5ccc79
test: update snap
sxzz May 19, 2024
7ddd1d3
refactor
sxzz May 19, 2024
e454717
fix(compiler-vapor): fix argument generate when item absent
Doctor-wu May 19, 2024
47008f7
chore(compiler-vapor): simplify impl
Doctor-wu May 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,61 @@ export function render(_ctx) {
}"
`;

exports[`compiler: transform slot > dynamic slots name w/ v-for 1`] = `
"import { resolveComponent as _resolveComponent, createComponent as _createComponent, createForSlots as _createForSlots, template as _template } from 'vue/vapor';
const t0 = _template("foo")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponent(_component_Comp, null, null, () => [_createForSlots(_ctx.list, (item) => ({
name: _ctx.i,
fn: () => {
const n0 = t0()
return n0
}
}))], true)
return n2
}"
`;

exports[`compiler: transform slot > dynamic slots name w/ v-if / v-else[-if] 1`] = `
"import { resolveComponent as _resolveComponent, createComponent as _createComponent, template as _template } from 'vue/vapor';
const t0 = _template("condition slot")
const t1 = _template("another condition")
const t2 = _template("else condition")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n6 = _createComponent(_component_Comp, null, null, () => [_ctx.condition
? {
name: "condition",
fn: () => {
const n0 = t0()
return n0
},
key: "0"
}
: _ctx.anotherCondition
? {
name: "condition",
fn: () => {
const n2 = t1()
return n2
},
key: "1"
}
: {
name: "condition",
fn: () => {
const n4 = t2()
return n4
},
key: "2"
}], true)
return n6
}"
`;

exports[`compiler: transform slot > implicit default slot 1`] = `
"import { resolveComponent as _resolveComponent, createComponent as _createComponent, template as _template } from 'vue/vapor';
const t0 = _template("<div></div>")
Expand Down
72 changes: 72 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ErrorCodes, NodeTypes } from '@vue/compiler-core'
import {
DynamicSlotType,
IRNodeTypes,
transformChildren,
transformElement,
Expand Down Expand Up @@ -126,6 +127,77 @@ describe('compiler: transform slot', () => {
])
})

test('dynamic slots name w/ v-for', () => {
const { ir, code } = compileWithSlots(
`<Comp>
<template v-for="item in list" #[i]>foo</template>
</Comp>`,
)
expect(code).toMatchSnapshot()
expect(ir.block.operation[0].type).toBe(IRNodeTypes.CREATE_COMPONENT_NODE)
expect(ir.block.operation).toMatchObject([
{
type: IRNodeTypes.CREATE_COMPONENT_NODE,
tag: 'Comp',
slots: undefined,
dynamicSlots: [
{
name: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'i',
isStatic: false,
},
fn: { type: IRNodeTypes.BLOCK },
loop: {
source: { content: 'list' },
value: { content: 'item' },
key: undefined,
index: undefined,
},
},
],
},
])
})

test('dynamic slots name w/ v-if / v-else[-if]', () => {
const { ir, code } = compileWithSlots(
`<Comp>
<template v-if="condition" #condition>condition slot</template>
<template v-else-if="anotherCondition" #condition>another condition</template>
<template v-else #condition>else condition</template>
</Comp>`,
)
expect(code).toMatchSnapshot()
expect(ir.block.operation[0].type).toBe(IRNodeTypes.CREATE_COMPONENT_NODE)
expect(ir.block.operation).toMatchObject([
{
type: IRNodeTypes.CREATE_COMPONENT_NODE,
tag: 'Comp',
slots: undefined,
dynamicSlots: [
{
slotType: DynamicSlotType.CONDITIONAL,
condition: { content: 'condition' },
positive: {
slotType: DynamicSlotType.BASIC,
key: 0,
},
negative: {
slotType: DynamicSlotType.CONDITIONAL,
condition: { content: 'anotherCondition' },
positive: {
slotType: DynamicSlotType.BASIC,
key: 1,
},
negative: { slotType: DynamicSlotType.BASIC, key: 2 },
},
},
],
},
])
})

describe('errors', () => {
test('error on extraneous children w/ named default slot', () => {
const onError = vi.fn()
Expand Down
96 changes: 89 additions & 7 deletions packages/compiler-vapor/src/generators/component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { camelize, extend, isArray } from '@vue/shared'
import type { CodegenContext } from '../generate'
import {
type ComponentBasicDynamicSlot,
type ComponentConditionalDynamicSlot,
type ComponentDynamicSlot,
type ComponentLoopDynamicSlot,
type ComponentSlots,
type CreateComponentIRNode,
DynamicSlotType,
IRDynamicPropsKind,
type IRProp,
type IRProps,
Expand All @@ -15,6 +19,8 @@ import {
DELIMITERS_ARRAY_NEWLINE,
DELIMITERS_OBJECT,
DELIMITERS_OBJECT_NEWLINE,
INDENT_END,
INDENT_START,
NEWLINE,
genCall,
genMulti,
Expand Down Expand Up @@ -155,13 +161,89 @@ function genDynamicSlots(
) {
const slotsExpr = genMulti(
dynamicSlots.length > 1 ? DELIMITERS_ARRAY_NEWLINE : DELIMITERS_ARRAY,
...dynamicSlots.map(({ name, fn }) =>
genMulti(
DELIMITERS_OBJECT_NEWLINE,
['name: ', ...genExpression(name, context)],
['fn: ', ...genBlock(fn, context)],
),
),
...dynamicSlots.map(slot => genDynamicSlot(slot, context)),
)
return ['() => ', ...slotsExpr]
}

function genDynamicSlot(
slot: ComponentDynamicSlot,
context: CodegenContext,
): CodeFragment[] {
switch (slot.slotType) {
case DynamicSlotType.BASIC:
return genBasicDynamicSlot(slot, context)
case DynamicSlotType.LOOP:
return genLoopSlot(slot, context)
case DynamicSlotType.CONDITIONAL:
return genConditionalSlot(slot, context)
}
}

function genBasicDynamicSlot(
slot: ComponentBasicDynamicSlot,
context: CodegenContext,
): CodeFragment[] {
const { name, fn, key } = slot
return genMulti(
DELIMITERS_OBJECT_NEWLINE,
['name: ', ...genExpression(name, context)],
['fn: ', ...genBlock(fn, context)],
...(key !== undefined ? [`key: "${key}"`] : []),
)
}

function genLoopSlot(
slot: ComponentLoopDynamicSlot,
context: CodegenContext,
): CodeFragment[] {
const { name, fn, loop } = slot
const { value, key, index, source } = loop
const rawValue = value && value.content
const rawKey = key && key.content
const rawIndex = index && index.content

const idMap: Record<string, string> = {}
if (rawValue) idMap[rawValue] = rawValue
if (rawKey) idMap[rawKey] = rawKey
if (rawIndex) idMap[rawIndex] = rawIndex
const slotExpr = genMulti(
DELIMITERS_OBJECT_NEWLINE,
['name: ', ...context.withId(() => genExpression(name, context), idMap)],
['fn: ', ...context.withId(() => genBlock(fn, context), idMap)],
)
return [
...genCall(
context.vaporHelper('createForSlots'),
genExpression(source, context),
[
`(`,
[value, key, index]
.filter(Boolean)
.map(exp => exp?.content)
.join(', '),
') => (',
...slotExpr,
')',
],
),
]
}

function genConditionalSlot(
slot: ComponentConditionalDynamicSlot,
context: CodegenContext,
): CodeFragment[] {
const { condition, positive, negative } = slot
return [
...genExpression(condition, context),
INDENT_START,
NEWLINE,
'? ',
...genDynamicSlot(positive, context),
NEWLINE,
': ',
...(negative ? [...genDynamicSlot(negative, context)] : ['void 0']),
INDENT_END,
]
}
40 changes: 35 additions & 5 deletions packages/compiler-vapor/src/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ export interface IfIRNode extends BaseIRNode {
once?: boolean
}

export interface ForIRNode extends BaseIRNode {
type: IRNodeTypes.FOR
id: number
export interface IRFor {
source: SimpleExpressionNode
value?: SimpleExpressionNode
key?: SimpleExpressionNode
index?: SimpleExpressionNode
}

export interface ForIRNode extends BaseIRNode, IRFor {
type: IRNodeTypes.FOR
id: number
keyProp?: SimpleExpressionNode
render: BlockIRNode
once: boolean
Expand Down Expand Up @@ -208,12 +211,39 @@ export interface ComponentSlotBlockIRNode extends BlockIRNode {
// TODO slot props
}
export type ComponentSlots = Record<string, ComponentSlotBlockIRNode>
export interface ComponentDynamicSlot {

export enum DynamicSlotType {
BASIC,
LOOP,
CONDITIONAL,
}

export interface ComponentBasicDynamicSlot {
slotType: DynamicSlotType.BASIC
name: SimpleExpressionNode
fn: ComponentSlotBlockIRNode
key?: string
key?: number
}

export interface ComponentLoopDynamicSlot {
slotType: DynamicSlotType.LOOP
name: SimpleExpressionNode
fn: ComponentSlotBlockIRNode
loop: IRFor
}

export interface ComponentConditionalDynamicSlot {
slotType: DynamicSlotType.CONDITIONAL
condition: SimpleExpressionNode
positive: ComponentBasicDynamicSlot
negative?: ComponentBasicDynamicSlot | ComponentConditionalDynamicSlot
}

export type ComponentDynamicSlot =
| ComponentBasicDynamicSlot
| ComponentLoopDynamicSlot
| ComponentConditionalDynamicSlot

export interface CreateComponentIRNode extends BaseIRNode {
type: IRNodeTypes.CREATE_COMPONENT_NODE
id: number
Expand Down