From: Evan You Date: Tue, 11 Apr 2023 07:35:05 +0000 (+0800) Subject: refactor(compiler-sfc): split all macros X-Git-Tag: v3.3.0-alpha.10~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3da1bb36b1ec2470e3816c9680bd2c9d7f0774eb;p=thirdparty%2Fvuejs%2Fcore.git refactor(compiler-sfc): split all macros --- diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 3fb435223b..8693fb29bf 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