diff --git a/packages/compiler-core/src/errors.ts b/packages/compiler-core/src/errors.ts index 36ab783edbe..57eee97a591 100644 --- a/packages/compiler-core/src/errors.ts +++ b/packages/compiler-core/src/errors.ts @@ -17,7 +17,7 @@ export function defaultOnWarn(msg: CompilerError) { __DEV__ && console.warn(`[Vue warn] ${msg.message}`) } -type InferCompilerError = T extends ErrorCodes +export type InferCompilerError = T extends ErrorCodes ? CoreCompilerError : CompilerError diff --git a/packages/compiler-core/src/index.ts b/packages/compiler-core/src/index.ts index 4898a181dfc..71007f25080 100644 --- a/packages/compiler-core/src/index.ts +++ b/packages/compiler-core/src/index.ts @@ -25,8 +25,11 @@ export { generate, type CodegenContext, type CodegenResult } from './codegen' export { ErrorCodes, createCompilerError, + defaultOnError, + defaultOnWarn, type CoreCompilerError, - type CompilerError + type CompilerError, + type InferCompilerError } from './errors' export * from './ast' diff --git a/packages/compiler-vapor/__tests__/compile.test.ts b/packages/compiler-vapor/__tests__/compile.test.ts index acb31e3e42f..a70442eed98 100644 --- a/packages/compiler-vapor/__tests__/compile.test.ts +++ b/packages/compiler-vapor/__tests__/compile.test.ts @@ -71,7 +71,7 @@ describe('comile', () => { await compile(`
`, { onError }) expect(onError.mock.calls[0][0]).toMatchObject({ - code: ErrorCodes.X_V_VAPOR_BIND_NO_EXPRESSION, + code: ErrorCodes.VAPOR_BIND_NO_EXPRESSION, loc: { start: { line: 1, @@ -100,7 +100,7 @@ describe('comile', () => { const onError = vi.fn() await compile(`
`, { onError }) expect(onError.mock.calls[0][0]).toMatchObject({ - code: ErrorCodes.X_V_VAPOR_ON_NO_EXPRESSION, + code: ErrorCodes.VAPOR_ON_NO_EXPRESSION, loc: { start: { line: 1, diff --git a/packages/compiler-vapor/src/errors.ts b/packages/compiler-vapor/src/errors.ts index 46a94704843..37fa284e24b 100644 --- a/packages/compiler-vapor/src/errors.ts +++ b/packages/compiler-vapor/src/errors.ts @@ -1,41 +1,11 @@ -import { SourceLocation } from '@vue/compiler-dom' - -export interface CompilerError extends SyntaxError { - code: number | string - loc?: SourceLocation -} - -export interface CoreCompilerError extends CompilerError { - code: ErrorCodes -} - -export function defaultOnError(error: CompilerError) { - throw error -} - -export function defaultOnWarn(msg: CompilerError) { - __DEV__ && console.warn(`[Vue warn] ${msg.message}`) -} - -type InferCompilerError = T extends ErrorCodes - ? CoreCompilerError - : CompilerError - -export function createCompilerError( - code: T, - loc?: SourceLocation, - messages?: { [code: number]: string }, - additionalMessage?: string, -): InferCompilerError { - const msg = - __DEV__ || !__BROWSER__ - ? (messages || errorMessages)[code] + (additionalMessage || ``) - : code - const error = new SyntaxError(String(msg)) as InferCompilerError - error.code = code - error.loc = loc - return error -} +export { + createCompilerError, + defaultOnError, + defaultOnWarn, + type CoreCompilerError, + type CompilerError, + type InferCompilerError, +} from '@vue/compiler-dom' export const enum ErrorCodes { // transform errors @@ -45,6 +15,6 @@ export const enum ErrorCodes { export const errorMessages: Record = { // transform errors - [ErrorCodes.X_V_VAPOR_BIND_NO_EXPRESSION]: `v-bind is missing expression.`, - [ErrorCodes.X_V_VAPOR_ON_NO_EXPRESSION]: `v-on is missing expression.`, + [ErrorCodes.VAPOR_BIND_NO_EXPRESSION]: `v-bind is missing expression.`, + [ErrorCodes.VAPOR_ON_NO_EXPRESSION]: `v-on is missing expression.`, } diff --git a/packages/compiler-vapor/src/transform.ts b/packages/compiler-vapor/src/transform.ts index b6213387210..510e7a1f310 100644 --- a/packages/compiler-vapor/src/transform.ts +++ b/packages/compiler-vapor/src/transform.ts @@ -1,14 +1,14 @@ import { NodeTypes, - RootNode, - Node, - TemplateChildNode, - ElementNode, - AttributeNode, - InterpolationNode, - TransformOptions, - DirectiveNode, - ExpressionNode, + type RootNode, + type Node, + type TemplateChildNode, + type ElementNode, + type AttributeNode, + type InterpolationNode, + type TransformOptions, + type DirectiveNode, + type ExpressionNode, } from '@vue/compiler-dom' import { type OperationNode, @@ -136,7 +136,7 @@ export function transform( options: TransformOptions = {}, ): RootIRNode { options.onError ||= defaultOnError - options.onWarn ??= defaultOnWarn + options.onWarn ||= defaultOnWarn const ir: RootIRNode = { type: IRNodeTypes.ROOT, @@ -363,7 +363,7 @@ function transformProp( (exp.type === NodeTypes.SIMPLE_EXPRESSION! && !exp.content.trim()) ) { ctx.options.onError!( - createCompilerError(ErrorCodes.X_V_VAPOR_BIND_NO_EXPRESSION, loc), + createCompilerError(ErrorCodes.VAPOR_BIND_NO_EXPRESSION, loc), ) return } @@ -394,7 +394,7 @@ function transformProp( case 'on': { if (!exp && !modifiers.length) { ctx.options.onError!( - createCompilerError(ErrorCodes.X_V_VAPOR_ON_NO_EXPRESSION, loc), + createCompilerError(ErrorCodes.VAPOR_ON_NO_EXPRESSION, loc), ) return }