]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(compiler-vapor): errors
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Fri, 1 Dec 2023 00:05:43 +0000 (08:05 +0800)
committer三咲智子 Kevin Deng <sxzz@sxzz.moe>
Fri, 1 Dec 2023 00:05:43 +0000 (08:05 +0800)
packages/compiler-core/src/index.ts
packages/compiler-dom/src/errors.ts
packages/compiler-vapor/__tests__/compile.test.ts
packages/compiler-vapor/src/compile.ts
packages/compiler-vapor/src/errors.ts
packages/compiler-vapor/src/transform.ts
playground/setup/dev.js

index 09259cc7b9d5ab27ad4f43a91fcd47c9d70a9c91..25a446e7f466d87f5f2ea7ac450140a67ae4db57 100644 (file)
@@ -26,6 +26,8 @@ export {
   ErrorCodes,
   errorMessages,
   createCompilerError,
+  defaultOnError,
+  defaultOnWarn,
   type CoreCompilerError,
   type CompilerError
 } from './errors'
index f8582c0b6ac11a6646d6c2f09b14ad4c09f77de4..8fb7e36ed6d7dfbf04a415b12e7a0705a0871c92 100644 (file)
@@ -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__]: ``
 }
index 21046f388c0dea4976bd66cb90ec62240ed917ae..e4b8e295fbea3e0bfc29c0c5ab1e008ee88d36e4 100644 (file)
@@ -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,
index 62b4f27dea5b5974800b2fe7a24a509b48535624..1892d4974c220d36738595651ba2b8a1c0c62c45 100644 (file)
@@ -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))
     }
   }
 
index b95eca3c7b57f8efa028d96fcc5081f6c566c328..e6b02cd3a077c07a76654f1c3b839fffbe464ac0 100644 (file)
@@ -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__]: ``,
 }
index dd88feceec81ebd813ed18f30dd953051108d802..dcc781c2a78423cff33ff53b7a0d87c1d2c72ed1 100644 (file)
@@ -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
       }
index f68ac01a783161f2ab41dce6eedff9b0f864a1ca..707358703a00b08bed98525df9b23484207827a2 100644 (file)
@@ -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),