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

perf(runtime-vapor): refactor apiCreateIf #12629

Open
wants to merge 6 commits into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export function render(_ctx) {
const _directive_test = _resolveDirective("test")
const n4 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n0 = _createIf(() => (true), () => {
const n0 = _createIf(() => (true) ? () => {
const n3 = t0()
const n2 = _createComponentWithFallback(_component_Bar)
_withDirectives(n2, [[_directive_hello, void 0, void 0, { world: true }]])
_insert(n2, n3)
return n3
})
} : undefined)
return n0
}
}, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const t0 = _template("<div></div>", true)

export function render(_ctx) {
const _setTemplateRef = _createTemplateRefSetter()
const n0 = _createIf(() => (true), () => {
const n0 = _createIf(() => (true) ? () => {
const n2 = t0()
_setTemplateRef(n2, "foo")
return n2
})
} : undefined)
return n0
}"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ exports[`compiler: v-if > basic v-if 1`] = `
const t0 = _template("<div></div>", true)

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
_renderEffect(() => _setText(n2, _ctx.msg))
return n2
})
} : undefined)
return n0
}"
`;
Expand All @@ -25,18 +25,18 @@ const t5 = _template("<input>")

export function render(_ctx) {
const n13 = t5()
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
return n2
}, () => _createIf(() => (_ctx.orNot), () => {
} : (_ctx.orNot) ? () => {
const n5 = t1()
const n6 = t2()
return [n5, n6]
}, () => {
} : () => {
const n10 = t3()
const n11 = t4()
return [n10, n11]
}))
})
_renderEffect(() => _setText(n13, _ctx.text))
return [n0, n13]
}"
Expand All @@ -47,14 +47,14 @@ exports[`compiler: v-if > dedupe same template 1`] = `
const t0 = _template("<div>hello</div>")

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
return n2
})
const n3 = _createIf(() => (_ctx.ok), () => {
} : undefined)
const n3 = _createIf(() => (_ctx.ok) ? () => {
const n5 = t0()
return n5
})
} : undefined)
return [n0, n3]
}"
`;
Expand All @@ -66,13 +66,13 @@ const t1 = _template("hello")
const t2 = _template("<p></p>", true)

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
const n3 = t1()
const n4 = t2()
_renderEffect(() => _setText(n4, _ctx.msg))
return [n2, n3, n4]
})
} : undefined)
return n0
}"
`;
Expand All @@ -83,10 +83,10 @@ const t0 = _template("<div></div>")
const t1 = _template("<p></p>")

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
return n2
}, () => {
} : () => {
const n4 = t1()
return n4
})
Expand All @@ -101,16 +101,16 @@ const t1 = _template("<p></p>")
const t2 = _template("fine")

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
return n2
}, () => _createIf(() => (_ctx.orNot), () => {
} : (_ctx.orNot) ? () => {
const n4 = t1()
return n4
}, () => {
} : () => {
const n7 = t2()
return n7
}))
})
return n0
}"
`;
Expand All @@ -121,13 +121,13 @@ const t0 = _template("<div></div>")
const t1 = _template("<p></p>")

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.ok), () => {
const n0 = _createIf(() => (_ctx.ok) ? () => {
const n2 = t0()
return n2
}, () => _createIf(() => (_ctx.orNot), () => {
} : (_ctx.orNot) ? () => {
const n4 = t1()
return n4
}))
} : undefined)
return n0
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ exports[`compiler: v-once > with v-if 1`] = `
const t0 = _template("<div></div>", true)

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.expr), () => {
const n0 = _createIf(() => (_ctx.expr) ? () => {
const n2 = t0()
return n2
}, null, true)
} : undefined, true)
return n0
}"
`;
Expand All @@ -92,10 +92,10 @@ const t0 = _template("<div></div>")
const t1 = _template("<p></p>")

export function render(_ctx) {
const n0 = _createIf(() => (_ctx.expr), () => {
const n0 = _createIf(() => (_ctx.expr) ? () => {
const n2 = t0()
return n2
}, () => {
} : () => {
const n4 = t1()
return n4
}, true)
Expand Down
32 changes: 18 additions & 14 deletions packages/compiler-vapor/src/generators/if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function genIf(
const { condition, positive, negative, once } = oper
const [frag, push] = buildCodeFragment()

const conditionExpr: CodeFragment[] = [
'() => (',
const codes: CodeFragment[] = [
isNested ? '(' : '() => (',
...genExpression(condition, context),
')',
]
Expand All @@ -23,23 +23,27 @@ export function genIf(
let negativeArg: false | CodeFragment[] = false

if (negative) {
positiveArg.unshift(' ? ')
negativeArg = [' : ']
if (negative.type === IRNodeTypes.BLOCK) {
negativeArg = genBlock(negative, context)
negativeArg.push(...genBlock(negative, context))
} else {
negativeArg = ['() => ', ...genIf(negative!, context, true)]
negativeArg.push(...genIf(negative!, context, true))
}
} else {
positiveArg.unshift(' ? ')
positiveArg.push(' : undefined')
}

if (!isNested) push(NEWLINE, `const n${oper.id} = `)
push(
...genCall(
helper('createIf'),
conditionExpr,
positiveArg,
negativeArg,
once && 'true',
),
)
codes.push(...positiveArg)
if (negativeArg) codes.push(...negativeArg)

if (isNested) {
push(...codes)
} else {
push(NEWLINE, `const n${oper.id} = `)
push(...genCall(helper('createIf'), codes, once && 'true'))
}

return frag
}
29 changes: 14 additions & 15 deletions packages/runtime-vapor/__tests__/apiLifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ describe('api: lifecycle hooks', () => {
const { render, host } = define({
setup() {
// @ts-expect-error
const n0 = createIf(
() => toggle.value,
() => createComponent(Child),
const n0 = createIf(() =>
toggle.value ? () => createComponent(Child) : undefined,
)
return n0
},
Expand Down Expand Up @@ -172,9 +171,8 @@ describe('api: lifecycle hooks', () => {
const { render, host } = define({
setup() {
// @ts-expect-error
const n0 = createIf(
() => toggle.value,
() => createComponent(Child),
const n0 = createIf(() =>
toggle.value ? () => createComponent(Child) : undefined,
)
return n0
},
Expand Down Expand Up @@ -206,9 +204,8 @@ describe('api: lifecycle hooks', () => {
const { render, host } = define({
setup() {
// @ts-expect-error
const n0 = createIf(
() => toggle.value,
() => createComponent(Child),
const n0 = createIf(() =>
toggle.value ? () => createComponent(Child) : undefined,
)
return n0
},
Expand Down Expand Up @@ -249,9 +246,10 @@ describe('api: lifecycle hooks', () => {
onUnmounted(() => calls.push('onUnmounted'))

// @ts-expect-error
const n0 = createIf(
() => toggle.value,
() => createComponent(Mid, { count: () => count.value }),
const n0 = createIf(() =>
toggle.value
? () => createComponent(Mid, { count: () => count.value })
: undefined,
)
return n0
},
Expand Down Expand Up @@ -428,9 +426,10 @@ describe('api: lifecycle hooks', () => {
const { render } = define({
setup() {
// @ts-expect-error
return createIf(
() => toggle.value,
() => [createComponent(Child), createComponent(Child)],
return createIf(() =>
toggle.value
? () => [createComponent(Child), createComponent(Child)]
: undefined,
)
},
})
Expand Down
11 changes: 6 additions & 5 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,12 @@ describe('component: slots', () => {
setup() {
return createComponent(Child, null, {
default: () => {
return createIf(
() => toggle.value,
() => {
return document.createTextNode('content')
},
return createIf(() =>
toggle.value
? () => {
return document.createTextNode('content')
}
: () => [],
)
},
})
Expand Down
Loading
Loading