Skip to content

Commit

Permalink
wip: fix incorrect condition
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Dec 4, 2024
1 parent c1a3688 commit 84adc78
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ exports[`compile > expression parsing > v-bind 1`] = `
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))
_renderEffect(() => (_key_value !== key.value || _foo !== _unref(foo)) && _setDynamicProps(n0, [{ [(_key_value = key.value)+1]: (_foo = _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)) && _setDynamicProps(n0, [{ [(_key_value = key.value)+1]: (_foo = _unref(foo))[key.value+1]() }], true)',
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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))
_renderEffect(() => (_foo !== _ctx.foo || _id !== _ctx.id) && _setDynamicProps(n0, [{ [_camelize((_foo = _ctx.foo))]: (_id = _ctx.id) }], true))
return n0
}"
`;
Expand Down Expand Up @@ -217,7 +217,7 @@ 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))
_renderEffect(() => (_fooBar !== _ctx.fooBar || _id !== _ctx.id) && _setDynamicProps(n0, [{ ["." + (_fooBar = _ctx.fooBar)]: (_id = _ctx.id) }], true))
return n0
}"
`;
Expand Down Expand Up @@ -469,7 +469,7 @@ 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))
_renderEffect(() => (_id !== _ctx.id || _title !== _ctx.title) && _setDynamicProps(n0, [{ [(_id = _ctx.id)]: _ctx.id, [(_title = _ctx.title)]: _ctx.title }], true))
return n0
}"
`;
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
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) && _setDynamicProps(n0, [{ [(_id = _ctx.id)]: _ctx.id, [(_title = _ctx.title)]: _ctx.title }], true)',
)
})

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) && _setDynamicProps(n0, [{ [_camelize((_foo = _ctx.foo))]: (_id = _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) && _setDynamicProps(n0, [{ ["." + (_fooBar = _ctx.fooBar)]: (_id = _ctx.id) }], true)`,
)
})

Expand Down
12 changes: 10 additions & 2 deletions packages/compiler-vapor/src/generators/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,17 @@ export function genEffect(
'})',
)
} else {
// single line early return condition: _foo !== _ctx.foo && _bar !== _ctx.bar &&
// single line early return condition: _foo !== _ctx.foo || _bar !== _ctx.bar &&
const multiple = conditions.length > 1
const condition: CodeFragment[] =
conditions.length > 0 ? [...conditions.join(' && '), ' && '] : []
conditions.length > 0
? [
multiple ? `(` : undefined,
...conditions.join(' || '),
multiple ? `)` : undefined,
' && ',
]
: []
push(...condition, ...operationsExps.filter(frag => frag !== NEWLINE), ')')
}

Expand Down

0 comments on commit 84adc78

Please sign in to comment.