From: Evan You Date: Fri, 6 Aug 2021 16:55:48 +0000 (-0400) Subject: fix(compiler-sfc): fix ref sugar rewrite for identifiers in ts casting expressions X-Git-Tag: v3.2.0-beta.8~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=865b84bfe81622626152e9c571cd26f30ba37bd5;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-sfc): fix ref sugar rewrite for identifiers in ts casting expressions fix #4254 --- diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap index f497091935..6dc4229ce2 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap @@ -92,6 +92,23 @@ return { n, a, b, c } }" `; +exports[``, + { + refSugar: true + } + ) + assertCode(content) + expect(content).toMatch('console.log(n.value!)') + expect(content).toMatch('console.log(n.value as number)') + }) + describe('errors', () => { test('non-let $ref declaration', () => { expect(() => diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index a3f86a3dd9..b7f940fc89 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1781,7 +1781,12 @@ export function walkIdentifiers( ;(walk as any)(root, { enter(node: Node & { scopeIds?: Set }, parent: Node | undefined) { parent && parentStack.push(parent) - if (node.type.startsWith('TS')) { + if ( + parent && + parent.type.startsWith('TS') && + parent.type !== 'TSAsExpression' && + parent.type !== 'TSNonNullExpression' + ) { return this.skip() } if (onNode && onNode(node, parent!, parentStack) === false) {