From 9c0f77091c0b956282a33e101ec6c9173a9c201e Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 7 Mar 2017 06:42:50 -0800 Subject: [PATCH 1/3] 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 { From 18f283f68adbeeca78d70c6c7474eda46133a545 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 16 Mar 2017 14:43:56 -0700 Subject: [PATCH 2/3] Add more missing quotes --- src/compiler/parser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6db26355d92..f5c4d880072 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); From 6234cbb82f18cb7d8634f33dfd8c37139c7d0e2e Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 17 Mar 2017 14:04:36 -0700 Subject: [PATCH 3/3] Move comment to JSDoc --- src/compiler/parser.ts | 3 --- src/compiler/types.ts | 5 +++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index f5c4d880072..5d585e2658a 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5643,9 +5643,6 @@ namespace ts { } 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; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7728c2b8519..a7adf28ac8e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1838,6 +1838,11 @@ namespace ts { export type ModuleReference = EntityName | ExternalModuleReference; + /** + * One of: + * - import x = require("mod"); + * - import x = M.x; + */ export interface ImportEqualsDeclaration extends DeclarationStatement { kind: SyntaxKind.ImportEqualsDeclaration; parent?: SourceFile | ModuleBlock;