errors.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import type { SourceLocation } from './ast'
  2. export interface CompilerError extends SyntaxError {
  3. code: number | string
  4. loc?: SourceLocation
  5. }
  6. export interface CoreCompilerError extends CompilerError {
  7. code: ErrorCodes
  8. }
  9. export function defaultOnError(error: CompilerError): never {
  10. throw error
  11. }
  12. export function defaultOnWarn(msg: CompilerError): void {
  13. __DEV__ && console.warn(`[Vue warn] ${msg.message}`)
  14. }
  15. type InferCompilerError<T> = T extends ErrorCodes
  16. ? CoreCompilerError
  17. : CompilerError
  18. export function createCompilerError<T extends number>(
  19. code: T,
  20. loc?: SourceLocation,
  21. messages?: { [code: number]: string },
  22. additionalMessage?: string,
  23. ): InferCompilerError<T> {
  24. const msg =
  25. __DEV__ || !__BROWSER__
  26. ? (messages || errorMessages)[code] + (additionalMessage || ``)
  27. : `https://vuejs.org/error-reference/#compiler-${code}`
  28. const error = new SyntaxError(String(msg)) as InferCompilerError<T>
  29. error.code = code
  30. error.loc = loc
  31. return error
  32. }
  33. export enum ErrorCodes {
  34. // parse errors
  35. ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  36. CDATA_IN_HTML_CONTENT,
  37. DUPLICATE_ATTRIBUTE,
  38. END_TAG_WITH_ATTRIBUTES,
  39. END_TAG_WITH_TRAILING_SOLIDUS,
  40. EOF_BEFORE_TAG_NAME,
  41. EOF_IN_CDATA,
  42. EOF_IN_COMMENT,
  43. EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  44. EOF_IN_TAG,
  45. INCORRECTLY_CLOSED_COMMENT,
  46. INCORRECTLY_OPENED_COMMENT,
  47. INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  48. MISSING_ATTRIBUTE_VALUE,
  49. MISSING_END_TAG_NAME,
  50. MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  51. NESTED_COMMENT,
  52. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  53. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  54. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  55. UNEXPECTED_NULL_CHARACTER,
  56. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  57. UNEXPECTED_SOLIDUS_IN_TAG,
  58. // Vue-specific parse errors
  59. X_INVALID_END_TAG,
  60. X_MISSING_END_TAG,
  61. X_MISSING_INTERPOLATION_END,
  62. X_MISSING_DIRECTIVE_NAME,
  63. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  64. // transform errors
  65. X_V_IF_NO_EXPRESSION,
  66. X_V_IF_SAME_KEY,
  67. X_V_ELSE_NO_ADJACENT_IF,
  68. X_V_FOR_NO_EXPRESSION,
  69. X_V_FOR_MALFORMED_EXPRESSION,
  70. X_V_FOR_TEMPLATE_KEY_PLACEMENT,
  71. X_V_BIND_NO_EXPRESSION,
  72. X_V_ON_NO_EXPRESSION,
  73. X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
  74. X_V_SLOT_MIXED_SLOT_USAGE,
  75. X_V_SLOT_DUPLICATE_SLOT_NAMES,
  76. X_V_SLOT_EXTRANEOUS_DEFAULT_SLOT_CHILDREN,
  77. X_V_SLOT_MISPLACED,
  78. X_V_MODEL_NO_EXPRESSION,
  79. X_V_MODEL_MALFORMED_EXPRESSION,
  80. X_V_MODEL_ON_SCOPE_VARIABLE,
  81. X_V_MODEL_ON_PROPS,
  82. X_V_MODEL_ON_CONST,
  83. X_INVALID_EXPRESSION,
  84. X_KEEP_ALIVE_INVALID_CHILDREN,
  85. // generic errors
  86. X_PREFIX_ID_NOT_SUPPORTED,
  87. X_MODULE_MODE_NOT_SUPPORTED,
  88. X_CACHE_HANDLER_NOT_SUPPORTED,
  89. X_SCOPE_ID_NOT_SUPPORTED,
  90. X_VNODE_HOOKS,
  91. // placed here to preserve order for the current minor
  92. // TODO adjust order in 3.5
  93. X_V_BIND_INVALID_SAME_NAME_ARGUMENT,
  94. // Special value for higher-order compilers to pick up the last code
  95. // to avoid collision of error codes. This should always be kept as the last
  96. // item.
  97. __EXTEND_POINT__,
  98. }
  99. export const errorMessages: Record<ErrorCodes, string> = {
  100. // parse errors
  101. [ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT]: 'Illegal comment.',
  102. [ErrorCodes.CDATA_IN_HTML_CONTENT]:
  103. 'CDATA section is allowed only in XML context.',
  104. [ErrorCodes.DUPLICATE_ATTRIBUTE]: 'Duplicate attribute.',
  105. [ErrorCodes.END_TAG_WITH_ATTRIBUTES]: 'End tag cannot have attributes.',
  106. [ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS]: "Illegal '/' in tags.",
  107. [ErrorCodes.EOF_BEFORE_TAG_NAME]: 'Unexpected EOF in tag.',
  108. [ErrorCodes.EOF_IN_CDATA]: 'Unexpected EOF in CDATA section.',
  109. [ErrorCodes.EOF_IN_COMMENT]: 'Unexpected EOF in comment.',
  110. [ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT]:
  111. 'Unexpected EOF in script.',
  112. [ErrorCodes.EOF_IN_TAG]: 'Unexpected EOF in tag.',
  113. [ErrorCodes.INCORRECTLY_CLOSED_COMMENT]: 'Incorrectly closed comment.',
  114. [ErrorCodes.INCORRECTLY_OPENED_COMMENT]: 'Incorrectly opened comment.',
  115. [ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME]:
  116. "Illegal tag name. Use '&lt;' to print '<'.",
  117. [ErrorCodes.MISSING_ATTRIBUTE_VALUE]: 'Attribute value was expected.',
  118. [ErrorCodes.MISSING_END_TAG_NAME]: 'End tag name was expected.',
  119. [ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES]:
  120. 'Whitespace was expected.',
  121. [ErrorCodes.NESTED_COMMENT]: "Unexpected '<!--' in comment.",
  122. [ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME]:
  123. 'Attribute name cannot contain U+0022 ("), U+0027 (\'), and U+003C (<).',
  124. [ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE]:
  125. 'Unquoted attribute value cannot contain U+0022 ("), U+0027 (\'), U+003C (<), U+003D (=), and U+0060 (`).',
  126. [ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME]:
  127. "Attribute name cannot start with '='.",
  128. [ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME]:
  129. "'<?' is allowed only in XML context.",
  130. [ErrorCodes.UNEXPECTED_NULL_CHARACTER]: `Unexpected null character.`,
  131. [ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG]: "Illegal '/' in tags.",
  132. // Vue-specific parse errors
  133. [ErrorCodes.X_INVALID_END_TAG]: 'Invalid end tag.',
  134. [ErrorCodes.X_MISSING_END_TAG]: 'Element is missing end tag.',
  135. [ErrorCodes.X_MISSING_INTERPOLATION_END]:
  136. 'Interpolation end sign was not found.',
  137. [ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
  138. 'End bracket for dynamic directive argument was not found. ' +
  139. 'Note that dynamic directive argument cannot contain spaces.',
  140. [ErrorCodes.X_MISSING_DIRECTIVE_NAME]: 'Legal directive name was expected.',
  141. // transform errors
  142. [ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
  143. [ErrorCodes.X_V_IF_SAME_KEY]: `v-if/else branches must use unique keys.`,
  144. [ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if or v-else-if.`,
  145. [ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
  146. [ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
  147. [ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT]: `<template v-for> key should be placed on the <template> tag.`,
  148. [ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
  149. [ErrorCodes.X_V_BIND_INVALID_SAME_NAME_ARGUMENT]: `v-bind with same-name shorthand only allows static argument.`,
  150. [ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
  151. [ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
  152. [ErrorCodes.X_V_SLOT_MIXED_SLOT_USAGE]:
  153. `Mixed v-slot usage on both the component and nested <template>. ` +
  154. `When there are multiple named slots, all slots should use <template> ` +
  155. `syntax to avoid scope ambiguity.`,
  156. [ErrorCodes.X_V_SLOT_DUPLICATE_SLOT_NAMES]: `Duplicate slot names found. `,
  157. [ErrorCodes.X_V_SLOT_EXTRANEOUS_DEFAULT_SLOT_CHILDREN]:
  158. `Extraneous children found when component already has explicitly named ` +
  159. `default slot. These children will be ignored.`,
  160. [ErrorCodes.X_V_SLOT_MISPLACED]: `v-slot can only be used on components or <template> tags.`,
  161. [ErrorCodes.X_V_MODEL_NO_EXPRESSION]: `v-model is missing expression.`,
  162. [ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION]: `v-model value must be a valid JavaScript member expression.`,
  163. [ErrorCodes.X_V_MODEL_ON_SCOPE_VARIABLE]: `v-model cannot be used on v-for or v-slot scope variables because they are not writable.`,
  164. [ErrorCodes.X_V_MODEL_ON_PROPS]: `v-model cannot be used on a prop, because local prop bindings are not writable.\nUse a v-bind binding combined with a v-on listener that emits update:x event instead.`,
  165. [ErrorCodes.X_V_MODEL_ON_CONST]: `v-model cannot be used on a const binding because it is not writable.`,
  166. [ErrorCodes.X_INVALID_EXPRESSION]: `Error parsing JavaScript expression: `,
  167. [ErrorCodes.X_KEEP_ALIVE_INVALID_CHILDREN]: `<KeepAlive> expects exactly one child component.`,
  168. [ErrorCodes.X_VNODE_HOOKS]: `@vnode-* hooks in templates are no longer supported. Use the vue: prefix instead. For example, @vnode-mounted should be changed to @vue:mounted. @vnode-* hooks support has been removed in 3.4.`,
  169. // generic errors
  170. [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
  171. [ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`,
  172. [ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED]: `"cacheHandlers" option is only supported when the "prefixIdentifiers" option is enabled.`,
  173. [ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED]: `"scopeId" option is only supported in module mode.`,
  174. // just to fulfill types
  175. [ErrorCodes.__EXTEND_POINT__]: ``,
  176. }