Skip to content

Commit

Permalink
Merge pull request microsoft#14690 from Microsoft/clean_up_parser
Browse files Browse the repository at this point in the history
Clean up code in parser
  • Loading branch information
Andy authored Mar 17, 2017
2 parents 230e1da + 6234cbb commit ac67b94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
49 changes: 25 additions & 24 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2113,7 +2113,7 @@ namespace ts {
return finishNode(node);
}

function parseTypeParameters(): NodeArray<TypeParameterDeclaration> {
function parseTypeParameters(): NodeArray<TypeParameterDeclaration> | undefined {
if (token() === SyntaxKind.LessThanToken) {
return parseBracketedList(ParsingContext.TypeParameters, parseTypeParameter, SyntaxKind.LessThanToken, SyntaxKind.GreaterThanToken);
}
Expand Down Expand Up @@ -2183,7 +2183,7 @@ namespace ts {
}

function fillSignature(
returnToken: SyntaxKind,
returnToken: SyntaxKind.ColonToken | SyntaxKind.EqualsGreaterThanToken,
yieldContext: boolean,
awaitContext: boolean,
requireCompleteParameterList: boolean,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -2497,7 +2497,7 @@ namespace ts {
return finishNode(node);
}

function parseKeywordAndNoDot(): TypeNode {
function parseKeywordAndNoDot(): TypeNode | undefined {
const node = parseTokenNode<TypeNode>();
return token() === SyntaxKind.DotToken ? undefined : node;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -5281,8 +5281,8 @@ namespace ts {
*
* In such situations, 'permitInvalidConstAsModifier' should be set to true.
*/
function parseModifiers(permitInvalidConstAsModifier?: boolean): NodeArray<Modifier> {
let modifiers: NodeArray<Modifier>;
function parseModifiers(permitInvalidConstAsModifier?: boolean): NodeArray<Modifier> | undefined {
let modifiers: NodeArray<Modifier> | undefined;
while (true) {
const modifierStart = scanner.getStartPos();
const modifierKind = token();
Expand Down Expand Up @@ -5422,7 +5422,7 @@ namespace ts {
return token() === SyntaxKind.ImplementsKeyword && lookAhead(nextTokenIsIdentifierOrKeyword);
}

function parseHeritageClauses(): NodeArray<HeritageClause> {
function parseHeritageClauses(): NodeArray<HeritageClause> | undefined {
// ClassTail[Yield,Await] : (Modified) See 14.5
// ClassHeritage[?Yield,?Await]opt { ClassBody[?Yield,?Await]opt }

Expand All @@ -5433,7 +5433,7 @@ namespace ts {
return undefined;
}

function parseHeritageClause() {
function parseHeritageClause(): HeritageClause | undefined {
if (token() === SyntaxKind.ExtendsKeyword || token() === SyntaxKind.ImplementsKeyword) {
const node = <HeritageClause>createNode(SyntaxKind.HeritageClause);
node.token = token();
Expand All @@ -5459,7 +5459,7 @@ namespace ts {
return token() === SyntaxKind.ExtendsKeyword || token() === SyntaxKind.ImplementsKeyword;
}

function parseClassMembers() {
function parseClassMembers(): NodeArray<ClassElement> {
return parseList(ParsingContext.ClassMembers, parseClassElement);
}

Expand Down Expand Up @@ -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 = <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);
}
}

Expand All @@ -5652,6 +5642,17 @@ namespace ts {
return finishNode(importDeclaration);
}

function parseImportEqualsDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: NodeArray<Modifier>, identifier: ts.Identifier): ImportEqualsDeclaration {
const importEqualsDeclaration = <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
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1889,7 +1894,6 @@ namespace ts {
export interface NamespaceExportDeclaration extends DeclarationStatement {
kind: SyntaxKind.NamespaceExportDeclaration;
name: Identifier;
moduleReference: LiteralLikeNode;
}

export interface ExportDeclaration extends DeclarationStatement {
Expand Down

0 comments on commit ac67b94

Please sign in to comment.