Skip to content

Commit f9a8d2f

Browse files
MaxGraeydcodeIO
authored andcommitted
Use lazy initializations for decorators and type parameters in parser (AssemblyScript#554)
1 parent 858cca5 commit f9a8d2f

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/parser.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ export class Parser extends DiagnosticEmitter {
164164
this.skipStatement(tn);
165165
continue;
166166
}
167-
if (!decorators) decorators = [];
168-
decorators.push(decorator);
167+
if (!decorators) decorators = [decorator];
168+
else decorators.push(decorator);
169169
}
170170

171171
// check modifiers
@@ -454,7 +454,7 @@ export class Parser extends DiagnosticEmitter {
454454
} else if (token == Token.IDENTIFIER) {
455455
let first = Node.createSimpleTypeName(tn.readIdentifier(), tn.range());
456456
let current = first;
457-
let parameters = new Array<TypeNode>();
457+
let parameters: TypeNode[] | null = null;
458458
let nullable = false;
459459

460460
// Identifier ('.' Identifier)+
@@ -477,7 +477,8 @@ export class Parser extends DiagnosticEmitter {
477477
do {
478478
let parameter = this.parseType(tn, true, suppressErrors);
479479
if (!parameter) return null;
480-
parameters.push(<TypeNode>parameter);
480+
if (!parameters) parameters = [<TypeNode>parameter];
481+
else parameters.push(<TypeNode>parameter);
481482
} while (tn.skip(Token.COMMA));
482483
if (!tn.skip(Token.GREATERTHAN)) {
483484
if (!suppressErrors) {
@@ -503,8 +504,7 @@ export class Parser extends DiagnosticEmitter {
503504
return null;
504505
}
505506
}
506-
type = Node.createType(first, parameters, nullable, tn.range(startPos, tn.pos));
507-
507+
type = Node.createType(first, parameters || [], nullable, tn.range(startPos, tn.pos));
508508
} else {
509509
if (!suppressErrors) {
510510
this.error(
@@ -939,7 +939,7 @@ export class Parser extends DiagnosticEmitter {
939939

940940
// at '<': TypeParameter (',' TypeParameter)* '>'
941941

942-
var typeParameters = new Array<TypeParameterNode>();
942+
var typeParameters: TypeParameterNode[] | null = null;
943943
var seenOptional = false;
944944
while (!tn.skip(Token.GREATERTHAN)) {
945945
let typeParameter = this.parseTypeParameter(tn);
@@ -953,7 +953,8 @@ export class Parser extends DiagnosticEmitter {
953953
);
954954
typeParameter.defaultType = null;
955955
}
956-
typeParameters.push(<TypeParameterNode>typeParameter);
956+
if (!typeParameters) typeParameters = [ typeParameter ];
957+
else typeParameters.push(typeParameter);
957958
if (!tn.skip(Token.COMMA)) {
958959
if (tn.skip(Token.GREATERTHAN)) {
959960
break;
@@ -966,7 +967,7 @@ export class Parser extends DiagnosticEmitter {
966967
}
967968
}
968969
}
969-
if (typeParameters.length === 0) {
970+
if (!(typeParameters && typeParameters.length)) {
970971
this.error(
971972
DiagnosticCode.Type_parameter_list_cannot_be_empty,
972973
tn.range()
@@ -1532,8 +1533,8 @@ export class Parser extends DiagnosticEmitter {
15321533
let type = this.parseType(tn);
15331534
if (!type) return null;
15341535
if (!isInterface) {
1535-
if (!implementsTypes) implementsTypes = [];
1536-
implementsTypes.push(<TypeNode>type);
1536+
if (!implementsTypes) implementsTypes = [<TypeNode>type];
1537+
else implementsTypes.push(<TypeNode>type);
15371538
}
15381539
} while (tn.skip(Token.COMMA));
15391540
}
@@ -1636,14 +1637,15 @@ export class Parser extends DiagnosticEmitter {
16361637
var startPos = tn.pos;
16371638
var isInterface = parent.kind == NodeKind.INTERFACEDECLARATION;
16381639

1639-
var decorators = new Array<DecoratorNode>();
1640+
var decorators: DecoratorNode[] | null = null;
16401641
if (tn.skip(Token.AT)) {
16411642
do {
16421643
let decorator = this.parseDecorator(tn);
16431644
if (!decorator) break;
1644-
decorators.push(<DecoratorNode>decorator);
1645+
if (!decorators) decorators = [<DecoratorNode>decorator];
1646+
else decorators.push(<DecoratorNode>decorator);
16451647
} while (tn.skip(Token.AT));
1646-
if (isInterface) {
1648+
if (decorators && isInterface) {
16471649
this.error(
16481650
DiagnosticCode.Decorators_are_not_valid_here,
16491651
Range.join(decorators[0].range, decorators[decorators.length - 1].range)
@@ -2064,11 +2066,11 @@ export class Parser extends DiagnosticEmitter {
20642066
return null;
20652067
}
20662068

2067-
parseIndexSignatureDeclaration(tn: Tokenizer, decorators: DecoratorNode[]): IndexSignatureDeclaration | null {
2069+
parseIndexSignatureDeclaration(tn: Tokenizer, decorators: DecoratorNode[] | null): IndexSignatureDeclaration | null {
20682070

20692071
// at: '[': 'key' ':' Type ']' ':' Type
20702072

2071-
if (decorators.length) {
2073+
if (decorators && decorators.length) {
20722074
this.error(
20732075
DiagnosticCode.Decorators_are_not_valid_here,
20742076
Range.join(decorators[0].range, decorators[decorators.length - 1].range)
@@ -3410,7 +3412,7 @@ export class Parser extends DiagnosticEmitter {
34103412

34113413
var state = tn.mark();
34123414
if (!tn.skip(Token.LESSTHAN)) return null;
3413-
var typeArguments = new Array<CommonTypeNode>();
3415+
var typeArguments: CommonTypeNode[] | null = null;
34143416
do {
34153417
if (tn.peek() === Token.GREATERTHAN) {
34163418
break;
@@ -3420,7 +3422,8 @@ export class Parser extends DiagnosticEmitter {
34203422
tn.reset(state);
34213423
return null;
34223424
}
3423-
typeArguments.push(type);
3425+
if (!typeArguments) typeArguments = [ type ];
3426+
else typeArguments.push(type);
34243427
} while (tn.skip(Token.COMMA));
34253428
if (tn.skip(Token.GREATERTHAN) && tn.skip(Token.OPENPAREN)) {
34263429
return typeArguments;

0 commit comments

Comments
 (0)