errors.ts 7.7 KB

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