From c8af08d7ede410132a2eb699f688ae58b3269dd0 Mon Sep 17 00:00:00 2001 From: daiwei Date: Mon, 6 Sep 2021 09:01:39 +0800 Subject: [PATCH] chore: improve code --- .../__snapshots__/compileScript.spec.ts.snap | 15 ++++++++------- .../compiler-sfc/__tests__/compileScript.spec.ts | 16 +++++++++------- packages/compiler-sfc/src/compileScript.ts | 13 +++++++++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 009a16a8851..befdf2f2797 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -913,12 +913,13 @@ return { } exports[`SFC compile `) assertCode(content) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index e57c37bfa74..fe7f2222d89 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -440,6 +440,12 @@ export function compileScript( return true } + function getAstBody(): Statement[] { + return scriptAst + ? [...scriptSetupAst.body, ...scriptAst.body] + : scriptSetupAst.body + } + function resolveExtendsType( node: Node, qualifier: (node: Node) => boolean, @@ -451,7 +457,8 @@ export function compileScript( extend.type === 'TSExpressionWithTypeArguments' && extend.expression.type === 'Identifier' ) { - for (const node of scriptSetupAst.body) { + const body = getAstBody() + for (const node of body) { const qualified = isQualifiedType( node, qualifier, @@ -521,9 +528,7 @@ export function compileScript( node.typeName.type === 'Identifier' ) { const refName = node.typeName.name - const body = scriptAst - ? [...scriptSetupAst.body, ...scriptAst.body] - : scriptSetupAst.body + const body = getAstBody() for (const node of body) { let qualified = isQualifiedType( node,