Skip to content

Commit b4216a1

Browse files
committed
refactor
1 parent ff80e6a commit b4216a1

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

packages/compiler-vapor/src/ir.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,25 @@ export interface ForIRNode extends BaseIRNode {
8383
export interface IRProp extends Omit<DirectiveTransformResult, 'value'> {
8484
values: SimpleExpressionNode[]
8585
}
86-
export interface IRDynamicProps {
86+
87+
export enum DynamicPropsKind {
88+
EXPRESSION, // v-bind="value"
89+
ATTRIBUTE, // v-bind:[foo]="value"
90+
}
91+
92+
export type IRPropsStatic = IRProp[]
93+
export interface IRPropsDynamicExpression {
94+
kind: DynamicPropsKind.EXPRESSION
8795
value: SimpleExpressionNode
8896
handler?: boolean
8997
}
90-
export type IRProps = IRProp[] | IRProp | IRDynamicProps
98+
export interface IRPropsDynamicAttribute extends IRProp {
99+
kind: DynamicPropsKind.ATTRIBUTE
100+
}
101+
export type IRProps =
102+
| IRPropsStatic
103+
| IRPropsDynamicAttribute
104+
| IRPropsDynamicExpression
91105

92106
export interface SetPropIRNode extends BaseIRNode {
93107
type: IRNodeTypes.SET_PROP

packages/compiler-vapor/src/transforms/transformElement.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import type {
2424
} from '../transform'
2525
import {
2626
DynamicFlag,
27+
DynamicPropsKind,
2728
IRNodeTypes,
2829
type IRProp,
2930
type IRProps,
31+
type IRPropsDynamicAttribute,
3032
type VaporDirectiveNode,
3133
} from '../ir'
3234
import { EMPTY_EXPRESSION } from './utils'
@@ -205,7 +207,10 @@ function buildProps(
205207
if (prop.exp) {
206208
dynamicExpr.push(prop.exp)
207209
pushMergeArg()
208-
dynamicArgs.push({ value: prop.exp })
210+
dynamicArgs.push({
211+
kind: DynamicPropsKind.EXPRESSION,
212+
value: prop.exp,
213+
})
209214
} else {
210215
context.options.onError(
211216
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, prop.loc),
@@ -218,7 +223,11 @@ function buildProps(
218223
if (isComponent) {
219224
dynamicExpr.push(prop.exp)
220225
pushMergeArg()
221-
dynamicArgs.push({ value: prop.exp, handler: true })
226+
dynamicArgs.push({
227+
kind: DynamicPropsKind.EXPRESSION,
228+
value: prop.exp,
229+
handler: true,
230+
})
222231
} else {
223232
context.registerEffect(
224233
[prop.exp],
@@ -245,7 +254,11 @@ function buildProps(
245254
if (isComponent && !result.key.isStatic) {
246255
// v-bind:[name]="value" or v-on:[name]="value"
247256
pushMergeArg()
248-
dynamicArgs.push(normalizeIRProp(result))
257+
dynamicArgs.push(
258+
extend(resolveDirectiveResult(result), {
259+
kind: DynamicPropsKind.ATTRIBUTE,
260+
}) as IRPropsDynamicAttribute,
261+
)
249262
} else {
250263
// other static props
251264
results.push(result)
@@ -304,7 +317,7 @@ function dedupeProperties(results: DirectiveTransformResult[]): IRProp[] {
304317
const deduped: IRProp[] = []
305318

306319
for (const result of results) {
307-
const prop = normalizeIRProp(result)
320+
const prop = resolveDirectiveResult(result)
308321
// dynamic keys are always allowed
309322
if (!prop.key.isStatic) {
310323
deduped.push(prop)
@@ -314,7 +327,7 @@ function dedupeProperties(results: DirectiveTransformResult[]): IRProp[] {
314327
const existing = knownProps.get(name)
315328
if (existing) {
316329
if (name === 'style' || name === 'class') {
317-
mergeAsArray(existing, prop)
330+
mergePropValues(existing, prop)
318331
}
319332
// unexpected duplicate, should have emitted error during parse
320333
} else {
@@ -325,11 +338,14 @@ function dedupeProperties(results: DirectiveTransformResult[]): IRProp[] {
325338
return deduped
326339
}
327340

328-
function normalizeIRProp(prop: DirectiveTransformResult): IRProp {
329-
return extend({}, prop, { value: undefined, values: [prop.value] })
341+
function resolveDirectiveResult(prop: DirectiveTransformResult): IRProp {
342+
return extend({}, prop, {
343+
value: undefined,
344+
values: [prop.value],
345+
})
330346
}
331347

332-
function mergeAsArray(existing: IRProp, incoming: IRProp) {
348+
function mergePropValues(existing: IRProp, incoming: IRProp) {
333349
const newValues = incoming.values
334350
existing.values.push(...newValues)
335351
}

0 commit comments

Comments
 (0)