From d079ef1b0a3e20a0ddb3d7eae26d2f7209d0e2c8 Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 18 Sep 2024 16:45:14 +0800 Subject: [PATCH] fix(compile-core): fix v-model with newlines edge case --- .../compiler-core/src/transforms/vModel.ts | 2 +- .../__snapshots__/compileScript.spec.ts.snap | 23 +++++++++++++++++++ .../__tests__/compileScript.spec.ts | 17 ++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/src/transforms/vModel.ts b/packages/compiler-core/src/transforms/vModel.ts index f168c181803..598c1ea4387 100644 --- a/packages/compiler-core/src/transforms/vModel.ts +++ b/packages/compiler-core/src/transforms/vModel.ts @@ -31,7 +31,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => { // we assume v-model directives are always parsed // (not artificially created by a transform) - const rawExp = exp.loc.source + const rawExp = exp.loc.source.trim() const expString = exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : rawExp diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index bf40b0529d4..7fcc1d39506 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1084,6 +1084,29 @@ return (_ctx, _cache) => { }" `; +exports[`SFC compile + + `, + { inlineTemplate: true }, + ) + expect(content).toMatch(`_isRef(count) ? (count).value = $event : null`) + assertCode(content) + }) + test('v-model should not generate ref assignment code for non-setup bindings', () => { const { content } = compile( `