errors.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { 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) {
  10. throw error
  11. }
  12. export function defaultOnWarn(msg: CompilerError) {
  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. : 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 const 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_INVALID_EXPRESSION,
  82. X_KEEP_ALIVE_INVALID_CHILDREN,
  83. // generic errors
  84. X_PREFIX_ID_NOT_SUPPORTED,
  85. X_MODULE_MODE_NOT_SUPPORTED,
  86. X_CACHE_HANDLER_NOT_SUPPORTED,
  87. X_SCOPE_ID_NOT_SUPPORTED,
  88. // Special value for higher-order compilers to pick up the last code
  89. // to avoid collision of error codes. This should always be kept as the last
  90. // item.
  91. __EXTEND_POINT__
  92. }
  93. export const errorMessages: Record<ErrorCodes, string> = {
  94. // parse errors
  95. [ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT]: 'Illegal comment.',
  96. [ErrorCodes.CDATA_IN_HTML_CONTENT]:
  97. 'CDATA section is allowed only in XML context.',
  98. [ErrorCodes.DUPLICATE_ATTRIBUTE]: 'Duplicate attribute.',
  99. [ErrorCodes.END_TAG_WITH_ATTRIBUTES]: 'End tag cannot have attributes.',
  100. [ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS]: "Illegal '/' in tags.",
  101. [ErrorCodes.EOF_BEFORE_TAG_NAME]: 'Unexpected EOF in tag.',
  102. [ErrorCodes.EOF_IN_CDATA]: 'Unexpected EOF in CDATA section.',
  103. [ErrorCodes.EOF_IN_COMMENT]: 'Unexpected EOF in comment.',
  104. [ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT]:
  105. 'Unexpected EOF in script.',
  106. [ErrorCodes.EOF_IN_TAG]: 'Unexpected EOF in tag.',
  107. [ErrorCodes.INCORRECTLY_CLOSED_COMMENT]: 'Incorrectly closed comment.',
  108. [ErrorCodes.INCORRECTLY_OPENED_COMMENT]: 'Incorrectly opened comment.',
  109. [ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME]:
  110. "Illegal tag name. Use '&lt;' to print '<'.",
  111. [ErrorCodes.MISSING_ATTRIBUTE_VALUE]: 'Attribute value was expected.',
  112. [ErrorCodes.MISSING_END_TAG_NAME]: 'End tag name was expected.',
  113. [ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES]:
  114. 'Whitespace was expected.',
  115. [ErrorCodes.NESTED_COMMENT]: "Unexpected '<!--' in comment.",
  116. [ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME]:
  117. 'Attribute name cannot contain U+0022 ("), U+0027 (\'), and U+003C (<).',
  118. [ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE]:
  119. 'Unquoted attribute value cannot contain U+0022 ("), U+0027 (\'), U+003C (<), U+003D (=), and U+0060 (`).',
  120. [ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME]:
  121. "Attribute name cannot start with '='.",
  122. [ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME]:
  123. "'<?' is allowed only in XML context.",
  124. [ErrorCodes.UNEXPECTED_NULL_CHARACTER]: `Unexpected null character.`,
  125. [ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG]: "Illegal '/' in tags.",
  126. // Vue-specific parse errors
  127. [ErrorCodes.X_INVALID_END_TAG]: 'Invalid end tag.',
  128. [ErrorCodes.X_MISSING_END_TAG]: 'Element is missing end tag.',
  129. [ErrorCodes.X_MISSING_INTERPOLATION_END]:
  130. 'Interpolation end sign was not found.',
  131. [ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
  132. 'End bracket for dynamic directive argument was not found. ' +
  133. 'Note that dynamic directive argument cannot contain spaces.',
  134. [ErrorCodes.X_MISSING_DIRECTIVE_NAME]: 'Legal directive name was expected.',
  135. // transform errors
  136. [ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
  137. [ErrorCodes.X_V_IF_SAME_KEY]: `v-if/else branches must use unique keys.`,
  138. [ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if or v-else-if.`,
  139. [ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
  140. [ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
  141. [ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT]: `<template v-for> key should be placed on the <template> tag.`,
  142. [ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
  143. [ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
  144. [ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
  145. [ErrorCodes.X_V_SLOT_MIXED_SLOT_USAGE]:
  146. `Mixed v-slot usage on both the component and nested <template>.` +
  147. `When there are multiple named slots, all slots should use <template> ` +
  148. `syntax to avoid scope ambiguity.`,
  149. [ErrorCodes.X_V_SLOT_DUPLICATE_SLOT_NAMES]: `Duplicate slot names found. `,
  150. [ErrorCodes.X_V_SLOT_EXTRANEOUS_DEFAULT_SLOT_CHILDREN]:
  151. `Extraneous children found when component already has explicitly named ` +
  152. `default slot. These children will be ignored.`,
  153. [ErrorCodes.X_V_SLOT_MISPLACED]: `v-slot can only be used on components or <template> tags.`,
  154. [ErrorCodes.X_V_MODEL_NO_EXPRESSION]: `v-model is missing expression.`,
  155. [ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION]: `v-model value must be a valid JavaScript member expression.`,
  156. [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.`,
  157. [ErrorCodes.X_INVALID_EXPRESSION]: `Error parsing JavaScript expression: `,
  158. [ErrorCodes.X_KEEP_ALIVE_INVALID_CHILDREN]: `<KeepAlive> expects exactly one child component.`,
  159. // generic errors
  160. [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
  161. [ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`,
  162. [ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED]: `"cacheHandlers" option is only supported when the "prefixIdentifiers" option is enabled.`,
  163. [ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED]: `"scopeId" option is only supported in module mode.`,
  164. // just to fulfill types
  165. [ErrorCodes.__EXTEND_POINT__]: ``
  166. }