2019-09-23 02:19:42 +00:00
|
|
|
import { SourceLocation } from './ast'
|
2019-09-17 23:08:47 +00:00
|
|
|
|
|
|
|
export interface CompilerError extends SyntaxError {
|
|
|
|
code: ErrorCodes
|
2019-09-23 17:25:18 +00:00
|
|
|
loc?: SourceLocation
|
2019-09-17 23:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 16:16:19 +00:00
|
|
|
export function defaultOnError(error: CompilerError) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2019-09-17 23:08:47 +00:00
|
|
|
export function createCompilerError(
|
|
|
|
code: ErrorCodes,
|
2019-09-23 17:25:18 +00:00
|
|
|
loc?: SourceLocation
|
2019-09-17 23:08:47 +00:00
|
|
|
): CompilerError {
|
2019-09-23 17:25:18 +00:00
|
|
|
const msg = __DEV__ || !__BROWSER__ ? errorMessages[code] : code
|
|
|
|
const locInfo = loc ? ` (${loc.start.line}:${loc.start.column})` : ``
|
|
|
|
const error = new SyntaxError(msg + locInfo) as CompilerError
|
2019-09-17 23:08:47 +00:00
|
|
|
error.code = code
|
|
|
|
error.loc = loc
|
|
|
|
return error
|
|
|
|
}
|
|
|
|
|
|
|
|
export const enum ErrorCodes {
|
|
|
|
// parse errors
|
|
|
|
ABRUPT_CLOSING_OF_EMPTY_COMMENT,
|
|
|
|
ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
|
|
|
|
CDATA_IN_HTML_CONTENT,
|
|
|
|
CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE,
|
|
|
|
CONTROL_CHARACTER_REFERENCE,
|
|
|
|
DUPLICATE_ATTRIBUTE,
|
|
|
|
END_TAG_WITH_ATTRIBUTES,
|
|
|
|
END_TAG_WITH_TRAILING_SOLIDUS,
|
|
|
|
EOF_BEFORE_TAG_NAME,
|
|
|
|
EOF_IN_CDATA,
|
|
|
|
EOF_IN_COMMENT,
|
|
|
|
EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
|
|
|
|
EOF_IN_TAG,
|
|
|
|
INCORRECTLY_CLOSED_COMMENT,
|
|
|
|
INCORRECTLY_OPENED_COMMENT,
|
|
|
|
INVALID_FIRST_CHARACTER_OF_TAG_NAME,
|
|
|
|
MISSING_ATTRIBUTE_VALUE,
|
|
|
|
MISSING_END_TAG_NAME,
|
|
|
|
MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE,
|
|
|
|
MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
|
|
|
|
NESTED_COMMENT,
|
|
|
|
NONCHARACTER_CHARACTER_REFERENCE,
|
|
|
|
NULL_CHARACTER_REFERENCE,
|
|
|
|
SURROGATE_CHARACTER_REFERENCE,
|
|
|
|
UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
|
|
|
|
UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
|
|
|
|
UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
|
|
|
|
UNEXPECTED_NULL_CHARACTER,
|
|
|
|
UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
|
|
|
|
UNEXPECTED_SOLIDUS_IN_TAG,
|
|
|
|
UNKNOWN_NAMED_CHARACTER_REFERENCE,
|
2019-09-23 17:25:18 +00:00
|
|
|
|
|
|
|
// Vue-specific parse errors
|
2019-09-17 23:08:47 +00:00
|
|
|
X_INVALID_END_TAG,
|
|
|
|
X_MISSING_END_TAG,
|
|
|
|
X_MISSING_INTERPOLATION_END,
|
|
|
|
X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
|
|
|
|
|
|
|
|
// transform errors
|
2019-10-09 14:27:24 +00:00
|
|
|
X_V_IF_NO_EXPRESSION,
|
|
|
|
X_V_ELSE_NO_ADJACENT_IF,
|
|
|
|
X_V_FOR_NO_EXPRESSION,
|
|
|
|
X_V_FOR_MALFORMED_EXPRESSION,
|
2019-09-23 17:25:18 +00:00
|
|
|
X_V_BIND_NO_EXPRESSION,
|
2019-09-25 00:51:48 +00:00
|
|
|
X_V_ON_NO_EXPRESSION,
|
2019-10-08 19:35:57 +00:00
|
|
|
X_V_HTML_NO_EXPRESSION,
|
|
|
|
X_V_HTML_WITH_CHILDREN,
|
2019-10-09 14:27:24 +00:00
|
|
|
X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
|
|
|
|
X_V_SLOT_NAMED_SLOT_ON_COMPONENT,
|
|
|
|
X_V_SLOT_MIXED_SLOT_USAGE,
|
|
|
|
X_V_SLOT_DUPLICATE_SLOT_NAMES,
|
|
|
|
X_V_SLOT_EXTRANEOUS_NON_SLOT_CHILDREN,
|
|
|
|
X_V_SLOT_MISPLACED,
|
2019-09-23 17:25:18 +00:00
|
|
|
|
|
|
|
// generic errors
|
2019-09-26 02:29:37 +00:00
|
|
|
X_PREFIX_ID_NOT_SUPPORTED,
|
|
|
|
X_MODULE_MODE_NOT_SUPPORTED
|
2019-09-17 23:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const errorMessages: { [code: number]: string } = {
|
|
|
|
// parse errors
|
|
|
|
[ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT]: 'Illegal comment.',
|
|
|
|
[ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE]:
|
|
|
|
'Illegal numeric character reference: invalid character.',
|
|
|
|
[ErrorCodes.CDATA_IN_HTML_CONTENT]:
|
|
|
|
'CDATA section is allowed only in XML context.',
|
|
|
|
[ErrorCodes.CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE]:
|
|
|
|
'Illegal numeric character reference: too big.',
|
|
|
|
[ErrorCodes.CONTROL_CHARACTER_REFERENCE]:
|
|
|
|
'Illegal numeric character reference: control character.',
|
|
|
|
[ErrorCodes.DUPLICATE_ATTRIBUTE]: 'Duplicate attribute.',
|
|
|
|
[ErrorCodes.END_TAG_WITH_ATTRIBUTES]: 'End tag cannot have attributes.',
|
|
|
|
[ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS]: "Illegal '/' in tags.",
|
|
|
|
[ErrorCodes.EOF_BEFORE_TAG_NAME]: 'Unexpected EOF in tag.',
|
|
|
|
[ErrorCodes.EOF_IN_CDATA]: 'Unexpected EOF in CDATA section.',
|
|
|
|
[ErrorCodes.EOF_IN_COMMENT]: 'Unexpected EOF in comment.',
|
|
|
|
[ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT]:
|
|
|
|
'Unexpected EOF in script.',
|
|
|
|
[ErrorCodes.EOF_IN_TAG]: 'Unexpected EOF in tag.',
|
|
|
|
[ErrorCodes.INCORRECTLY_CLOSED_COMMENT]: 'Incorrectly closed comment.',
|
|
|
|
[ErrorCodes.INCORRECTLY_OPENED_COMMENT]: 'Incorrectly opened comment.',
|
|
|
|
[ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME]:
|
|
|
|
"Illegal tag name. Use '<' to print '<'.",
|
|
|
|
[ErrorCodes.MISSING_ATTRIBUTE_VALUE]: 'Attribute value was expected.',
|
|
|
|
[ErrorCodes.MISSING_END_TAG_NAME]: 'End tag name was expected.',
|
|
|
|
[ErrorCodes.MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE]:
|
|
|
|
'Semicolon was expected.',
|
|
|
|
[ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES]:
|
|
|
|
'Whitespace was expected.',
|
|
|
|
[ErrorCodes.NESTED_COMMENT]: "Unexpected '<!--' in comment.",
|
|
|
|
[ErrorCodes.NONCHARACTER_CHARACTER_REFERENCE]:
|
|
|
|
'Illegal numeric character reference: non character.',
|
|
|
|
[ErrorCodes.NULL_CHARACTER_REFERENCE]:
|
|
|
|
'Illegal numeric character reference: null character.',
|
|
|
|
[ErrorCodes.SURROGATE_CHARACTER_REFERENCE]:
|
|
|
|
'Illegal numeric character reference: non-pair surrogate.',
|
|
|
|
[ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME]:
|
|
|
|
'Attribute name cannot contain U+0022 ("), U+0027 (\'), and U+003C (<).',
|
|
|
|
[ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE]:
|
|
|
|
'Unquoted attribute value cannot contain U+0022 ("), U+0027 (\'), U+003C (<), U+003D (=), and U+0060 (`).',
|
|
|
|
[ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME]:
|
|
|
|
"Attribute name cannot start with '='.",
|
|
|
|
[ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME]:
|
|
|
|
"'<?' is allowed only in XML context.",
|
|
|
|
[ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG]: "Illegal '/' in tags.",
|
|
|
|
[ErrorCodes.UNKNOWN_NAMED_CHARACTER_REFERENCE]: 'Unknown entity name.',
|
2019-09-23 17:25:18 +00:00
|
|
|
|
|
|
|
// Vue-specific parse errors
|
2019-09-17 23:08:47 +00:00
|
|
|
[ErrorCodes.X_INVALID_END_TAG]: 'Invalid end tag.',
|
|
|
|
[ErrorCodes.X_MISSING_END_TAG]: 'End tag was not found.',
|
|
|
|
[ErrorCodes.X_MISSING_INTERPOLATION_END]:
|
|
|
|
'Interpolation end sign was not found.',
|
2019-09-23 17:25:18 +00:00
|
|
|
[ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
|
2019-09-27 15:42:02 +00:00
|
|
|
'End bracket for dynamic directive argument was not found. ' +
|
2019-10-05 14:35:19 +00:00
|
|
|
'Note that dynamic directive argument cannot contain spaces.',
|
2019-09-17 23:08:47 +00:00
|
|
|
|
|
|
|
// transform errors
|
2019-10-09 14:27:24 +00:00
|
|
|
[ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
|
|
|
|
[ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if.`,
|
|
|
|
[ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
|
|
|
|
[ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
|
2019-09-28 02:25:32 +00:00
|
|
|
[ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
|
|
|
|
[ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
|
2019-10-08 19:35:57 +00:00
|
|
|
[ErrorCodes.X_V_HTML_NO_EXPRESSION]: `v-html is missing epxression.`,
|
|
|
|
[ErrorCodes.X_V_HTML_WITH_CHILDREN]: `v-html will override element children.`,
|
2019-10-09 14:27:24 +00:00
|
|
|
[ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET]: `Unexpected custom directive on <slot> outlet.`,
|
|
|
|
[ErrorCodes.X_V_SLOT_NAMED_SLOT_ON_COMPONENT]:
|
2019-09-28 02:25:32 +00:00
|
|
|
`Named v-slot on component. ` +
|
|
|
|
`Named slots should use <template v-slot> syntax nested inside the component.`,
|
2019-10-09 14:27:24 +00:00
|
|
|
[ErrorCodes.X_V_SLOT_MIXED_SLOT_USAGE]:
|
2019-09-28 02:25:32 +00:00
|
|
|
`Mixed v-slot usage on both the component and nested <template>.` +
|
|
|
|
`The default slot should also use <template> syntax when there are other ` +
|
|
|
|
`named slots to avoid scope ambiguity.`,
|
2019-10-09 14:27:24 +00:00
|
|
|
[ErrorCodes.X_V_SLOT_DUPLICATE_SLOT_NAMES]: `Duplicate slot names found. `,
|
|
|
|
[ErrorCodes.X_V_SLOT_EXTRANEOUS_NON_SLOT_CHILDREN]:
|
2019-09-28 02:25:32 +00:00
|
|
|
`Extraneous children found when component has explicit slots. ` +
|
|
|
|
`These children will be ignored.`,
|
2019-10-09 14:27:24 +00:00
|
|
|
[ErrorCodes.X_V_SLOT_MISPLACED]: `v-slot can only be used on components or <template> tags.`,
|
2019-09-28 18:05:10 +00:00
|
|
|
|
2019-09-23 17:25:18 +00:00
|
|
|
// generic errors
|
2019-09-26 02:29:37 +00:00
|
|
|
[ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
|
|
|
|
[ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`
|
2019-09-17 23:08:47 +00:00
|
|
|
}
|