Bläddra i källkod

refactor(compiler-vapor): errors

三咲智子 Kevin Deng 2 år sedan
förälder
incheckning
0d9f0867d7

+ 2 - 0
packages/compiler-core/src/index.ts

@@ -26,6 +26,8 @@ export {
   ErrorCodes,
   errorMessages,
   createCompilerError,
+  defaultOnError,
+  defaultOnWarn,
   type CoreCompilerError,
   type CompilerError
 } from './errors'

+ 5 - 2
packages/compiler-dom/src/errors.ts

@@ -48,7 +48,7 @@ if (__TEST__) {
   }
 }
 
-export const DOMErrorMessages: { [code: number]: string } = {
+export const DOMErrorMessages: Record<DOMErrorCodes, 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.`,
@@ -59,5 +59,8 @@ export const DOMErrorMessages: { [code: number]: string } = {
   [DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE]: `Unnecessary value binding used alongside v-model. It will interfere with v-model's behavior.`,
   [DOMErrorCodes.X_V_SHOW_NO_EXPRESSION]: `v-show is missing expression.`,
   [DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN]: `<Transition> expects exactly one child element or component.`,
-  [DOMErrorCodes.X_IGNORED_SIDE_EFFECT_TAG]: `Tags with side effect (<script> and <style>) are ignored in client component templates.`
+  [DOMErrorCodes.X_IGNORED_SIDE_EFFECT_TAG]: `Tags with side effect (<script> and <style>) are ignored in client component templates.`,
+
+  // just to fulfill types
+  [DOMErrorCodes.__EXTEND_POINT__]: ``
 }

+ 4 - 8
packages/compiler-vapor/__tests__/compile.test.ts

@@ -1,9 +1,5 @@
-import { type RootNode, BindingTypes } from '@vue/compiler-dom'
-import {
-  type CompilerOptions,
-  VaporErrorCodes,
-  compile as _compile,
-} from '../src'
+import { type RootNode, BindingTypes, ErrorCodes } from '@vue/compiler-dom'
+import { type CompilerOptions, compile as _compile } from '../src'
 
 // TODO remove it
 import { format } from 'prettier'
@@ -82,7 +78,7 @@ describe('compile', () => {
         await compile(`<div v-bind:arg />`, { onError })
 
         expect(onError.mock.calls[0][0]).toMatchObject({
-          code: VaporErrorCodes.X_VAPOR_BIND_NO_EXPRESSION,
+          code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
           loc: {
             start: {
               line: 1,
@@ -111,7 +107,7 @@ describe('compile', () => {
         const onError = vi.fn()
         await compile(`<div v-on:click />`, { onError })
         expect(onError.mock.calls[0][0]).toMatchObject({
-          code: VaporErrorCodes.X_VAPOR_ON_NO_EXPRESSION,
+          code: ErrorCodes.X_V_ON_NO_EXPRESSION,
           loc: {
             start: {
               line: 1,

+ 5 - 3
packages/compiler-vapor/src/compile.ts

@@ -4,11 +4,13 @@ import {
   type RootNode,
   type DirectiveTransform,
   parse,
+  defaultOnError,
+  createCompilerError,
+  ErrorCodes,
 } from '@vue/compiler-dom'
 import { extend, isString } from '@vue/shared'
 import { NodeTransform, transform } from './transform'
 import { generate } from './generate'
-import { defaultOnError, createCompilerError, VaporErrorCodes } from './errors'
 import { transformOnce } from './transforms/vOnce'
 import { HackOptions } from './hack'
 
@@ -25,9 +27,9 @@ export function compile(
   /* istanbul ignore if */
   if (__BROWSER__) {
     if (options.prefixIdentifiers === true) {
-      onError(createCompilerError(VaporErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
+      onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
     } else if (isModuleMode) {
-      onError(createCompilerError(VaporErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
+      onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
     }
   }
 

+ 22 - 19
packages/compiler-vapor/src/errors.ts

@@ -1,29 +1,32 @@
-import type { CompilerError } from '@vue/compiler-dom'
+import {
+  CompilerError,
+  SourceLocation,
+  createCompilerError,
+} from '@vue/compiler-dom'
 
-export { createCompilerError } from '@vue/compiler-dom'
-export function defaultOnError(error: CompilerError) {
-  throw error
+export interface VaporCompilerError extends CompilerError {
+  code: VaporErrorCodes
 }
 
-export function defaultOnWarn(msg: CompilerError) {
-  __DEV__ && console.warn(`[Vue warn] ${msg.message}`)
+export function createVaporCompilerError(
+  code: VaporErrorCodes,
+  loc?: SourceLocation,
+) {
+  return createCompilerError(
+    code,
+    loc,
+    __DEV__ || !__BROWSER__ ? VaporErrorMessages : undefined,
+  ) as VaporCompilerError
 }
 
 export enum VaporErrorCodes {
-  // transform errors
-  X_VAPOR_BIND_NO_EXPRESSION,
-  X_VAPOR_ON_NO_EXPRESSION,
-
-  // generic errors
-  X_PREFIX_ID_NOT_SUPPORTED,
-  X_MODULE_MODE_NOT_SUPPORTED,
+  X_V_PLACEHOLDER = 100,
+  __EXTEND_POINT__,
 }
 
-export const errorMessages: Record<VaporErrorCodes, string> = {
-  // transform errors
-  [VaporErrorCodes.X_VAPOR_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
-  [VaporErrorCodes.X_VAPOR_ON_NO_EXPRESSION]: `v-on is missing expression.`,
+export const VaporErrorMessages: Record<VaporErrorCodes, string> = {
+  [VaporErrorCodes.X_V_PLACEHOLDER]: `[placeholder]`,
 
-  [VaporErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
-  [VaporErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`,
+  // just to fulfill types
+  [VaporErrorCodes.__EXTEND_POINT__]: ``,
 }

+ 9 - 11
packages/compiler-vapor/src/transform.ts

@@ -9,24 +9,22 @@ import {
   type ExpressionNode,
   type ParentNode,
   type AllNode,
+  type CompilerCompatOptions,
   NodeTypes,
   BindingTypes,
-  CompilerCompatOptions,
+  defaultOnError,
+  defaultOnWarn,
+  ErrorCodes,
+  createCompilerError,
 } from '@vue/compiler-dom'
+import { EMPTY_OBJ, NOOP, isArray, isVoidTag } from '@vue/shared'
 import {
   type OperationNode,
   type RootIRNode,
   IRNodeTypes,
   DynamicInfo,
 } from './ir'
-import { EMPTY_OBJ, NOOP, isArray, isVoidTag } from '@vue/shared'
-import {
-  VaporErrorCodes,
-  createCompilerError,
-  defaultOnError,
-  defaultOnWarn,
-} from './errors'
-import { HackOptions } from './hack'
+import type { HackOptions } from './hack'
 
 export type NodeTransform = (
   node: RootNode | TemplateChildNode,
@@ -438,7 +436,7 @@ function transformProp(
         (exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content.trim())
       ) {
         ctx.options.onError!(
-          createCompilerError(VaporErrorCodes.X_VAPOR_BIND_NO_EXPRESSION, loc),
+          createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
         )
         return
       }
@@ -467,7 +465,7 @@ function transformProp(
     case 'on': {
       if (!exp && !modifiers.length) {
         ctx.options.onError!(
-          createCompilerError(VaporErrorCodes.X_VAPOR_ON_NO_EXPRESSION, loc),
+          createCompilerError(ErrorCodes.X_V_ON_NO_EXPRESSION, loc),
         )
         return
       }

+ 7 - 3
playground/setup/dev.js

@@ -5,8 +5,12 @@ const dirname = path.dirname(new URL(import.meta.url).pathname)
 const resolve = (/** @type {string} */ p) =>
   path.resolve(dirname, '../../packages', p)
 
-/** @returns {import('vite').Plugin} */
-export function DevPlugin() {
+/**
+ * @param {Object} [env]
+ * @param {boolean} [env.browser]
+ * @returns {import('vite').Plugin}
+ */
+export function DevPlugin({ browser = false } = {}) {
   return {
     name: 'dev-plugin',
     config() {
@@ -39,7 +43,7 @@ export function DevPlugin() {
           // this is only used during Vue's internal tests
           __TEST__: `false`,
           // If the build is expected to run directly in the browser (global / esm builds)
-          __BROWSER__: String(true),
+          __BROWSER__: String(browser),
           __GLOBAL__: String(false),
           __ESM_BUNDLER__: String(true),
           __ESM_BROWSER__: String(false),