Skip to content

Commit

Permalink
refactor(compiler-sfc): always parse explicit script setup signature
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 30, 2020
1 parent 941b645 commit ed2eb81
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,8 @@ export function compileScript(
// record declared types for runtime props type generation
const declaredTypes: Record<string, string[]> = {}

if (isTS && hasExplicitSignature) {
// <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.
// <script setup="xxx">
if (hasExplicitSignature) {
let signatureAST
try {
signatureAST = _parse(`(${setupValue})=>{}`, { plugins }).program.body[0]
Expand All @@ -382,26 +380,32 @@ export function compileScript(
)}`
)
}
const params = ((signatureAST as ExpressionStatement)
.expression as ArrowFunctionExpression).params
if (params[0] && params[0].type === 'Identifier') {
propsASTNode = params[0]
propsVar = propsASTNode.name
}
if (params[1] && params[1].type === 'ObjectPattern') {
setupCtxASTNode = params[1]
for (const p of params[1].properties) {
if (
p.type === 'ObjectProperty' &&
p.key.type === 'Identifier' &&
p.value.type === 'Identifier'
) {
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

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)
.expression as ArrowFunctionExpression).params
if (params[0] && params[0].type === 'Identifier') {
propsASTNode = params[0]
propsVar = propsASTNode.name
}
if (params[1] && params[1].type === 'ObjectPattern') {
setupCtxASTNode = params[1]
for (const p of params[1].properties) {
if (
p.type === 'ObjectProperty' &&
p.key.type === 'Identifier' &&
p.value.type === 'Identifier'
) {
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
}
}
}
}
Expand Down

0 comments on commit ed2eb81

Please sign in to comment.