Skip to content

Commit

Permalink
fix: according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Nov 28, 2023
1 parent 12ca532 commit 0ab2281
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/compiler-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function defaultOnWarn(msg: CompilerError) {
__DEV__ && console.warn(`[Vue warn] ${msg.message}`)
}

type InferCompilerError<T> = T extends ErrorCodes
export type InferCompilerError<T> = T extends ErrorCodes
? CoreCompilerError
: CompilerError

Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-vapor/__tests__/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('comile', () => {
await compile(`<div v-bind:arg />`, { 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,
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('comile', () => {
const onError = vi.fn()
await compile(`<div v-on:click />`, { 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,
Expand Down
50 changes: 10 additions & 40 deletions packages/compiler-vapor/src/errors.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends ErrorCodes
? CoreCompilerError
: CompilerError

export function createCompilerError<T extends number>(
code: T,
loc?: SourceLocation,
messages?: { [code: number]: string },
additionalMessage?: string,
): InferCompilerError<T> {
const msg =
__DEV__ || !__BROWSER__
? (messages || errorMessages)[code] + (additionalMessage || ``)
: code
const error = new SyntaxError(String(msg)) as InferCompilerError<T>
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
Expand All @@ -45,6 +15,6 @@ export const enum ErrorCodes {

export const errorMessages: Record<ErrorCodes, string> = {
// 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.`,
}
24 changes: 12 additions & 12 deletions packages/compiler-vapor/src/transform.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0ab2281

Please sign in to comment.