diff --git a/packages/compiler-core/src/options.ts b/packages/compiler-core/src/options.ts index 37641a45..05c3d2cd 100644 --- a/packages/compiler-core/src/options.ts +++ b/packages/compiler-core/src/options.ts @@ -62,7 +62,7 @@ export type HoistTransform = ( ) => void export interface BindingMetadata { - [key: string]: 'data' | 'props' | 'setup' | 'options' | 'component-import' + [key: string]: 'data' | 'props' | 'setup' | 'options' | 'setup-raw' } interface SharedTransformCodegenOptions { diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 1025e602..937e3a71 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -270,9 +270,9 @@ export function resolveComponentType( `${context.helperString(UNREF)}(${tagFromSetup})` : `$setup[${JSON.stringify(tagFromSetup)}]` } - const tagFromImport = checkType('component-import') + const tagFromImport = checkType('setup-raw') if (tagFromImport) { - // imports can be used as-is + // raw setup bindings (e.g. imports) can be used as-is return tagFromImport } } diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index 924d217a..87c6c65b 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -105,7 +105,11 @@ export function processExpression( // setup inline mode if (type === 'setup') { return `${context.helperString(UNREF)}(${raw})` - } else if (type === 'component-import') { + } else if (type === 'props') { + // use __props which is generated by compileScript so in ts mode + // it gets correct type + return `__props.${raw}` + } else if (type === 'setup-raw') { return raw } } diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index ebb60a29..4306fa37 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -134,13 +134,13 @@ export function compileScript( source: string } > = Object.create(null) - const setupBindings: Record = Object.create(null) - const refBindings: Record = Object.create(null) + const setupBindings: Record = Object.create(null) + const refBindings: Record = Object.create(null) const refIdentifiers: Set = new Set() const enableRefSugar = options.refSugar !== false let defaultExport: Node | undefined let setupContextExp: string | undefined - let setupContextArg: Node | undefined + let setupContextArg: ObjectExpression | undefined let setupContextType: TSTypeLiteral | undefined let hasAwait = false @@ -222,7 +222,7 @@ export function compileScript( if (id.name[0] === '$') { error(`ref variable identifiers cannot start with $.`, id) } - refBindings[id.name] = setupBindings[id.name] = true + refBindings[id.name] = setupBindings[id.name] = 'var' refIdentifiers.add(id) } @@ -513,10 +513,25 @@ export function compileScript( decl.id.start!, decl.id.end! ) - setupContextArg = decl.init.arguments[0] + const optsArg = decl.init.arguments[0] + if (optsArg.type === 'ObjectExpression') { + setupContextArg = optsArg + } else { + error( + `${CTX_FN_NAME}() argument must be an object literal.`, + optsArg + ) + } // useSetupContext() has type parameters - infer runtime types from it if (decl.init.typeParameters) { + if (setupContextArg) { + error( + `${CTX_FN_NAME}() cannot accept both type and non-type arguments ` + + `at the same time. Use one or the other.`, + decl.init + ) + } const typeArg = decl.init.typeParameters.params[0] if (typeArg.type === 'TSTypeLiteral') { setupContextType = typeArg @@ -685,7 +700,7 @@ export function compileScript( // 7. finalize setup argument signature. let args = setupContextExp ? `__props, ${setupContextExp}` : `` - if (isTS) { + if (setupContextExp && setupContextType) { if (slotsType === 'Slots') { helperImports.add('Slots') } @@ -695,32 +710,11 @@ export function compileScript( slots: ${slotsType}, attrs: ${attrsType} }` - // if (hasExplicitSignature) { - // // inject types to user signature - // args = setupValue as string - // const ss = new MagicString(args) - // if (propsASTNode) { - // // compensate for () wraper offset - // ss.appendRight(propsASTNode.end! - 1, `: ${propsType}`) - // } - // if (setupCtxASTNode) { - // ss.appendRight(setupCtxASTNode.end! - 1!, `: ${ctxType}`) - // } - // args = ss.toString() - // } } - // 8. wrap setup code with function. - // export the content of