errors.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { SourceLocation } from './ast'
  2. export interface CompilerError extends SyntaxError {
  3. code: number
  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 createCompilerError<T extends number>(
  13. code: T,
  14. loc?: SourceLocation,
  15. messages?: { [code: number]: string }
  16. ): T extends ErrorCodes ? CoreCompilerError : CompilerError {
  17. const msg = __DEV__ || !__BROWSER__ ? (messages || errorMessages)[code] : code
  18. const error = new SyntaxError(String(msg)) as CompilerError
  19. error.code = code
  20. error.loc = loc
  21. return error as any
  22. }
  23. export const enum ErrorCodes {
  24. // parse errors
  25. ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  26. ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
  27. CDATA_IN_HTML_CONTENT,
  28. CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE,
  29. CONTROL_CHARACTER_REFERENCE,
  30. DUPLICATE_ATTRIBUTE,
  31. END_TAG_WITH_ATTRIBUTES,
  32. END_TAG_WITH_TRAILING_SOLIDUS,
  33. EOF_BEFORE_TAG_NAME,
  34. EOF_IN_CDATA,
  35. EOF_IN_COMMENT,
  36. EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  37. EOF_IN_TAG,
  38. INCORRECTLY_CLOSED_COMMENT,
  39. INCORRECTLY_OPENED_COMMENT,
  40. INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  41. MISSING_ATTRIBUTE_VALUE,
  42. MISSING_END_TAG_NAME,
  43. MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE,
  44. MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  45. NESTED_COMMENT,
  46. NONCHARACTER_CHARACTER_REFERENCE,
  47. NULL_CHARACTER_REFERENCE,
  48. SURROGATE_CHARACTER_REFERENCE,
  49. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  50. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  51. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  52. UNEXPECTED_NULL_CHARACTER,
  53. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  54. UNEXPECTED_SOLIDUS_IN_TAG,
  55. UNKNOWN_NAMED_CHARACTER_REFERENCE,
  56. // Vue-specific parse errors
  57. X_INVALID_END_TAG,
  58. X_MISSING_END_TAG,
  59. X_MISSING_INTERPOLATION_END,
  60. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  61. // transform errors
  62. X_V_IF_NO_EXPRESSION,
  63. X_V_ELSE_NO_ADJACENT_IF,
  64. X_V_FOR_NO_EXPRESSION,
  65. X_V_FOR_MALFORMED_EXPRESSION,
  66. X_V_BIND_NO_EXPRESSION,
  67. X_V_ON_NO_EXPRESSION,
  68. X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
  69. X_V_SLOT_NAMED_SLOT_ON_COMPONENT,
  70. X_V_SLOT_MIXED_SLOT_USAGE,
  71. X_V_SLOT_DUPLICATE_SLOT_NAMES,
  72. X_V_SLOT_EXTRANEOUS_DEFAULT_SLOT_CHILDREN,
  73. X_V_SLOT_MISPLACED,
  74. X_V_MODEL_NO_EXPRESSION,
  75. X_V_MODEL_MALFORMED_EXPRESSION,
  76. X_V_MODEL_ON_SCOPE_VARIABLE,
  77. X_INVALID_EXPRESSION,
  78. // generic errors
  79. X_PREFIX_ID_NOT_SUPPORTED,
  80. X_MODULE_MODE_NOT_SUPPORTED,
  81. X_CACHE_HANDLER_NOT_SUPPORTED,
  82. X_SCOPE_ID_NOT_SUPPORTED,
  83. // Special value for higher-order compilers to pick up the last code
  84. // to avoid collision of error codes. This should always be kept as the last
  85. // item.
  86. __EXTEND_POINT__
  87. }
  88. export const errorMessages: { [code: number]: string } = {
  89. // parse errors
  90. [ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT]: 'Illegal comment.',
  91. [ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE]:
  92. 'Illegal numeric character reference: invalid character.',
  93. [ErrorCodes.CDATA_IN_HTML_CONTENT]:
  94. 'CDATA section is allowed only in XML context.',
  95. [ErrorCodes.CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE]:
  96. 'Illegal numeric character reference: too big.',
  97. [ErrorCodes.CONTROL_CHARACTER_REFERENCE]:
  98. 'Illegal numeric character reference: control character.',
  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_SEMICOLON_AFTER_CHARACTER_REFERENCE]:
  115. 'Semicolon was expected.',
  116. [ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES]:
  117. 'Whitespace was expected.',
  118. [ErrorCodes.NESTED_COMMENT]: "Unexpected '<!--' in comment.",
  119. [ErrorCodes.NONCHARACTER_CHARACTER_REFERENCE]:
  120. 'Illegal numeric character reference: non character.',
  121. [ErrorCodes.NULL_CHARACTER_REFERENCE]:
  122. 'Illegal numeric character reference: null character.',
  123. [ErrorCodes.SURROGATE_CHARACTER_REFERENCE]:
  124. 'Illegal numeric character reference: non-pair surrogate.',
  125. [ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME]:
  126. 'Attribute name cannot contain U+0022 ("), U+0027 (\'), and U+003C (<).',
  127. [ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE]:
  128. 'Unquoted attribute value cannot contain U+0022 ("), U+0027 (\'), U+003C (<), U+003D (=), and U+0060 (`).',
  129. [ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME]:
  130. "Attribute name cannot start with '='.",
  131. [ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME]:
  132. "'<?' is allowed only in XML context.",
  133. [ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG]: "Illegal '/' in tags.",
  134. [ErrorCodes.UNKNOWN_NAMED_CHARACTER_REFERENCE]: 'Unknown entity name.',
  135. // Vue-specific parse errors
  136. [ErrorCodes.X_INVALID_END_TAG]: 'Invalid end tag.',
  137. [ErrorCodes.X_MISSING_END_TAG]: 'Element is missing end tag.',
  138. [ErrorCodes.X_MISSING_INTERPOLATION_END]:
  139. 'Interpolation end sign was not found.',
  140. [ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
  141. 'End bracket for dynamic directive argument was not found. ' +
  142. 'Note that dynamic directive argument cannot contain spaces.',
  143. // transform errors
  144. [ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
  145. [ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if.`,
  146. [ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
  147. [ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
  148. [ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
  149. [ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
  150. [ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
  151. [ErrorCodes.X_V_SLOT_NAMED_SLOT_ON_COMPONENT]:
  152. `Named v-slot on component. ` +
  153. `Named slots should use <template v-slot> syntax nested inside the component.`,
  154. [ErrorCodes.X_V_SLOT_MIXED_SLOT_USAGE]:
  155. `Mixed v-slot usage on both the component and nested <template>.` +
  156. `The default slot should also use <template> syntax when there are other ` +
  157. `named slots to avoid scope ambiguity.`,
  158. [ErrorCodes.X_V_SLOT_DUPLICATE_SLOT_NAMES]: `Duplicate slot names found. `,
  159. [ErrorCodes.X_V_SLOT_EXTRANEOUS_DEFAULT_SLOT_CHILDREN]:
  160. `Extraneous children found when component already has explicitly named ` +
  161. `default slot. These children will be ignored.`,
  162. [ErrorCodes.X_V_SLOT_MISPLACED]: `v-slot can only be used on components or <template> tags.`,
  163. [ErrorCodes.X_V_MODEL_NO_EXPRESSION]: `v-model is missing expression.`,
  164. [ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION]: `v-model value must be a valid JavaScript member expression.`,
  165. [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.`,
  166. [ErrorCodes.X_INVALID_EXPRESSION]: `Invalid JavaScript expression.`,
  167. // generic errors
  168. [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
  169. [ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`,
  170. [ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED]: `"cacheHandlers" option is only supported when the "prefixIdentifiers" option is enabled.`,
  171. [ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED]: `"scopeId" option is only supported in module mode.`
  172. }