errors.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { SourceLocation } from './ast'
  2. export interface CompilerError extends SyntaxError {
  3. code: ErrorCodes
  4. loc?: SourceLocation
  5. }
  6. export function defaultOnError(error: CompilerError) {
  7. throw error
  8. }
  9. export function createCompilerError(
  10. code: ErrorCodes,
  11. loc?: SourceLocation
  12. ): CompilerError {
  13. const msg = __DEV__ || !__BROWSER__ ? errorMessages[code] : code
  14. const locInfo = loc ? ` (${loc.start.line}:${loc.start.column})` : ``
  15. const error = new SyntaxError(msg + locInfo) as CompilerError
  16. error.code = code
  17. error.loc = loc
  18. return error
  19. }
  20. export const enum ErrorCodes {
  21. // parse errors
  22. ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  23. ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
  24. CDATA_IN_HTML_CONTENT,
  25. CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE,
  26. CONTROL_CHARACTER_REFERENCE,
  27. DUPLICATE_ATTRIBUTE,
  28. END_TAG_WITH_ATTRIBUTES,
  29. END_TAG_WITH_TRAILING_SOLIDUS,
  30. EOF_BEFORE_TAG_NAME,
  31. EOF_IN_CDATA,
  32. EOF_IN_COMMENT,
  33. EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  34. EOF_IN_TAG,
  35. INCORRECTLY_CLOSED_COMMENT,
  36. INCORRECTLY_OPENED_COMMENT,
  37. INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  38. MISSING_ATTRIBUTE_VALUE,
  39. MISSING_END_TAG_NAME,
  40. MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE,
  41. MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  42. NESTED_COMMENT,
  43. NONCHARACTER_CHARACTER_REFERENCE,
  44. NULL_CHARACTER_REFERENCE,
  45. SURROGATE_CHARACTER_REFERENCE,
  46. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  47. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  48. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  49. UNEXPECTED_NULL_CHARACTER,
  50. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  51. UNEXPECTED_SOLIDUS_IN_TAG,
  52. UNKNOWN_NAMED_CHARACTER_REFERENCE,
  53. // Vue-specific parse errors
  54. X_INVALID_END_TAG,
  55. X_MISSING_END_TAG,
  56. X_MISSING_INTERPOLATION_END,
  57. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  58. // transform errors
  59. X_IF_NO_EXPRESSION,
  60. X_ELSE_NO_ADJACENT_IF,
  61. X_FOR_NO_EXPRESSION,
  62. X_FOR_MALFORMED_EXPRESSION,
  63. X_V_BIND_NO_EXPRESSION,
  64. X_V_ON_NO_EXPRESSION,
  65. X_V_HTML_NO_EXPRESSION,
  66. X_V_HTML_WITH_CHILDREN,
  67. X_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
  68. X_NAMED_SLOT_ON_COMPONENT,
  69. X_MIXED_SLOT_USAGE,
  70. X_DUPLICATE_SLOT_NAMES,
  71. X_EXTRANEOUS_NON_SLOT_CHILDREN,
  72. X_MISPLACED_V_SLOT,
  73. // generic errors
  74. X_PREFIX_ID_NOT_SUPPORTED,
  75. X_MODULE_MODE_NOT_SUPPORTED
  76. }
  77. export const errorMessages: { [code: number]: string } = {
  78. // parse errors
  79. [ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT]: 'Illegal comment.',
  80. [ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE]:
  81. 'Illegal numeric character reference: invalid character.',
  82. [ErrorCodes.CDATA_IN_HTML_CONTENT]:
  83. 'CDATA section is allowed only in XML context.',
  84. [ErrorCodes.CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE]:
  85. 'Illegal numeric character reference: too big.',
  86. [ErrorCodes.CONTROL_CHARACTER_REFERENCE]:
  87. 'Illegal numeric character reference: control character.',
  88. [ErrorCodes.DUPLICATE_ATTRIBUTE]: 'Duplicate attribute.',
  89. [ErrorCodes.END_TAG_WITH_ATTRIBUTES]: 'End tag cannot have attributes.',
  90. [ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS]: "Illegal '/' in tags.",
  91. [ErrorCodes.EOF_BEFORE_TAG_NAME]: 'Unexpected EOF in tag.',
  92. [ErrorCodes.EOF_IN_CDATA]: 'Unexpected EOF in CDATA section.',
  93. [ErrorCodes.EOF_IN_COMMENT]: 'Unexpected EOF in comment.',
  94. [ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT]:
  95. 'Unexpected EOF in script.',
  96. [ErrorCodes.EOF_IN_TAG]: 'Unexpected EOF in tag.',
  97. [ErrorCodes.INCORRECTLY_CLOSED_COMMENT]: 'Incorrectly closed comment.',
  98. [ErrorCodes.INCORRECTLY_OPENED_COMMENT]: 'Incorrectly opened comment.',
  99. [ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME]:
  100. "Illegal tag name. Use '&lt;' to print '<'.",
  101. [ErrorCodes.MISSING_ATTRIBUTE_VALUE]: 'Attribute value was expected.',
  102. [ErrorCodes.MISSING_END_TAG_NAME]: 'End tag name was expected.',
  103. [ErrorCodes.MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE]:
  104. 'Semicolon was expected.',
  105. [ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES]:
  106. 'Whitespace was expected.',
  107. [ErrorCodes.NESTED_COMMENT]: "Unexpected '<!--' in comment.",
  108. [ErrorCodes.NONCHARACTER_CHARACTER_REFERENCE]:
  109. 'Illegal numeric character reference: non character.',
  110. [ErrorCodes.NULL_CHARACTER_REFERENCE]:
  111. 'Illegal numeric character reference: null character.',
  112. [ErrorCodes.SURROGATE_CHARACTER_REFERENCE]:
  113. 'Illegal numeric character reference: non-pair surrogate.',
  114. [ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME]:
  115. 'Attribute name cannot contain U+0022 ("), U+0027 (\'), and U+003C (<).',
  116. [ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE]:
  117. 'Unquoted attribute value cannot contain U+0022 ("), U+0027 (\'), U+003C (<), U+003D (=), and U+0060 (`).',
  118. [ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME]:
  119. "Attribute name cannot start with '='.",
  120. [ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME]:
  121. "'<?' is allowed only in XML context.",
  122. [ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG]: "Illegal '/' in tags.",
  123. [ErrorCodes.UNKNOWN_NAMED_CHARACTER_REFERENCE]: 'Unknown entity name.',
  124. // Vue-specific parse errors
  125. [ErrorCodes.X_INVALID_END_TAG]: 'Invalid end tag.',
  126. [ErrorCodes.X_MISSING_END_TAG]: 'End tag was not found.',
  127. [ErrorCodes.X_MISSING_INTERPOLATION_END]:
  128. 'Interpolation end sign was not found.',
  129. [ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
  130. 'End bracket for dynamic directive argument was not found. ' +
  131. 'Note that dynamic directive argument cannot contain spaces.',
  132. // transform errors
  133. [ErrorCodes.X_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
  134. [ErrorCodes.X_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if.`,
  135. [ErrorCodes.X_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
  136. [ErrorCodes.X_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
  137. [ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
  138. [ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
  139. [ErrorCodes.X_V_HTML_NO_EXPRESSION]: `v-html is missing epxression.`,
  140. [ErrorCodes.X_V_HTML_WITH_CHILDREN]: `v-html will override element children.`,
  141. [ErrorCodes.X_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
  142. [ErrorCodes.X_NAMED_SLOT_ON_COMPONENT]:
  143. `Named v-slot on component. ` +
  144. `Named slots should use <template v-slot> syntax nested inside the component.`,
  145. [ErrorCodes.X_MIXED_SLOT_USAGE]:
  146. `Mixed v-slot usage on both the component and nested <template>.` +
  147. `The default slot should also use <template> syntax when there are other ` +
  148. `named slots to avoid scope ambiguity.`,
  149. [ErrorCodes.X_DUPLICATE_SLOT_NAMES]: `Duplicate slot names found. `,
  150. [ErrorCodes.X_EXTRANEOUS_NON_SLOT_CHILDREN]:
  151. `Extraneous children found when component has explicit slots. ` +
  152. `These children will be ignored.`,
  153. [ErrorCodes.X_MISPLACED_V_SLOT]: `v-slot can only be used on components or <template> tags.`,
  154. // generic errors
  155. [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
  156. [ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`
  157. }