refactor(compiler-sfc): always parse explicit script setup signature

This commit is contained in:
Evan You 2020-10-30 12:03:14 -04:00
parent 941b645d58
commit ed2eb81317

View File

@ -366,10 +366,8 @@ export function compileScript(
// record declared types for runtime props type generation // record declared types for runtime props type generation
const declaredTypes: Record<string, string[]> = {} const declaredTypes: Record<string, string[]> = {}
if (isTS && hasExplicitSignature) { // <script setup="xxx">
// <script setup="xxx" lang="ts"> if (hasExplicitSignature) {
// parse the signature to extract the props/emit variables the user wants
// we need them to find corresponding type declarations.
let signatureAST let signatureAST
try { try {
signatureAST = _parse(`(${setupValue})=>{}`, { plugins }).program.body[0] signatureAST = _parse(`(${setupValue})=>{}`, { plugins }).program.body[0]
@ -382,6 +380,11 @@ export function compileScript(
)}` )}`
) )
} }
if (isTS) {
// <script setup="xxx" lang="ts">
// parse the signature to extract the props/emit variables the user wants
// we need them to find corresponding type declarations.
const params = ((signatureAST as ExpressionStatement) const params = ((signatureAST as ExpressionStatement)
.expression as ArrowFunctionExpression).params .expression as ArrowFunctionExpression).params
if (params[0] && params[0].type === 'Identifier') { if (params[0] && params[0].type === 'Identifier') {
@ -407,6 +410,7 @@ export function compileScript(
} }
} }
} }
}
// 3. parse <script setup> and walk over top level statements // 3. parse <script setup> and walk over top level statements
const scriptSetupAst = parse( const scriptSetupAst = parse(