From 9c0f77091c0b956282a33e101ec6c9173a9c201e Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 7 Mar 2017 06:42:50 -0800 Subject: [PATCH] Clean up code in parser --- src/compiler/parser.ts | 52 +++++++++++++++++++++++------------------- src/compiler/types.ts | 3 +-- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 308d3a317f9..6db26355d92 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1309,7 +1309,7 @@ namespace ts { case ParsingContext.ObjectBindingElements: return token() === SyntaxKind.OpenBracketToken || token() === SyntaxKind.DotDotDotToken || isLiteralPropertyName(); case ParsingContext.HeritageClauseElement: - // If we see { } then only consume it as an expression if it is followed by , or { + // If we see { } then only consume it as an expression if it is followed by `,` or `{` // That way we won't consume the body of a class in its heritage clause. if (token() === SyntaxKind.OpenBraceToken) { return lookAhead(isValidHeritageClauseObjectLiteral); @@ -2113,7 +2113,7 @@ namespace ts { return finishNode(node); } - function parseTypeParameters(): NodeArray { + function parseTypeParameters(): NodeArray | undefined { if (token() === SyntaxKind.LessThanToken) { return parseBracketedList(ParsingContext.TypeParameters, parseTypeParameter, SyntaxKind.LessThanToken, SyntaxKind.GreaterThanToken); } @@ -2183,7 +2183,7 @@ namespace ts { } function fillSignature( - returnToken: SyntaxKind, + returnToken: SyntaxKind.ColonToken | SyntaxKind.EqualsGreaterThanToken, yieldContext: boolean, awaitContext: boolean, requireCompleteParameterList: boolean, @@ -2373,14 +2373,14 @@ namespace ts { } function isTypeMemberStart(): boolean { - let idToken: SyntaxKind; // Return true if we have the start of a signature member if (token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.LessThanToken) { return true; } + let idToken: boolean; // Eat up all modifiers, but hold on to the last one in case it is actually an identifier while (isModifierKind(token())) { - idToken = token(); + idToken = true; nextToken(); } // Index signatures and computed property names are type members @@ -2389,7 +2389,7 @@ namespace ts { } // Try to get the first property-like token following all modifiers if (isLiteralPropertyName()) { - idToken = token(); + idToken = true; nextToken(); } // If we were able to get any potential identifier, check that it is @@ -2497,7 +2497,7 @@ namespace ts { return finishNode(node); } - function parseKeywordAndNoDot(): TypeNode { + function parseKeywordAndNoDot(): TypeNode | undefined { const node = parseTokenNode(); return token() === SyntaxKind.DotToken ? undefined : node; } @@ -2635,7 +2635,7 @@ namespace ts { return parseArrayTypeOrHigher(); } - function parseUnionOrIntersectionType(kind: SyntaxKind, parseConstituentType: () => TypeNode, operator: SyntaxKind): TypeNode { + function parseUnionOrIntersectionType(kind: SyntaxKind.UnionType | SyntaxKind.IntersectionType, parseConstituentType: () => TypeNode, operator: SyntaxKind.BarToken | SyntaxKind.AmpersandToken): TypeNode { parseOptional(operator); let type = parseConstituentType(); if (token() === operator) { @@ -5281,8 +5281,8 @@ namespace ts { * * In such situations, 'permitInvalidConstAsModifier' should be set to true. */ - function parseModifiers(permitInvalidConstAsModifier?: boolean): NodeArray { - let modifiers: NodeArray; + function parseModifiers(permitInvalidConstAsModifier?: boolean): NodeArray | undefined { + let modifiers: NodeArray | undefined; while (true) { const modifierStart = scanner.getStartPos(); const modifierKind = token(); @@ -5422,7 +5422,7 @@ namespace ts { return token() === SyntaxKind.ImplementsKeyword && lookAhead(nextTokenIsIdentifierOrKeyword); } - function parseHeritageClauses(): NodeArray { + function parseHeritageClauses(): NodeArray | undefined { // ClassTail[Yield,Await] : (Modified) See 14.5 // ClassHeritage[?Yield,?Await]opt { ClassBody[?Yield,?Await]opt } @@ -5433,7 +5433,7 @@ namespace ts { return undefined; } - function parseHeritageClause() { + function parseHeritageClause(): HeritageClause | undefined { if (token() === SyntaxKind.ExtendsKeyword || token() === SyntaxKind.ImplementsKeyword) { const node = createNode(SyntaxKind.HeritageClause); node.token = token(); @@ -5459,7 +5459,7 @@ namespace ts { return token() === SyntaxKind.ExtendsKeyword || token() === SyntaxKind.ImplementsKeyword; } - function parseClassMembers() { + function parseClassMembers(): NodeArray { return parseList(ParsingContext.ClassMembers, parseClassElement); } @@ -5618,17 +5618,7 @@ namespace ts { if (isIdentifier()) { identifier = parseIdentifier(); if (token() !== SyntaxKind.CommaToken && token() !== SyntaxKind.FromKeyword) { - // ImportEquals declaration of type: - // import x = require("mod"); or - // import x = M.x; - const importEqualsDeclaration = createNode(SyntaxKind.ImportEqualsDeclaration, fullStart); - importEqualsDeclaration.decorators = decorators; - importEqualsDeclaration.modifiers = modifiers; - importEqualsDeclaration.name = identifier; - parseExpected(SyntaxKind.EqualsToken); - importEqualsDeclaration.moduleReference = parseModuleReference(); - parseSemicolon(); - return addJSDocComment(finishNode(importEqualsDeclaration)); + return parseImportEqualsDeclaration(fullStart, decorators, modifiers, identifier); } } @@ -5652,6 +5642,20 @@ namespace ts { return finishNode(importDeclaration); } + function parseImportEqualsDeclaration(fullStart: number, decorators: NodeArray, modifiers: NodeArray, identifier: ts.Identifier): ImportEqualsDeclaration { + // ImportEquals declaration of type: + // import x = require("mod"); or + // import x = M.x; + const importEqualsDeclaration = createNode(SyntaxKind.ImportEqualsDeclaration, fullStart); + importEqualsDeclaration.decorators = decorators; + importEqualsDeclaration.modifiers = modifiers; + importEqualsDeclaration.name = identifier; + parseExpected(SyntaxKind.EqualsToken); + importEqualsDeclaration.moduleReference = parseModuleReference(); + parseSemicolon(); + return addJSDocComment(finishNode(importEqualsDeclaration)); + } + function parseImportClause(identifier: Identifier, fullStart: number) { // ImportClause: // ImportedDefaultBinding diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 86895f3db6e..7728c2b8519 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1813,7 +1813,7 @@ namespace ts { kind: SyntaxKind.ModuleDeclaration; parent?: ModuleBody | SourceFile; name: ModuleName; - body?: ModuleBody | JSDocNamespaceDeclaration | Identifier; + body?: ModuleBody | JSDocNamespaceDeclaration; } export type NamespaceBody = ModuleBlock | NamespaceDeclaration; @@ -1889,7 +1889,6 @@ namespace ts { export interface NamespaceExportDeclaration extends DeclarationStatement { kind: SyntaxKind.NamespaceExportDeclaration; name: Identifier; - moduleReference: LiteralLikeNode; } export interface ExportDeclaration extends DeclarationStatement {