Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vapor errors of v-bind / v-on #3

Merged
merged 13 commits into from
Nov 28, 2023
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
LittleSound marked this conversation as resolved.
Show resolved Hide resolved
? 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
38 changes: 38 additions & 0 deletions packages/compiler-vapor/__tests__/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BindingTypes, CompilerOptions, RootNode } from '@vue/compiler-dom'
// TODO remove it
import { format } from 'prettier'
import { compile as _compile } from '../src'
import { ErrorCodes } from '../src/errors'

async function compile(
template: string | RootNode,
Expand Down Expand Up @@ -64,6 +65,25 @@ describe('comile', () => {
})
expect(code).matchSnapshot()
})

test('should error if no expression', async () => {
const onError = vi.fn()
await compile(`<div v-bind:arg />`, { onError })

expect(onError.mock.calls[0][0]).toMatchObject({
code: ErrorCodes.VAPOR_BIND_NO_EXPRESSION,
loc: {
start: {
line: 1,
column: 6,
},
end: {
line: 1,
column: 16,
},
},
})
})
})

describe('v-on', () => {
Expand All @@ -75,6 +95,24 @@ describe('comile', () => {
})
expect(code).matchSnapshot()
})

test('should error if no expression AND no modifier', async () => {
const onError = vi.fn()
await compile(`<div v-on:click />`, { onError })
expect(onError.mock.calls[0][0]).toMatchObject({
code: ErrorCodes.VAPOR_ON_NO_EXPRESSION,
loc: {
start: {
line: 1,
column: 6,
},
end: {
line: 1,
column: 16,
},
},
})
})
})

describe('v-html', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/compiler-vapor/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export {
createCompilerError,
defaultOnError,
defaultOnWarn,
type CoreCompilerError,
type CompilerError,
type InferCompilerError,
} from '@vue/compiler-dom'

export const enum ErrorCodes {
// transform errors
VAPOR_BIND_NO_EXPRESSION,
VAPOR_ON_NO_EXPRESSION,
}

export const errorMessages: Record<ErrorCodes, string> = {
// transform errors
[ErrorCodes.VAPOR_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
[ErrorCodes.VAPOR_ON_NO_EXPRESSION]: `v-on is missing expression.`,
}
51 changes: 40 additions & 11 deletions packages/compiler-vapor/src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {
import {
LittleSound marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -17,6 +17,12 @@ import {
DynamicInfo,
} from './ir'
import { isVoidTag } from '@vue/shared'
import {
ErrorCodes,
createCompilerError,
defaultOnError,
defaultOnWarn,
} from './errors'

export interface TransformContext<T extends Node = Node> {
node: T
Expand Down Expand Up @@ -129,6 +135,9 @@ export function transform(
root: RootNode,
options: TransformOptions = {},
): RootIRNode {
options.onError ||= defaultOnError
options.onWarn ||= defaultOnWarn

const ir: RootIRNode = {
type: IRNodeTypes.ROOT,
loc: root.loc,
Expand All @@ -145,6 +154,7 @@ export function transform(
helpers: new Set([]),
vaporHelpers: new Set([]),
}

const ctx = createRootContext(ir, root, options)

// TODO: transform presets, see packages/compiler-core/src/transforms
Expand Down Expand Up @@ -343,9 +353,21 @@ function transformProp(
return
}

const expr = processExpression(ctx, node.exp)
const { exp, loc, modifiers } = node

const expr = processExpression(ctx, exp)
switch (name) {
case 'bind': {
if (
!exp ||
(exp.type === NodeTypes.SIMPLE_EXPRESSION! && !exp.content.trim())
) {
ctx.options.onError!(
createCompilerError(ErrorCodes.VAPOR_BIND_NO_EXPRESSION, loc),
)
return
}

if (expr === null) {
// TODO: Vue 3.4 supported shorthand syntax
// https://github.com/vuejs/core/pull/9451
Expand All @@ -370,6 +392,13 @@ function transformProp(
break
}
case 'on': {
if (!exp && !modifiers.length) {
ctx.options.onError!(
createCompilerError(ErrorCodes.VAPOR_ON_NO_EXPRESSION, loc),
)
return
}

if (!node.arg) {
// TODO support v-on="{}"
return
Expand Down