Skip to content

Commit 9c0f770

Browse files
author
Andy Hanson
committed
Clean up code in parser
1 parent 4b3cd6a commit 9c0f770

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/compiler/parser.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ namespace ts {
13091309
case ParsingContext.ObjectBindingElements:
13101310
return token() === SyntaxKind.OpenBracketToken || token() === SyntaxKind.DotDotDotToken || isLiteralPropertyName();
13111311
case ParsingContext.HeritageClauseElement:
1312-
// If we see { } then only consume it as an expression if it is followed by , or {
1312+
// If we see { } then only consume it as an expression if it is followed by `,` or `{`
13131313
// That way we won't consume the body of a class in its heritage clause.
13141314
if (token() === SyntaxKind.OpenBraceToken) {
13151315
return lookAhead(isValidHeritageClauseObjectLiteral);
@@ -2113,7 +2113,7 @@ namespace ts {
21132113
return finishNode(node);
21142114
}
21152115

2116-
function parseTypeParameters(): NodeArray<TypeParameterDeclaration> {
2116+
function parseTypeParameters(): NodeArray<TypeParameterDeclaration> | undefined {
21172117
if (token() === SyntaxKind.LessThanToken) {
21182118
return parseBracketedList(ParsingContext.TypeParameters, parseTypeParameter, SyntaxKind.LessThanToken, SyntaxKind.GreaterThanToken);
21192119
}
@@ -2183,7 +2183,7 @@ namespace ts {
21832183
}
21842184

21852185
function fillSignature(
2186-
returnToken: SyntaxKind,
2186+
returnToken: SyntaxKind.ColonToken | SyntaxKind.EqualsGreaterThanToken,
21872187
yieldContext: boolean,
21882188
awaitContext: boolean,
21892189
requireCompleteParameterList: boolean,
@@ -2373,14 +2373,14 @@ namespace ts {
23732373
}
23742374

23752375
function isTypeMemberStart(): boolean {
2376-
let idToken: SyntaxKind;
23772376
// Return true if we have the start of a signature member
23782377
if (token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.LessThanToken) {
23792378
return true;
23802379
}
2380+
let idToken: boolean;
23812381
// Eat up all modifiers, but hold on to the last one in case it is actually an identifier
23822382
while (isModifierKind(token())) {
2383-
idToken = token();
2383+
idToken = true;
23842384
nextToken();
23852385
}
23862386
// Index signatures and computed property names are type members
@@ -2389,7 +2389,7 @@ namespace ts {
23892389
}
23902390
// Try to get the first property-like token following all modifiers
23912391
if (isLiteralPropertyName()) {
2392-
idToken = token();
2392+
idToken = true;
23932393
nextToken();
23942394
}
23952395
// If we were able to get any potential identifier, check that it is
@@ -2497,7 +2497,7 @@ namespace ts {
24972497
return finishNode(node);
24982498
}
24992499

2500-
function parseKeywordAndNoDot(): TypeNode {
2500+
function parseKeywordAndNoDot(): TypeNode | undefined {
25012501
const node = parseTokenNode<TypeNode>();
25022502
return token() === SyntaxKind.DotToken ? undefined : node;
25032503
}
@@ -2635,7 +2635,7 @@ namespace ts {
26352635
return parseArrayTypeOrHigher();
26362636
}
26372637

2638-
function parseUnionOrIntersectionType(kind: SyntaxKind, parseConstituentType: () => TypeNode, operator: SyntaxKind): TypeNode {
2638+
function parseUnionOrIntersectionType(kind: SyntaxKind.UnionType | SyntaxKind.IntersectionType, parseConstituentType: () => TypeNode, operator: SyntaxKind.BarToken | SyntaxKind.AmpersandToken): TypeNode {
26392639
parseOptional(operator);
26402640
let type = parseConstituentType();
26412641
if (token() === operator) {
@@ -5281,8 +5281,8 @@ namespace ts {
52815281
*
52825282
* In such situations, 'permitInvalidConstAsModifier' should be set to true.
52835283
*/
5284-
function parseModifiers(permitInvalidConstAsModifier?: boolean): NodeArray<Modifier> {
5285-
let modifiers: NodeArray<Modifier>;
5284+
function parseModifiers(permitInvalidConstAsModifier?: boolean): NodeArray<Modifier> | undefined {
5285+
let modifiers: NodeArray<Modifier> | undefined;
52865286
while (true) {
52875287
const modifierStart = scanner.getStartPos();
52885288
const modifierKind = token();
@@ -5422,7 +5422,7 @@ namespace ts {
54225422
return token() === SyntaxKind.ImplementsKeyword && lookAhead(nextTokenIsIdentifierOrKeyword);
54235423
}
54245424

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

@@ -5433,7 +5433,7 @@ namespace ts {
54335433
return undefined;
54345434
}
54355435

5436-
function parseHeritageClause() {
5436+
function parseHeritageClause(): HeritageClause | undefined {
54375437
if (token() === SyntaxKind.ExtendsKeyword || token() === SyntaxKind.ImplementsKeyword) {
54385438
const node = <HeritageClause>createNode(SyntaxKind.HeritageClause);
54395439
node.token = token();
@@ -5459,7 +5459,7 @@ namespace ts {
54595459
return token() === SyntaxKind.ExtendsKeyword || token() === SyntaxKind.ImplementsKeyword;
54605460
}
54615461

5462-
function parseClassMembers() {
5462+
function parseClassMembers(): NodeArray<ClassElement> {
54635463
return parseList(ParsingContext.ClassMembers, parseClassElement);
54645464
}
54655465

@@ -5618,17 +5618,7 @@ namespace ts {
56185618
if (isIdentifier()) {
56195619
identifier = parseIdentifier();
56205620
if (token() !== SyntaxKind.CommaToken && token() !== SyntaxKind.FromKeyword) {
5621-
// ImportEquals declaration of type:
5622-
// import x = require("mod"); or
5623-
// import x = M.x;
5624-
const importEqualsDeclaration = <ImportEqualsDeclaration>createNode(SyntaxKind.ImportEqualsDeclaration, fullStart);
5625-
importEqualsDeclaration.decorators = decorators;
5626-
importEqualsDeclaration.modifiers = modifiers;
5627-
importEqualsDeclaration.name = identifier;
5628-
parseExpected(SyntaxKind.EqualsToken);
5629-
importEqualsDeclaration.moduleReference = parseModuleReference();
5630-
parseSemicolon();
5631-
return addJSDocComment(finishNode(importEqualsDeclaration));
5621+
return parseImportEqualsDeclaration(fullStart, decorators, modifiers, identifier);
56325622
}
56335623
}
56345624

@@ -5652,6 +5642,20 @@ namespace ts {
56525642
return finishNode(importDeclaration);
56535643
}
56545644

5645+
function parseImportEqualsDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: NodeArray<Modifier>, identifier: ts.Identifier): ImportEqualsDeclaration {
5646+
// ImportEquals declaration of type:
5647+
// import x = require("mod"); or
5648+
// import x = M.x;
5649+
const importEqualsDeclaration = <ImportEqualsDeclaration>createNode(SyntaxKind.ImportEqualsDeclaration, fullStart);
5650+
importEqualsDeclaration.decorators = decorators;
5651+
importEqualsDeclaration.modifiers = modifiers;
5652+
importEqualsDeclaration.name = identifier;
5653+
parseExpected(SyntaxKind.EqualsToken);
5654+
importEqualsDeclaration.moduleReference = parseModuleReference();
5655+
parseSemicolon();
5656+
return addJSDocComment(finishNode(importEqualsDeclaration));
5657+
}
5658+
56555659
function parseImportClause(identifier: Identifier, fullStart: number) {
56565660
// ImportClause:
56575661
// ImportedDefaultBinding

src/compiler/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ namespace ts {
18131813
kind: SyntaxKind.ModuleDeclaration;
18141814
parent?: ModuleBody | SourceFile;
18151815
name: ModuleName;
1816-
body?: ModuleBody | JSDocNamespaceDeclaration | Identifier;
1816+
body?: ModuleBody | JSDocNamespaceDeclaration;
18171817
}
18181818

18191819
export type NamespaceBody = ModuleBlock | NamespaceDeclaration;
@@ -1889,7 +1889,6 @@ namespace ts {
18891889
export interface NamespaceExportDeclaration extends DeclarationStatement {
18901890
kind: SyntaxKind.NamespaceExportDeclaration;
18911891
name: Identifier;
1892-
moduleReference: LiteralLikeNode;
18931892
}
18941893

18951894
export interface ExportDeclaration extends DeclarationStatement {

0 commit comments

Comments
 (0)