errors.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. type CompilerError,
  3. ErrorCodes,
  4. type SourceLocation,
  5. createCompilerError,
  6. } from '@vue/compiler-core'
  7. export interface DOMCompilerError extends CompilerError {
  8. code: DOMErrorCodes
  9. }
  10. export function createDOMCompilerError(
  11. code: DOMErrorCodes,
  12. loc?: SourceLocation,
  13. ) {
  14. return createCompilerError(
  15. code,
  16. loc,
  17. __DEV__ || !__BROWSER__ ? DOMErrorMessages : undefined,
  18. ) as DOMCompilerError
  19. }
  20. export enum DOMErrorCodes {
  21. X_V_HTML_NO_EXPRESSION = 54 /* ErrorCodes.__EXTEND_POINT__ */,
  22. X_V_HTML_WITH_CHILDREN,
  23. X_V_TEXT_NO_EXPRESSION,
  24. X_V_TEXT_WITH_CHILDREN,
  25. X_V_MODEL_ON_INVALID_ELEMENT,
  26. X_V_MODEL_ARG_ON_ELEMENT,
  27. X_V_MODEL_ON_FILE_INPUT_ELEMENT,
  28. X_V_MODEL_UNNECESSARY_VALUE,
  29. X_V_SHOW_NO_EXPRESSION,
  30. X_TRANSITION_INVALID_CHILDREN,
  31. X_IGNORED_SIDE_EFFECT_TAG,
  32. __EXTEND_POINT__,
  33. }
  34. if (__TEST__) {
  35. // esbuild cannot infer enum increments if first value is from another
  36. // file, so we have to manually keep them in sync. this check ensures it
  37. // errors out if there are collisions.
  38. if (DOMErrorCodes.X_V_HTML_NO_EXPRESSION < ErrorCodes.__EXTEND_POINT__) {
  39. throw new Error(
  40. `DOMErrorCodes need to be updated to ${
  41. ErrorCodes.__EXTEND_POINT__ + 1
  42. } to match extension point from core ErrorCodes.`,
  43. )
  44. }
  45. }
  46. export const DOMErrorMessages: { [code: number]: string } = {
  47. [DOMErrorCodes.X_V_HTML_NO_EXPRESSION]: `v-html is missing expression.`,
  48. [DOMErrorCodes.X_V_HTML_WITH_CHILDREN]: `v-html will override element children.`,
  49. [DOMErrorCodes.X_V_TEXT_NO_EXPRESSION]: `v-text is missing expression.`,
  50. [DOMErrorCodes.X_V_TEXT_WITH_CHILDREN]: `v-text will override element children.`,
  51. [DOMErrorCodes.X_V_MODEL_ON_INVALID_ELEMENT]: `v-model can only be used on <input>, <textarea> and <select> elements.`,
  52. [DOMErrorCodes.X_V_MODEL_ARG_ON_ELEMENT]: `v-model argument is not supported on plain elements.`,
  53. [DOMErrorCodes.X_V_MODEL_ON_FILE_INPUT_ELEMENT]: `v-model cannot be used on file inputs since they are read-only. Use a v-on:change listener instead.`,
  54. [DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE]: `Unnecessary value binding used alongside v-model. It will interfere with v-model's behavior.`,
  55. [DOMErrorCodes.X_V_SHOW_NO_EXPRESSION]: `v-show is missing expression.`,
  56. [DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN]: `<Transition> expects exactly one child element or component.`,
  57. [DOMErrorCodes.X_IGNORED_SIDE_EFFECT_TAG]: `Tags with side effect (<script> and <style>) are ignored in client component templates.`,
  58. }