buntis
Version:
A 100% compliant, self-hosted typescript parser that emits an ESTree-compatible abstract syntax tree
234 lines (229 loc) • 12.1 kB
text/typescript
import { Context, ParserState } from './common';
/*@internal*/
export const enum Errors {
Unexpected,
UnexpectedToken,
Expected,
InvalidCharacter,
InvalidSMPCharacter,
StrictOctalLiteral,
InvalidHexEscapeSequence,
InvalidCodePoint,
InvalidUnicodeEscapeSequence,
InvalidEscapeIdentifier,
ContinuousNumericSeparator,
MissingHexDigits,
MissingOctalDigits,
MissingBinaryDigits,
TrailingNumericSeparator,
MissingExponent,
IDStartAfterNumber,
UnterminatedString,
UnterminatedTemplate,
TemplateOctalLiteral,
StrictOctalEscape,
InvalidEightAndNine,
UnicodeOverflow,
DuplicateRegExpFlag,
UnexpectedTokenRegExpFlag,
UnterminatedRegExp,
RestricedLetProduction,
InvalidCoalescing,
InvalidBigIntLiteral,
MissingCurlyBrace,
SeparatorInZeroPrefixedNumber,
InvalidASCIILineBreak,
InvalidEOFInEscape,
HtmlCommentInWebCompat,
UnterminatedComment,
UnexpectedPropertyOrSignature,
NonStaticConstructorField,
InvalidReadOnlyModifier,
ModifierMustPrecede,
SeenAccessibilityModifier,
AlreadySeenModifier,
AbstractMethodNoAbstractClass,
AbstractModifierNoOthers,
NoClassImplementation,
ReadOnlyModifierNoMethod,
MissingFuncDecl,
NoImplementAbstractAccessor,
UnexpectedPropertyToken,
UnexpectedDefiniteInCtx,
InvalidIndexSignatureModifier,
InvalidIndexSignatureOptional,
InvalidIndexSignatureAnnotation,
IndexSignatureOneParam,
IndexSignatureNoRest,
InvalidParameterProperty,
InvalidStaticOnParam,
ExpressionExpected,
InvalidStaticOnIdxSignature,
InvalidIdxSignatureUnion,
StrictReservedInClass,
ParamPropertyNoConstructor,
InvalidAbstractModifier,
UnexpectedStrictReserved,
InvalidAsyncModifier,
InvalidSuperProperty,
SuperNoConstructor,
InvalidConstructor,
DuplicateConstructor,
InvalidImplmentAmbientContext,
MissingConstructorImplement,
InvalidGeneratorAmbientContext,
EnumInvalidMember,
EnumNumericName,
MissingInitInConstructor,
AccessorWrongArgs,
BadSetterRestParameter,
InvalidNewTarget,
InvalidComputedInterface,
InvalidIndexSignatureParam,
InvalidJSDoc,
EmptyImplementList,
ReqElemFollowOptElem,
NotLastElemInTuple,
DeclNoName,
ExportAssignModifiers,
InvalidAccessedBlockBodyArrow,
InvalidInvokedBlockBodyArrow,
InvalidArrowPostfix,
InvalidImportDeclNotGlobal,
DeclarationMissingInitializer,
ForInOfLoopInitializer,
InvalidThisRefStaticProp,
ExpectedJSXClosingTag,
InvalidJSXAttributeValue,
InvalidNonEmptyJSXExpr,
AdjacentJSXElements,
KeywordNotId,
AwaitOutsideAsync,
DisallowedInContext,
DisallowDecorator,
DecoratorESNext
}
/*@internal*/
export const errorMessages: {
[]: string;
} = {
[]: 'Unexpected',
[]: 'Unexpected token %0',
[]: 'Expected %0',
[]: 'Invalid character',
[]: 'Invalid SMP character',
[]: 'Octal numeric literals and escape characters not allowed in strict mode',
[]: 'Invalid hexadecimal escape sequence',
[]: 'Invalid code point %0',
[]: 'Invalid Unicode escape sequence',
[]: 'Only unicode escapes are legal in identifier names',
[]: 'Only one underscore is allowed as numeric separato',
[]: "Numeric separators '_' are not allowed in numbers that start with '0'",
[]: "Missing hexadecimal digits after '0x'",
[]: "Missing octal digits after '0o'",
[]: "Missing binary digits after '0b'",
[]: 'Numeric separators are not allowed at the end of numeric literals',
[]: 'Non-number found after exponent indicator',
[]: 'Unexpected identifier after numeric literal',
[]: 'Unterminated string literal',
[]: 'Unterminated template literal',
[]: 'Octal escape sequences are not allowed in template strings',
[]: 'Octal escape sequences are not allowed in strict mode',
[]: 'Escapes \\8 or \\9 are not syntactically valid escapes',
[]: 'Unicode escape sequence value is higher than 0x10FFFF',
[]: "Duplicate regular expression flag '%0'",
[]: 'Unexpected regular expression flag',
[]: 'Unterminated regular expression',
[]: 'Invalid BigInt syntax',
[]: 'Expected a closing curly brace `}`',
[]: '`let \n [` is a restricted production at the start of a statement',
[]: 'Invalid unescaped line break in string literal',
[]: 'Reached end of script in the middle of an escape sequence',
[]: 'HTML comments are only allowed with web compability (Annex B)',
[]: 'Multiline comment was not closed properly',
[]:
'Coalescing and logical operators used together in the same expression must be disambiguated with parentheses',
[]: 'Property or signature expected',
[]: "Classes may not have a non-static field named 'constructor'",
[]: "'readonly' type modifier is only permitted on array and tuple literal types",
[]: "%0 modifier must precede '%1' modifier",
[]: 'Accessibility modifier already seen',
[]: '%0 modifier already seen',
[]: 'Member access on super must be in a method',
[]:
'Calls to super must be in the "constructor" method of a class expression or class declaration that has a superclass',
[]: 'Class constructor may not be a %0',
[]: 'Duplicate constructor method in class',
[]: "%0 modifier cannot be used with 'abstract' modifier",
[]: 'Abstract methods can only appear within an abstract class',
[]: 'Current method cannot have an implementation because it is marked abstract',
[]: "'readonly' modifier can only appear on a property declaration or index signature",
[]: 'Function implementation is missing or not immediately following the declaration',
[]: 'An abstract accessor cannot have an implementation',
[]: 'Unexpected token. A constructor, method, accessor, or property was expected',
[]: "'A definite assignment assertion '!' is not permitted in this context",
[]: '%0 modifier cannot appear on an index signature',
[]: 'An index signature parameter cannot have a question mark',
[]: 'An index signature must have a type annotation',
[]: 'An index signature must have exactly one parameter',
[]: 'An index signature cannot have a rest parameter',
[]: "'static' modifier cannot appear on parameter",
[]: 'Expression expected',
[]: "'static' modifier cannot appear on %0",
[]:
'An index signature parameter type cannot be a union type. Consider using a mapped object type instead',
[]:
"'%0' is a reserved word in strict mode. Class definitions are automatically in strict mode",
[]: 'A parameter property is only allowed in a constructor implementation',
[]: "'abstract' modifier can only appear on a class, method, or property declaration",
[]: 'Unexpected strict mode reserved word',
[]: 'Async modifier cannot be used here',
[]: 'An implementation cannot be declared in ambient contexts',
[]: 'Constructor implementation is missing',
[]: 'Generators are not allowed in an ambient context',
[]: 'Enum member expected',
[]: 'An enum member cannot have a numeric name',
[]:
'A parameter initializer is only allowed in a function or constructor implementation',
[]: '%0 functions must have exactly %1 argument%2',
[]: 'Setter function argument must not be a rest parameter',
[]: 'Invalid new target',
[]: "A computed property name must be of type 'string', 'number', 'symbol' or 'any",
[]: "An index signature parameter type must be 'string' or 'number'",
[]: 'JSDoc types can only be used inside documentation comments',
[]: 'Implements list cannot be empty',
[]: 'A required element cannot follow an optional element',
[]: 'A rest element must be last in a tuple type',
[]: '%0 declaration must have a name in this context',
[]: 'An export assignment cannot have modifiers',
[]: 'Block body arrows can not be immediately invoked without a group',
[]: 'Block body arrows can not be immediately accessed without a group',
[]: 'An arrow function can not have a postfix update operator',
[]: 'An import declaration can only be used in a namespace or module',
[]: 'Missing initializer in %0 declaration',
[]: "'for-%0' loop head declarations can not have an initializer",
[]: "'this' cannot be referenced in a static property initializer.",
[]: 'JSX value should be either an expression or a quoted JSX text',
[]: 'Expected corresponding JSX closing tag for %0',
[]: 'Adjacent JSX elements must be wrapped in an enclosing tag',
[]: "JSX attributes must only be assigned a non-empty 'expression'",
[]: 'Invalid keyword',
[]: 'Await is only valid in async functions',
[]: "'%0' may not be used as an identifier in this context",
[]: 'Decorators are not valid here',
[]: 'Decorators are not valid here'
};
export function report(parser: ParserState, _context: Context, type: Errors, _early: 0 | 1, ...params: string[]): void {
const message = errorMessages[type].replace(/%(\d+)/g, (_: string, i: number) => params[i]);
const { index, line, column } = parser;
// if (onError) {
// onError(message, index, line, column, early);
// } else {
const error: any = new SyntaxError(`Line ${line}, column ${column}: ${message}`);
error.index = index;
error.line = line;
error.column = column;
error.description = message;
throw error;
//}
}