Skip to content

Commit

Permalink
refactor: setDynamicProp(s) need return prev value
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Dec 6, 2024
1 parent e979308 commit 24fa426
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ exports[`compile > expression parsing > v-bind 1`] = `
"((_ctx) => {
const n0 = t0()
_setInheritAttrs(true)
let _key_value, _foo
_renderEffect(() => (_key_value !== key.value || _foo !== _unref(foo)) && _setDynamicProps(n0, [{ [(_key_value = key.value)+1]: (_foo = _unref(foo))[key.value+1]() }], true))
let _key_value, _foo, _key_value_foo
_renderEffect(() => (_key_value !== key.value || _foo !== _unref(foo)) && (_key_value_foo = _setDynamicProps(n0, _key_value_foo, [{ [key.value+1]: _unref(foo)[key.value+1]() }], true)))
return n0
})()"
`;
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-vapor/__tests__/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('compile', () => {
expect(code).matchSnapshot()
expect(code).contains('key.value+1')
expect(code).contains(
'(_key_value !== key.value || _foo !== _unref(foo)) && _setDynamicProps(n0, [{ [(_key_value = key.value)+1]: (_foo = _unref(foo))[key.value+1]() }], true)',
'(_key_value !== key.value || _foo !== _unref(foo)) && (_key_value_foo = _setDynamicProps(n0, _key_value_foo, [{ [key.value+1]: _unref(foo)[key.value+1]() }], true))',
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _obj
_renderEffect(() => _obj !== _ctx.obj && _setDynamicProps(n0, [(_obj = _ctx.obj)], true))
_renderEffect(() => _obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [_ctx.obj], true)))
return n0
}"
`;
Expand All @@ -370,7 +370,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _obj
_renderEffect(() => _obj !== _ctx.obj && _setDynamicProps(n0, [{ id: "foo" }, (_obj = _ctx.obj)], true))
_renderEffect(() => _obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [{ id: "foo" }, _ctx.obj], true)))
return n0
}"
`;
Expand All @@ -383,7 +383,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _obj
_renderEffect(() => _obj !== _ctx.obj && _setDynamicProps(n0, [(_obj = _ctx.obj), { id: "foo" }], true))
_renderEffect(() => _obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [_ctx.obj, { id: "foo" }], true)))
return n0
}"
`;
Expand All @@ -396,7 +396,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _obj
_renderEffect(() => _obj !== _ctx.obj && _setDynamicProps(n0, [{ id: "foo" }, (_obj = _ctx.obj), { class: "bar" }], true))
_renderEffect(() => _obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [{ id: "foo" }, _ctx.obj, { class: "bar" }], true)))
return n0
}"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["fooBar"])
let _id
_renderEffect(() => _id !== _ctx.id && _setDynamicProp(n0, "fooBar", _id, (_id = _ctx.id)))
_renderEffect(() => _id !== _ctx.id && (_id = _setDynamicProp(n0, "fooBar", _id, _ctx.id)))
return n0
}"
`;
Expand All @@ -99,8 +99,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _foo, _id
_renderEffect(() => (_foo !== _ctx.foo || _id !== _ctx.id) && _setDynamicProps(n0, [{ [_camelize((_foo = _ctx.foo))]: (_id = _ctx.id) }], true))
let _foo, _id, _foo_id
_renderEffect(() => (_foo !== _ctx.foo || _id !== _ctx.id) && (_foo_id = _setDynamicProps(n0, _foo_id, [{ [_camelize(_ctx.foo)]: _ctx.id }], true)))
return n0
}"
`;
Expand All @@ -113,7 +113,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["fooBar"])
let _fooBar
_renderEffect(() => _fooBar !== _ctx.fooBar && _setDynamicProp(n0, "fooBar", _fooBar, (_fooBar = _ctx.fooBar)))
_renderEffect(() => _fooBar !== _ctx.fooBar && (_fooBar = _setDynamicProp(n0, "fooBar", _fooBar, _ctx.fooBar)))
return n0
}"
`;
Expand Down Expand Up @@ -216,8 +216,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _fooBar, _id
_renderEffect(() => (_fooBar !== _ctx.fooBar || _id !== _ctx.id) && _setDynamicProps(n0, [{ ["." + (_fooBar = _ctx.fooBar)]: (_id = _ctx.id) }], true))
let _fooBar, _id, _fooBar_id
_renderEffect(() => (_fooBar !== _ctx.fooBar || _id !== _ctx.id) && (_fooBar_id = _setDynamicProps(n0, _fooBar_id, [{ ["." + _ctx.fooBar]: _ctx.id }], true)))
return n0
}"
`;
Expand Down Expand Up @@ -334,7 +334,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["value"])
let _foo
_renderEffect(() => _foo !== _ctx.foo && _setDynamicProp(n0, "value", _foo, (_foo = _ctx.foo)))
_renderEffect(() => _foo !== _ctx.foo && (_foo = _setDynamicProp(n0, "value", _foo, _ctx.foo)))
return n0
}"
`;
Expand Down Expand Up @@ -459,8 +459,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _id, _title
_renderEffect(() => (_id !== _ctx.id || _title !== _ctx.title) && _setDynamicProps(n0, [{ [(_id = _ctx.id)]: _ctx.id, [(_title = _ctx.title)]: _ctx.title }], true))
let _id, _title, _id_title
_renderEffect(() => (_id !== _ctx.id || _title !== _ctx.title) && (_id_title = _setDynamicProps(n0, _id_title, [{ [_ctx.id]: _ctx.id, [_ctx.title]: _ctx.title }], true)))
return n0
}"
`;
Expand All @@ -473,7 +473,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(true)
let _id
_renderEffect(() => _id !== _ctx.id && _setDynamicProps(n0, [{ [(_id = _ctx.id)]: _ctx.id, foo: "bar", checked: "" }], true))
_renderEffect(() => _id !== _ctx.id && (_id = _setDynamicProps(n0, _id, [{ [_ctx.id]: _ctx.id, foo: "bar", checked: "" }], true)))
return n0
}"
`;
Expand All @@ -486,7 +486,7 @@ export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["camel-case"])
let _camelCase
_renderEffect(() => _camelCase !== _ctx.camelCase && _setDynamicProp(n0, "camel-case", _camelCase, (_camelCase = _ctx.camelCase)))
_renderEffect(() => _camelCase !== _ctx.camelCase && (_camelCase = _setDynamicProp(n0, "camel-case", _camelCase, _ctx.camelCase)))
return n0
}"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function render(_ctx) {
_delegate(n0, "update:modelValue", () => $event => (_ctx.model = $event))
_setInheritAttrs(true)
let _obj
_renderEffect(() => _obj !== _ctx.obj && _setDynamicProps(n0, [(_obj = _ctx.obj)], true))
_renderEffect(() => _obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [_ctx.obj], true)))
return n0
}"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ describe('compiler: element transform', () => {
},
])
expect(code).contains(
'_obj !== _ctx.obj && _setDynamicProps(n0, [(_obj = _ctx.obj)], true)',
'_obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [_ctx.obj], true))',
)
})

Expand Down Expand Up @@ -627,7 +627,7 @@ describe('compiler: element transform', () => {
},
])
expect(code).contains(
'_obj !== _ctx.obj && _setDynamicProps(n0, [{ id: "foo" }, (_obj = _ctx.obj)], true)',
'_obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [{ id: "foo" }, _ctx.obj], true))',
)
})

Expand Down Expand Up @@ -655,7 +655,7 @@ describe('compiler: element transform', () => {
},
])
expect(code).contains(
'_obj !== _ctx.obj && _setDynamicProps(n0, [(_obj = _ctx.obj), { id: "foo" }], true)',
'_obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [_ctx.obj, { id: "foo" }], true))',
)
})

Expand Down Expand Up @@ -684,7 +684,7 @@ describe('compiler: element transform', () => {
},
])
expect(code).contains(
'_obj !== _ctx.obj && _setDynamicProps(n0, [{ id: "foo" }, (_obj = _ctx.obj), { class: "bar" }], true)',
'_obj !== _ctx.obj && (_obj = _setDynamicProps(n0, _obj, [{ id: "foo" }, _ctx.obj, { class: "bar" }], true))',
)
})

Expand Down
16 changes: 8 additions & 8 deletions packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('compiler v-bind', () => {
},
})
expect(code).contains(
'_camelCase !== _ctx.camelCase && _setDynamicProp(n0, "camel-case", _camelCase, (_camelCase = _ctx.camelCase))',
'_camelCase !== _ctx.camelCase && (_camelCase = _setDynamicProp(n0, "camel-case", _camelCase, _ctx.camelCase))',
)
})

Expand Down Expand Up @@ -177,7 +177,7 @@ describe('compiler v-bind', () => {
],
})
expect(code).contains(
'(_id !== _ctx.id || _title !== _ctx.title) && _setDynamicProps(n0, [{ [(_id = _ctx.id)]: _ctx.id, [(_title = _ctx.title)]: _ctx.title }], true)',
'(_id !== _ctx.id || _title !== _ctx.title) && (_id_title = _setDynamicProps(n0, _id_title, [{ [_ctx.id]: _ctx.id, [_ctx.title]: _ctx.title }], true))',
)
})

Expand Down Expand Up @@ -230,7 +230,7 @@ describe('compiler v-bind', () => {
],
})
expect(code).contains(
'_id !== _ctx.id && _setDynamicProps(n0, [{ [(_id = _ctx.id)]: _ctx.id, foo: "bar", checked: "" }], true)',
'_id !== _ctx.id && (_id = _setDynamicProps(n0, _id, [{ [_ctx.id]: _ctx.id, foo: "bar", checked: "" }], true))',
)
})

Expand Down Expand Up @@ -293,7 +293,7 @@ describe('compiler v-bind', () => {

expect(code).matchSnapshot()
expect(code).contains(
'_id !== _ctx.id && _setDynamicProp(n0, "fooBar", _id, (_id = _ctx.id))',
'_id !== _ctx.id && (_id = _setDynamicProp(n0, "fooBar", _id, _ctx.id))',
)
})

Expand All @@ -319,7 +319,7 @@ describe('compiler v-bind', () => {
})
expect(code).contains('renderEffect')
expect(code).contains(
'_fooBar !== _ctx.fooBar && _setDynamicProp(n0, "fooBar", _fooBar, (_fooBar = _ctx.fooBar))',
'_fooBar !== _ctx.fooBar && (_fooBar = _setDynamicProp(n0, "fooBar", _fooBar, _ctx.fooBar))',
)
})

Expand Down Expand Up @@ -351,7 +351,7 @@ describe('compiler v-bind', () => {
expect(code).matchSnapshot()
expect(code).contains('renderEffect')
expect(code).contains(
`(_foo !== _ctx.foo || _id !== _ctx.id) && _setDynamicProps(n0, [{ [_camelize((_foo = _ctx.foo))]: (_id = _ctx.id) }], true)`,
`(_foo !== _ctx.foo || _id !== _ctx.id) && (_foo_id = _setDynamicProps(n0, _foo_id, [{ [_camelize(_ctx.foo)]: _ctx.id }], true))`,
)
})

Expand Down Expand Up @@ -436,7 +436,7 @@ describe('compiler v-bind', () => {
})
expect(code).contains('renderEffect')
expect(code).contains(
`(_fooBar !== _ctx.fooBar || _id !== _ctx.id) && _setDynamicProps(n0, [{ ["." + (_fooBar = _ctx.fooBar)]: (_id = _ctx.id) }], true)`,
`(_fooBar !== _ctx.fooBar || _id !== _ctx.id) && (_fooBar_id = _setDynamicProps(n0, _fooBar_id, [{ ["." + _ctx.fooBar]: _ctx.id }], true))`,
)
})

Expand Down Expand Up @@ -788,7 +788,7 @@ describe('compiler v-bind', () => {
`)
expect(code).matchSnapshot()
expect(code).contains(
'_foo !== _ctx.foo && _setDynamicProp(n0, "value", _foo, (_foo = _ctx.foo))',
'_foo !== _ctx.foo && (_foo = _setDynamicProp(n0, "value", _foo, _ctx.foo))',
)
})

Expand Down
46 changes: 27 additions & 19 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
toHandlerKey,
} from '@vue/shared'

const helperNeedPrevValue = ['setStyle', 'setDynamicProp']
// those runtime helpers will return the prevValue
const helperNeedPrevValue = ['setStyle', 'setDynamicProp', 'setDynamicProps']

// only the static key prop will reach here
export function genSetProp(
Expand All @@ -51,37 +52,32 @@ export function genSetProp(
const { helperName, omitKey } = getRuntimeHelper(tag, key.content, modifier)
let propValue = genPropValue(values, context)

let prevValueName
if (shouldCacheRenderEffectDeps()) {
processValues(context, [propValue])

const needReturnValue = helperNeedPrevValue.includes(helperName)
processValues(context, [propValue], !needReturnValue)
const { declareNames } = processingRenderEffect!
// need prevValue parameter
if (declareNames.size > 0 && helperNeedPrevValue.includes(helperName)) {
const prevValueName = [...declareNames].join('')
if (declareNames.size > 0 && needReturnValue) {
prevValueName = [...declareNames].join('')
declareNames.add(prevValueName)
const needReCacheValue = declareNames.size > 1
propValue.unshift(
...[
`${prevValueName}, `, // prevValue parameter
needReCacheValue ? `(${prevValueName} = ` : undefined, // cache value to prevValue
],
)
needReCacheValue && propValue.push(')')
}
}

return [
NEWLINE,
...(prevValueName ? [`(`, `${prevValueName} = `] : []),
...genCall(
[vaporHelper(helperName), null],
`n${oper.element}`,
omitKey ? false : genExpression(key, context),
...(prevValueName ? [`${prevValueName}`] : []),
propValue,
// only `setClass` and `setStyle` need merge inherit attr
oper.root && (helperName === 'setClass' || helperName === 'setStyle')
? 'true'
: undefined,
),
...(prevValueName ? [`)`] : []),
]
}

Expand All @@ -90,7 +86,8 @@ export function genDynamicProps(
oper: SetDynamicPropsIRNode,
context: CodegenContext,
): CodeFragment[] {
const { vaporHelper, shouldCacheRenderEffectDeps } = context
const { vaporHelper, shouldCacheRenderEffectDeps, processingRenderEffect } =
context
const values = oper.props.map(props =>
Array.isArray(props)
? genLiteralObjectProps(props, context) // static and dynamic arg props
Expand All @@ -99,18 +96,27 @@ export function genDynamicProps(
: genExpression(props.value, context),
) // v-bind=""

let prevValueName
if (shouldCacheRenderEffectDeps()) {
processValues(context, values)
processValues(context, values, false)
const { declareNames } = processingRenderEffect!
if (declareNames.size > 0) {
prevValueName = [...declareNames].join('')
declareNames.add(prevValueName)
}
}

return [
NEWLINE,
...(prevValueName ? [`(`, `${prevValueName} = `] : []),
...genCall(
vaporHelper('setDynamicProps'),
`n${oper.element}`,
...(prevValueName ? [`${prevValueName}`] : []),
genMulti(DELIMITERS_ARRAY, ...values),
oper.root && 'true',
),
...(prevValueName ? [`)`] : []),
]
}

Expand Down Expand Up @@ -265,10 +271,11 @@ const getSpecialHelper = (
export function processValues(
context: CodegenContext,
values: CodeFragment[][],
needRewrite: boolean = true,
): string[] {
const allCheckExps: string[] = []
values.forEach(value => {
const checkExps = processValue(context, value)
const checkExps = processValue(context, value, needRewrite)
if (checkExps) allCheckExps.push(...checkExps, ' && ')
})

Expand All @@ -282,12 +289,13 @@ export function processValues(
function processValue(
context: CodegenContext,
values: CodeFragment[],
needRewrite: boolean = true,
): string[] | undefined {
const { processingRenderEffect, allRenderEffectSeenNames } = context
const { declareNames, rewrittenNames, earlyCheckExps, operations } =
processingRenderEffect!

const isMultiLine = operations.length > 1
const isSingleLine = operations.length === 1
for (const frag of values) {
if (!isArray(frag)) continue
// [code, newlineIndex, loc, name] -> [(_name = code), newlineIndex, loc, name]
Expand All @@ -307,7 +315,7 @@ function processValue(
declareNames.add(name)
earlyCheckExps.push(`${name} !== ${newName}`)

if (!isMultiLine) {
if (needRewrite && isSingleLine) {
// replace the original code fragment with the assignment expression
frag[0] = `(${name} = ${newName})`
}
Expand Down
Loading

0 comments on commit 24fa426

Please sign in to comment.