feat(compiler): v-text transform + move dom-specific errros codes to compiler-dom
This commit is contained in:
parent
21441830dd
commit
f91d335e65
@ -1,24 +1,29 @@
|
|||||||
import { SourceLocation } from './ast'
|
import { SourceLocation } from './ast'
|
||||||
|
|
||||||
export interface CompilerError extends SyntaxError {
|
export interface CompilerError extends SyntaxError {
|
||||||
code: ErrorCodes
|
code: number
|
||||||
loc?: SourceLocation
|
loc?: SourceLocation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CoreCompilerError extends CompilerError {
|
||||||
|
code: ErrorCodes
|
||||||
|
}
|
||||||
|
|
||||||
export function defaultOnError(error: CompilerError) {
|
export function defaultOnError(error: CompilerError) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createCompilerError(
|
export function createCompilerError<T extends number>(
|
||||||
code: ErrorCodes,
|
code: T,
|
||||||
loc?: SourceLocation
|
loc?: SourceLocation,
|
||||||
): CompilerError {
|
messages?: { [code: number]: string }
|
||||||
const msg = __DEV__ || !__BROWSER__ ? errorMessages[code] : code
|
): T extends ErrorCodes ? CoreCompilerError : CompilerError {
|
||||||
|
const msg = __DEV__ || !__BROWSER__ ? (messages || errorMessages)[code] : code
|
||||||
const locInfo = loc ? ` (${loc.start.line}:${loc.start.column})` : ``
|
const locInfo = loc ? ` (${loc.start.line}:${loc.start.column})` : ``
|
||||||
const error = new SyntaxError(msg + locInfo) as CompilerError
|
const error = new SyntaxError(msg + locInfo) as CompilerError
|
||||||
error.code = code
|
error.code = code
|
||||||
error.loc = loc
|
error.loc = loc
|
||||||
return error
|
return error as any
|
||||||
}
|
}
|
||||||
|
|
||||||
export const enum ErrorCodes {
|
export const enum ErrorCodes {
|
||||||
@ -68,8 +73,6 @@ export const enum ErrorCodes {
|
|||||||
X_V_FOR_MALFORMED_EXPRESSION,
|
X_V_FOR_MALFORMED_EXPRESSION,
|
||||||
X_V_BIND_NO_EXPRESSION,
|
X_V_BIND_NO_EXPRESSION,
|
||||||
X_V_ON_NO_EXPRESSION,
|
X_V_ON_NO_EXPRESSION,
|
||||||
X_V_HTML_NO_EXPRESSION,
|
|
||||||
X_V_HTML_WITH_CHILDREN,
|
|
||||||
X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
|
X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
|
||||||
X_V_SLOT_NAMED_SLOT_ON_COMPONENT,
|
X_V_SLOT_NAMED_SLOT_ON_COMPONENT,
|
||||||
X_V_SLOT_MIXED_SLOT_USAGE,
|
X_V_SLOT_MIXED_SLOT_USAGE,
|
||||||
@ -79,7 +82,12 @@ export const enum ErrorCodes {
|
|||||||
|
|
||||||
// generic errors
|
// generic errors
|
||||||
X_PREFIX_ID_NOT_SUPPORTED,
|
X_PREFIX_ID_NOT_SUPPORTED,
|
||||||
X_MODULE_MODE_NOT_SUPPORTED
|
X_MODULE_MODE_NOT_SUPPORTED,
|
||||||
|
|
||||||
|
// Sepcial value for higher-order compilers to pick up the last code
|
||||||
|
// to avoid collision of error codes. This should always be kept as the last
|
||||||
|
// item.
|
||||||
|
__EXTEND_POINT__
|
||||||
}
|
}
|
||||||
|
|
||||||
export const errorMessages: { [code: number]: string } = {
|
export const errorMessages: { [code: number]: string } = {
|
||||||
@ -146,8 +154,6 @@ export const errorMessages: { [code: number]: string } = {
|
|||||||
[ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
|
[ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
|
||||||
[ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
|
[ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
|
||||||
[ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
|
[ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
|
||||||
[ErrorCodes.X_V_HTML_NO_EXPRESSION]: `v-html is missing epxression.`,
|
|
||||||
[ErrorCodes.X_V_HTML_WITH_CHILDREN]: `v-html will override element children.`,
|
|
||||||
[ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
|
[ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
|
||||||
[ErrorCodes.X_V_SLOT_NAMED_SLOT_ON_COMPONENT]:
|
[ErrorCodes.X_V_SLOT_NAMED_SLOT_ON_COMPONENT]:
|
||||||
`Named v-slot on component. ` +
|
`Named v-slot on component. ` +
|
||||||
|
@ -87,5 +87,10 @@ export {
|
|||||||
CodegenContext,
|
CodegenContext,
|
||||||
CodegenResult
|
CodegenResult
|
||||||
} from './codegen'
|
} from './codegen'
|
||||||
export { ErrorCodes, CompilerError, createCompilerError } from './errors'
|
export {
|
||||||
|
ErrorCodes,
|
||||||
|
CoreCompilerError,
|
||||||
|
CompilerError,
|
||||||
|
createCompilerError
|
||||||
|
} from './errors'
|
||||||
export * from './ast'
|
export * from './ast'
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ErrorCodes,
|
ErrorCodes,
|
||||||
CompilerError,
|
CoreCompilerError,
|
||||||
createCompilerError,
|
createCompilerError,
|
||||||
defaultOnError
|
defaultOnError
|
||||||
} from './errors'
|
} from './errors'
|
||||||
@ -38,7 +38,7 @@ export interface ParserOptions {
|
|||||||
// The full set is https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references
|
// The full set is https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references
|
||||||
namedCharacterReferences?: { [name: string]: string | undefined }
|
namedCharacterReferences?: { [name: string]: string | undefined }
|
||||||
|
|
||||||
onError?: (error: CompilerError) => void
|
onError?: (error: CoreCompilerError) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultParserOptions: Required<ParserOptions> = {
|
export const defaultParserOptions: Required<ParserOptions> = {
|
||||||
|
@ -2,8 +2,7 @@ import {
|
|||||||
parse,
|
parse,
|
||||||
transform,
|
transform,
|
||||||
PlainElementNode,
|
PlainElementNode,
|
||||||
CompilerOptions,
|
CompilerOptions
|
||||||
ErrorCodes
|
|
||||||
} from '@vue/compiler-core'
|
} from '@vue/compiler-core'
|
||||||
import { transformVHtml } from '../../src/transforms/vHtml'
|
import { transformVHtml } from '../../src/transforms/vHtml'
|
||||||
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
|
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
|
||||||
@ -12,6 +11,7 @@ import {
|
|||||||
genFlagText
|
genFlagText
|
||||||
} from '../../../compiler-core/__tests__/testUtils'
|
} from '../../../compiler-core/__tests__/testUtils'
|
||||||
import { PatchFlags } from '@vue/shared'
|
import { PatchFlags } from '@vue/shared'
|
||||||
|
import { DOMErrorCodes } from '../../src/errors'
|
||||||
|
|
||||||
function transformWithVHtml(template: string, options: CompilerOptions = {}) {
|
function transformWithVHtml(template: string, options: CompilerOptions = {}) {
|
||||||
const ast = parse(template)
|
const ast = parse(template)
|
||||||
@ -47,7 +47,7 @@ describe('compiler: v-html transform', () => {
|
|||||||
onError
|
onError
|
||||||
})
|
})
|
||||||
expect(onError.mock.calls).toMatchObject([
|
expect(onError.mock.calls).toMatchObject([
|
||||||
[{ code: ErrorCodes.X_V_HTML_WITH_CHILDREN }]
|
[{ code: DOMErrorCodes.X_V_HTML_WITH_CHILDREN }]
|
||||||
])
|
])
|
||||||
expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({
|
expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({
|
||||||
arguments: [
|
arguments: [
|
||||||
@ -68,7 +68,7 @@ describe('compiler: v-html transform', () => {
|
|||||||
onError
|
onError
|
||||||
})
|
})
|
||||||
expect(onError.mock.calls).toMatchObject([
|
expect(onError.mock.calls).toMatchObject([
|
||||||
[{ code: ErrorCodes.X_V_HTML_NO_EXPRESSION }]
|
[{ code: DOMErrorCodes.X_V_HTML_NO_EXPRESSION }]
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
74
packages/compiler-dom/__tests__/transforms/vText.spec.ts
Normal file
74
packages/compiler-dom/__tests__/transforms/vText.spec.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import {
|
||||||
|
parse,
|
||||||
|
transform,
|
||||||
|
PlainElementNode,
|
||||||
|
CompilerOptions
|
||||||
|
} from '@vue/compiler-core'
|
||||||
|
import { transformVText } from '../../src/transforms/vText'
|
||||||
|
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
|
||||||
|
import {
|
||||||
|
createObjectMatcher,
|
||||||
|
genFlagText
|
||||||
|
} from '../../../compiler-core/__tests__/testUtils'
|
||||||
|
import { PatchFlags } from '@vue/shared'
|
||||||
|
import { DOMErrorCodes } from '../../src/errors'
|
||||||
|
|
||||||
|
function transformWithVText(template: string, options: CompilerOptions = {}) {
|
||||||
|
const ast = parse(template)
|
||||||
|
transform(ast, {
|
||||||
|
nodeTransforms: [transformElement],
|
||||||
|
directiveTransforms: {
|
||||||
|
text: transformVText
|
||||||
|
},
|
||||||
|
...options
|
||||||
|
})
|
||||||
|
return ast
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('compiler: v-text transform', () => {
|
||||||
|
it('should convert v-text to textContent', () => {
|
||||||
|
const ast = transformWithVText(`<div v-text="test"/>`)
|
||||||
|
expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({
|
||||||
|
arguments: [
|
||||||
|
`"div"`,
|
||||||
|
createObjectMatcher({
|
||||||
|
textContent: `[test]`
|
||||||
|
}),
|
||||||
|
`null`,
|
||||||
|
genFlagText(PatchFlags.PROPS),
|
||||||
|
`["textContent"]`
|
||||||
|
]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should raise error and ignore children when v-text is present', () => {
|
||||||
|
const onError = jest.fn()
|
||||||
|
const ast = transformWithVText(`<div v-text="test">hello</div>`, {
|
||||||
|
onError
|
||||||
|
})
|
||||||
|
expect(onError.mock.calls).toMatchObject([
|
||||||
|
[{ code: DOMErrorCodes.X_V_TEXT_WITH_CHILDREN }]
|
||||||
|
])
|
||||||
|
expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({
|
||||||
|
arguments: [
|
||||||
|
`"div"`,
|
||||||
|
createObjectMatcher({
|
||||||
|
textContent: `[test]`
|
||||||
|
}),
|
||||||
|
`null`, // <-- children should have been removed
|
||||||
|
genFlagText(PatchFlags.PROPS),
|
||||||
|
`["textContent"]`
|
||||||
|
]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should raise error if has no expression', () => {
|
||||||
|
const onError = jest.fn()
|
||||||
|
transformWithVText(`<div v-text></div>`, {
|
||||||
|
onError
|
||||||
|
})
|
||||||
|
expect(onError.mock.calls).toMatchObject([
|
||||||
|
[{ code: DOMErrorCodes.X_V_TEXT_NO_EXPRESSION }]
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
35
packages/compiler-dom/src/errors.ts
Normal file
35
packages/compiler-dom/src/errors.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import {
|
||||||
|
SourceLocation,
|
||||||
|
CompilerError,
|
||||||
|
createCompilerError,
|
||||||
|
ErrorCodes
|
||||||
|
} from '@vue/compiler-core'
|
||||||
|
|
||||||
|
export interface DOMCompilerError extends CompilerError {
|
||||||
|
code: DOMErrorCodes
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createDOMCompilerError(
|
||||||
|
code: DOMErrorCodes,
|
||||||
|
loc?: SourceLocation
|
||||||
|
): DOMCompilerError {
|
||||||
|
return createCompilerError(
|
||||||
|
code,
|
||||||
|
loc,
|
||||||
|
__DEV__ || !__BROWSER__ ? DOMErrorMessages : undefined
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const enum DOMErrorCodes {
|
||||||
|
X_V_HTML_NO_EXPRESSION = ErrorCodes.__EXTEND_POINT__,
|
||||||
|
X_V_HTML_WITH_CHILDREN,
|
||||||
|
X_V_TEXT_NO_EXPRESSION,
|
||||||
|
X_V_TEXT_WITH_CHILDREN
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DOMErrorMessages: { [code: number]: string } = {
|
||||||
|
[DOMErrorCodes.X_V_HTML_NO_EXPRESSION]: `v-html is missing expression.`,
|
||||||
|
[DOMErrorCodes.X_V_HTML_WITH_CHILDREN]: `v-html will override element children.`,
|
||||||
|
[DOMErrorCodes.X_V_TEXT_NO_EXPRESSION]: `v-text is missing expression.`,
|
||||||
|
[DOMErrorCodes.X_V_TEXT_WITH_CHILDREN]: `v-text will override element children.`
|
||||||
|
}
|
@ -1,18 +1,21 @@
|
|||||||
import {
|
import {
|
||||||
DirectiveTransform,
|
DirectiveTransform,
|
||||||
createCompilerError,
|
|
||||||
ErrorCodes,
|
|
||||||
createObjectProperty,
|
createObjectProperty,
|
||||||
createSimpleExpression
|
createSimpleExpression
|
||||||
} from '@vue/compiler-core'
|
} from '@vue/compiler-core'
|
||||||
|
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
|
||||||
|
|
||||||
export const transformVHtml: DirectiveTransform = (dir, node, context) => {
|
export const transformVHtml: DirectiveTransform = (dir, node, context) => {
|
||||||
const { exp, loc } = dir
|
const { exp, loc } = dir
|
||||||
if (!exp) {
|
if (!exp) {
|
||||||
context.onError(createCompilerError(ErrorCodes.X_V_HTML_NO_EXPRESSION, loc))
|
context.onError(
|
||||||
|
createDOMCompilerError(DOMErrorCodes.X_V_HTML_NO_EXPRESSION, loc)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if (node.children.length) {
|
if (node.children.length) {
|
||||||
context.onError(createCompilerError(ErrorCodes.X_V_HTML_WITH_CHILDREN, loc))
|
context.onError(
|
||||||
|
createDOMCompilerError(DOMErrorCodes.X_V_HTML_WITH_CHILDREN, loc)
|
||||||
|
)
|
||||||
node.children.length = 0
|
node.children.length = 0
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
@ -1 +1,28 @@
|
|||||||
// TODO
|
import {
|
||||||
|
DirectiveTransform,
|
||||||
|
createObjectProperty,
|
||||||
|
createSimpleExpression
|
||||||
|
} from '@vue/compiler-core'
|
||||||
|
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
|
||||||
|
|
||||||
|
export const transformVText: DirectiveTransform = (dir, node, context) => {
|
||||||
|
const { exp, loc } = dir
|
||||||
|
if (!exp) {
|
||||||
|
context.onError(
|
||||||
|
createDOMCompilerError(DOMErrorCodes.X_V_TEXT_NO_EXPRESSION, loc)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (node.children.length) {
|
||||||
|
context.onError(
|
||||||
|
createDOMCompilerError(DOMErrorCodes.X_V_TEXT_WITH_CHILDREN, loc)
|
||||||
|
)
|
||||||
|
node.children.length = 0
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
props: createObjectProperty(
|
||||||
|
createSimpleExpression(`textContent`, true, loc),
|
||||||
|
exp || createSimpleExpression('', true)
|
||||||
|
),
|
||||||
|
needRuntime: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user