From 84adc78465f3bbb7b8a13a5ed1a72af265460180 Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 4 Dec 2024 13:32:30 +0800 Subject: [PATCH] wip: fix incorrect condition --- .../__tests__/__snapshots__/compile.spec.ts.snap | 2 +- packages/compiler-vapor/__tests__/compile.spec.ts | 2 +- .../transforms/__snapshots__/vBind.spec.ts.snap | 6 +++--- .../__tests__/transforms/vBind.spec.ts | 6 +++--- packages/compiler-vapor/src/generators/operation.ts | 12 ++++++++++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap b/packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap index 7092652ac..2a251895a 100644 --- a/packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap +++ b/packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap @@ -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 })()" `; diff --git a/packages/compiler-vapor/__tests__/compile.spec.ts b/packages/compiler-vapor/__tests__/compile.spec.ts index e1596b64a..f2abf48f9 100644 --- a/packages/compiler-vapor/__tests__/compile.spec.ts +++ b/packages/compiler-vapor/__tests__/compile.spec.ts @@ -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)', ) }) diff --git a/packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap b/packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap index 944fe34d5..a56a511bb 100644 --- a/packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap +++ b/packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap @@ -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 }" `; @@ -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 }" `; @@ -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 }" `; diff --git a/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts b/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts index c5b33cded..f69fc34f8 100644 --- a/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts @@ -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)', ) }) @@ -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)`, ) }) @@ -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)`, ) }) diff --git a/packages/compiler-vapor/src/generators/operation.ts b/packages/compiler-vapor/src/generators/operation.ts index 4e4f0fe3b..fdef9b26d 100644 --- a/packages/compiler-vapor/src/generators/operation.ts +++ b/packages/compiler-vapor/src/generators/operation.ts @@ -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), ')') }