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,26 +380,32 @@ export function compileScript(
)}` )}`
) )
} }
const params = ((signatureAST as ExpressionStatement)
.expression as ArrowFunctionExpression).params if (isTS) {
if (params[0] && params[0].type === 'Identifier') { // <script setup="xxx" lang="ts">
propsASTNode = params[0] // parse the signature to extract the props/emit variables the user wants
propsVar = propsASTNode.name // we need them to find corresponding type declarations.
} const params = ((signatureAST as ExpressionStatement)
if (params[1] && params[1].type === 'ObjectPattern') { .expression as ArrowFunctionExpression).params
setupCtxASTNode = params[1] if (params[0] && params[0].type === 'Identifier') {
for (const p of params[1].properties) { propsASTNode = params[0]
if ( propsVar = propsASTNode.name
p.type === 'ObjectProperty' && }
p.key.type === 'Identifier' && if (params[1] && params[1].type === 'ObjectPattern') {
p.value.type === 'Identifier' setupCtxASTNode = params[1]
) { for (const p of params[1].properties) {
if (p.key.name === 'emit') { if (
emitVar = p.value.name p.type === 'ObjectProperty' &&
} else if (p.key.name === 'slots') { p.key.type === 'Identifier' &&
slotsVar = p.value.name p.value.type === 'Identifier'
} else if (p.key.name === 'attrs') { ) {
attrsVar = p.value.name if (p.key.name === 'emit') {
emitVar = p.value.name
} else if (p.key.name === 'slots') {
slotsVar = p.value.name
} else if (p.key.name === 'attrs') {
attrsVar = p.value.name
}
} }
} }
} }