From 3da1bb36b1ec2470e3816c9680bd2c9d7f0774eb Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 11 Apr 2023 15:35:05 +0800 Subject: [PATCH] refactor(compiler-sfc): split all macros --- packages/compiler-sfc/src/compileScript.ts | 208 ++---------------- packages/compiler-sfc/src/script/context.ts | 7 +- .../compiler-sfc/src/script/defineExpose.ts | 19 ++ .../compiler-sfc/src/script/defineOptions.ts | 73 ++++++ .../compiler-sfc/src/script/defineSlots.ts | 33 +++ .../compiler-sfc/src/script/resolveType.ts | 5 - .../compiler-sfc/src/script/topLevelAwait.ts | 69 ++++++ 7 files changed, 222 insertions(+), 192 deletions(-) create mode 100644 packages/compiler-sfc/src/script/defineExpose.ts create mode 100644 packages/compiler-sfc/src/script/defineOptions.ts create mode 100644 packages/compiler-sfc/src/script/defineSlots.ts create mode 100644 packages/compiler-sfc/src/script/topLevelAwait.ts diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 3fb435223..8693fb29b 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -18,8 +18,6 @@ import { ExportSpecifier, Statement, CallExpression, - AwaitExpression, - LVal, TSEnumDeclaration } from '@babel/types' import { walk } from 'estree-walker' @@ -46,16 +44,15 @@ import { genRuntimeEmits, DEFINE_EMITS } from './script/defineEmits' +import { DEFINE_EXPOSE, processDefineExpose } from './script/defineExpose' +import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions' +import { processDefineSlots } from './script/defineSlots' import { DEFINE_MODEL, processDefineModel } from './script/defineModel' import { isLiteralNode, unwrapTSNode, isCallOf } from './script/utils' import { inferRuntimeType } from './script/resolveType' import { analyzeScriptBindings } from './script/analyzeScriptBindings' import { isImportUsed } from './script/importUsageCheck' - -// Special compiler macros -const DEFINE_EXPOSE = 'defineExpose' -const DEFINE_OPTIONS = 'defineOptions' -const DEFINE_SLOTS = 'defineSlots' +import { processAwait } from './script/topLevelAwait' export interface SFCScriptCompileOptions { /** @@ -250,30 +247,15 @@ export function compileScript( const setupBindings: Record = Object.create(null) let defaultExport: Node | undefined - let optionsRuntimeDecl: Node | undefined let hasAwait = false let hasInlinedSsrRenderFn = false - // magic-string state - const startOffset = scriptSetup.loc.start.offset - const endOffset = scriptSetup.loc.end.offset + // string offsets + const startOffset = ctx.startOffset! + const endOffset = ctx.endOffset! const scriptStartOffset = script && script.loc.start.offset const scriptEndOffset = script && script.loc.end.offset - function error( - msg: string, - node: Node, - end: number = node.end! + startOffset - ): never { - throw new Error( - `[@vue/compiler-sfc] ${msg}\n\n${sfc.filename}\n${generateCodeFrame( - source, - node.start! + startOffset, - end - )}` - ) - } - function hoistNode(node: Statement) { const start = node.start! + startOffset let end = node.end! + startOffset @@ -324,108 +306,12 @@ export function compileScript( } } - function processDefineSlots(node: Node, declId?: LVal): boolean { - if (!isCallOf(node, DEFINE_SLOTS)) { - return false - } - if (ctx.hasDefineSlotsCall) { - error(`duplicate ${DEFINE_SLOTS}() call`, node) - } - ctx.hasDefineSlotsCall = true - - if (node.arguments.length > 0) { - error(`${DEFINE_SLOTS}() cannot accept arguments`, node) - } - - if (declId) { - ctx.s.overwrite( - startOffset + node.start!, - startOffset + node.end!, - `${ctx.helper('useSlots')}()` - ) - } - - return true - } - - function processDefineOptions(node: Node): boolean { - if (!isCallOf(node, DEFINE_OPTIONS)) { - return false - } - if (ctx.hasDefineOptionsCall) { - error(`duplicate ${DEFINE_OPTIONS}() call`, node) - } - if (node.typeParameters) { - error(`${DEFINE_OPTIONS}() cannot accept type arguments`, node) - } - if (!node.arguments[0]) return true - - ctx.hasDefineOptionsCall = true - optionsRuntimeDecl = unwrapTSNode(node.arguments[0]) - - let propsOption = undefined - let emitsOption = undefined - let exposeOption = undefined - let slotsOption = undefined - if (optionsRuntimeDecl.type === 'ObjectExpression') { - for (const prop of optionsRuntimeDecl.properties) { - if ( - (prop.type === 'ObjectProperty' || prop.type === 'ObjectMethod') && - prop.key.type === 'Identifier' - ) { - if (prop.key.name === 'props') propsOption = prop - if (prop.key.name === 'emits') emitsOption = prop - if (prop.key.name === 'expose') exposeOption = prop - if (prop.key.name === 'slots') slotsOption = prop - } - } - } - - if (propsOption) { - error( - `${DEFINE_OPTIONS}() cannot be used to declare props. Use ${DEFINE_PROPS}() instead.`, - propsOption - ) - } - if (emitsOption) { - error( - `${DEFINE_OPTIONS}() cannot be used to declare emits. Use ${DEFINE_EMITS}() instead.`, - emitsOption - ) - } - if (exposeOption) { - error( - `${DEFINE_OPTIONS}() cannot be used to declare expose. Use ${DEFINE_EXPOSE}() instead.`, - exposeOption - ) - } - if (slotsOption) { - error( - `${DEFINE_OPTIONS}() cannot be used to declare slots. Use ${DEFINE_SLOTS}() instead.`, - slotsOption - ) - } - - return true - } - - function processDefineExpose(node: Node): boolean { - if (isCallOf(node, DEFINE_EXPOSE)) { - if (ctx.hasDefineExposeCall) { - error(`duplicate ${DEFINE_EXPOSE}() call`, node) - } - ctx.hasDefineExposeCall = true - return true - } - return false - } - function checkInvalidScopeReference(node: Node | undefined, method: string) { if (!node) return walkIdentifiers(node, id => { const binding = setupBindings[id.name] if (binding && binding !== BindingTypes.LITERAL_CONST) { - error( + ctx.error( `\`${method}()\` in