UNPKG

buntis

Version:

A 100% compliant, self-hosted typescript parser that emits an ESTree-compatible abstract syntax tree

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