Skip to content

Commit 04c0552

Browse files
Merge pull request microsoft#1334 from Microsoft/tokens
Tokens
2 parents af324d1 + deca944 commit 04c0552

36 files changed

+648
-517
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ module ts {
348348
bindChildren(node, symbolKind, isBlockScopeContainer);
349349
}
350350

351-
function bindCatchVariableDeclaration(node: CatchBlock) {
352-
var symbol = createSymbol(SymbolFlags.FunctionScopedVariable, node.variable.text || "__missing");
351+
function bindCatchVariableDeclaration(node: CatchClause) {
352+
var symbol = createSymbol(SymbolFlags.FunctionScopedVariable, node.name.text || "__missing");
353353
addDeclarationToSymbol(symbol, node, SymbolFlags.FunctionScopedVariable);
354354
var saveParent = parent;
355355
var savedBlockScopeContainer = blockScopeContainer;
@@ -444,8 +444,8 @@ module ts {
444444
case SyntaxKind.ArrowFunction:
445445
bindAnonymousDeclaration(<FunctionExpression>node, SymbolFlags.Function, "__function", /*isBlockScopeContainer*/ true);
446446
break;
447-
case SyntaxKind.CatchBlock:
448-
bindCatchVariableDeclaration(<CatchBlock>node);
447+
case SyntaxKind.CatchClause:
448+
bindCatchVariableDeclaration(<CatchClause>node);
449449
break;
450450
case SyntaxKind.ClassDeclaration:
451451
bindDeclaration(<Declaration>node, SymbolFlags.Class, SymbolFlags.ClassExcludes, /*isBlockScopeContainer*/ false);
@@ -478,7 +478,7 @@ module ts {
478478

479479
case SyntaxKind.Block:
480480
case SyntaxKind.TryBlock:
481-
case SyntaxKind.CatchBlock:
481+
case SyntaxKind.CatchClause:
482482
case SyntaxKind.FinallyBlock:
483483
case SyntaxKind.ForStatement:
484484
case SyntaxKind.ForInStatement:

src/compiler/checker.ts

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ module ts {
415415
break loop;
416416
}
417417
break;
418-
case SyntaxKind.CatchBlock:
419-
var id = (<CatchBlock>location).variable;
418+
case SyntaxKind.CatchClause:
419+
var id = (<CatchClause>location).name;
420420
if (name === id.text) {
421421
result = location.symbol;
422422
break loop;
@@ -756,7 +756,7 @@ module ts {
756756
//
757757
// x is an optional parameter, but it is a required property.
758758
return propertySymbol.valueDeclaration &&
759-
propertySymbol.valueDeclaration.flags & NodeFlags.QuestionMark &&
759+
hasQuestionToken(propertySymbol.valueDeclaration) &&
760760
propertySymbol.valueDeclaration.kind !== SyntaxKind.Parameter;
761761
}
762762

@@ -1437,11 +1437,11 @@ module ts {
14371437
}
14381438

14391439
function buildParameterDisplay(p: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, typeStack?: Type[]) {
1440-
if (getDeclarationFlagsFromSymbol(p) & NodeFlags.Rest) {
1440+
if (hasDotDotDotToken(p.valueDeclaration)) {
14411441
writePunctuation(writer, SyntaxKind.DotDotDotToken);
14421442
}
14431443
appendSymbolNameOnly(p, writer);
1444-
if (p.valueDeclaration.flags & NodeFlags.QuestionMark || (<VariableDeclaration>p.valueDeclaration).initializer) {
1444+
if (hasQuestionToken(p.valueDeclaration) || (<VariableDeclaration>p.valueDeclaration).initializer) {
14451445
writePunctuation(writer, SyntaxKind.QuestionToken);
14461446
}
14471447
writePunctuation(writer, SyntaxKind.ColonToken);
@@ -1711,7 +1711,7 @@ module ts {
17111711
}
17121712

17131713
// Rest parameters default to type any[], other parameters default to type any
1714-
var type = declaration.flags & NodeFlags.Rest ? createArrayType(anyType) : anyType;
1714+
var type = hasDotDotDotToken(declaration) ? createArrayType(anyType) : anyType;
17151715
checkImplicitAny(type);
17161716
return type;
17171717

@@ -1733,9 +1733,9 @@ module ts {
17331733
var diagnostic = Diagnostics.Member_0_implicitly_has_an_1_type;
17341734
break;
17351735
case SyntaxKind.Parameter:
1736-
var diagnostic = declaration.flags & NodeFlags.Rest ?
1737-
Diagnostics.Rest_parameter_0_implicitly_has_an_any_type :
1738-
Diagnostics.Parameter_0_implicitly_has_an_1_type;
1736+
var diagnostic = hasDotDotDotToken(declaration)
1737+
? Diagnostics.Rest_parameter_0_implicitly_has_an_any_type
1738+
: Diagnostics.Parameter_0_implicitly_has_an_1_type;
17391739
break;
17401740
default:
17411741
var diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type;
@@ -1753,7 +1753,7 @@ module ts {
17531753
}
17541754
// Handle catch clause variables
17551755
var declaration = symbol.valueDeclaration;
1756-
if (declaration.kind === SyntaxKind.CatchBlock) {
1756+
if (declaration.kind === SyntaxKind.CatchClause) {
17571757
return links.type = anyType;
17581758
}
17591759
// Handle variable, parameter or property
@@ -2527,7 +2527,7 @@ module ts {
25272527
hasStringLiterals = true;
25282528
}
25292529
if (minArgumentCount < 0) {
2530-
if (param.initializer || param.flags & (NodeFlags.QuestionMark | NodeFlags.Rest)) {
2530+
if (param.initializer || param.questionToken || param.dotDotDotToken) {
25312531
minArgumentCount = i;
25322532
}
25332533
}
@@ -4375,7 +4375,7 @@ module ts {
43754375
case SyntaxKind.ThrowStatement:
43764376
case SyntaxKind.TryStatement:
43774377
case SyntaxKind.TryBlock:
4378-
case SyntaxKind.CatchBlock:
4378+
case SyntaxKind.CatchClause:
43794379
case SyntaxKind.FinallyBlock:
43804380
return forEachChild(node, isAssignedIn);
43814381
}
@@ -6673,7 +6673,7 @@ module ts {
66736673
!(parameterDeclaration.parent.kind === SyntaxKind.Constructor && (<ConstructorDeclaration>parameterDeclaration.parent).body)) {
66746674
error(parameterDeclaration, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
66756675
}
6676-
if (parameterDeclaration.flags & NodeFlags.Rest) {
6676+
if (parameterDeclaration.dotDotDotToken) {
66776677
if (!isArrayType(getTypeOfSymbol(parameterDeclaration.symbol))) {
66786678
error(parameterDeclaration, Diagnostics.A_rest_parameter_must_be_of_an_array_type);
66796679
}
@@ -7020,20 +7020,23 @@ module ts {
70207020
return;
70217021
}
70227022

7023+
function getCanonicalOverload(overloads: Declaration[], implementation: FunctionLikeDeclaration) {
7024+
// Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration
7025+
// Error on all deviations from this canonical set of flags
7026+
// The caveat is that if some overloads are defined in lib.d.ts, we don't want to
7027+
// report the errors on those. To achieve this, we will say that the implementation is
7028+
// the canonical signature only if it is in the same container as the first overload
7029+
var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent;
7030+
return implementationSharesContainerWithFirstOverload ? implementation : overloads[0];
7031+
}
7032+
70237033
function checkFlagAgreementBetweenOverloads(overloads: Declaration[], implementation: FunctionLikeDeclaration, flagsToCheck: NodeFlags, someOverloadFlags: NodeFlags, allOverloadFlags: NodeFlags): void {
70247034
// Error if some overloads have a flag that is not shared by all overloads. To find the
70257035
// deviations, we XOR someOverloadFlags with allOverloadFlags
70267036
var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags;
70277037
if (someButNotAllOverloadFlags !== 0) {
7028-
// Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration
7029-
// Error on all deviations from this canonical set of flags
7030-
// The caveat is that if some overloads are defined in lib.d.ts, we don't want to
7031-
// report the errors on those. To achieve this, we will say that the implementation is
7032-
// the canonical signature only if it is in the same container as the first overload
7033-
var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent;
7034-
var canonicalFlags = implementationSharesContainerWithFirstOverload
7035-
? getEffectiveDeclarationFlags(implementation, flagsToCheck)
7036-
: getEffectiveDeclarationFlags(overloads[0], flagsToCheck);
7038+
var canonicalFlags = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck);
7039+
70377040
forEach(overloads, o => {
70387041
var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags;
70397042
if (deviation & NodeFlags.Export) {
@@ -7045,16 +7048,27 @@ module ts {
70457048
else if (deviation & (NodeFlags.Private | NodeFlags.Protected)) {
70467049
error(o.name, Diagnostics.Overload_signatures_must_all_be_public_private_or_protected);
70477050
}
7048-
else if (deviation & NodeFlags.QuestionMark) {
7051+
});
7052+
}
7053+
}
7054+
7055+
function checkQuestionTokenAgreementBetweenOverloads(overloads: Declaration[], implementation: FunctionLikeDeclaration, someHaveQuestionToken: boolean, allHaveQuestionToken: boolean): void {
7056+
if (someHaveQuestionToken !== allHaveQuestionToken) {
7057+
var canonicalHasQuestionToken = hasQuestionToken(getCanonicalOverload(overloads, implementation));
7058+
forEach(overloads, o => {
7059+
var deviation = hasQuestionToken(o) !== canonicalHasQuestionToken;
7060+
if (deviation) {
70497061
error(o.name, Diagnostics.Overload_signatures_must_all_be_optional_or_required);
70507062
}
70517063
});
70527064
}
70537065
}
70547066

7055-
var flagsToCheck: NodeFlags = NodeFlags.Export | NodeFlags.Ambient | NodeFlags.Private | NodeFlags.Protected | NodeFlags.QuestionMark;
7067+
var flagsToCheck: NodeFlags = NodeFlags.Export | NodeFlags.Ambient | NodeFlags.Private | NodeFlags.Protected;
70567068
var someNodeFlags: NodeFlags = 0;
70577069
var allNodeFlags = flagsToCheck;
7070+
var someHaveQuestionToken = false;
7071+
var allHaveQuestionToken = true;
70587072
var hasOverloads = false;
70597073
var bodyDeclaration: FunctionLikeDeclaration;
70607074
var lastSeenNonAmbientDeclaration: FunctionLikeDeclaration;
@@ -7128,6 +7142,8 @@ module ts {
71287142
var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck);
71297143
someNodeFlags |= currentNodeFlags;
71307144
allNodeFlags &= currentNodeFlags;
7145+
someHaveQuestionToken = someHaveQuestionToken || hasQuestionToken(node);
7146+
allHaveQuestionToken = allHaveQuestionToken && hasQuestionToken(node);
71317147

71327148
if (node.body && bodyDeclaration) {
71337149
if (isConstructor) {
@@ -7176,6 +7192,8 @@ module ts {
71767192

71777193
if (hasOverloads) {
71787194
checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags);
7195+
checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken);
7196+
71797197
if (bodyDeclaration) {
71807198
var signatures = getSignaturesOfSymbol(symbol);
71817199
var bodySignature = getSignatureFromDeclaration(bodyDeclaration);
@@ -7725,13 +7743,14 @@ module ts {
77257743
function checkSwitchStatement(node: SwitchStatement) {
77267744
var expressionType = checkExpression(node.expression);
77277745
forEach(node.clauses, clause => {
7728-
if (fullTypeCheck && clause.expression) {
7746+
if (fullTypeCheck && clause.kind === SyntaxKind.CaseClause) {
7747+
var caseClause = <CaseClause>clause;
77297748
// TypeScript 1.0 spec (April 2014):5.9
77307749
// In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression.
7731-
var caseType = checkExpression(clause.expression);
7750+
var caseType = checkExpression(caseClause.expression);
77327751
if (!isTypeAssignableTo(expressionType, caseType)) {
77337752
// check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails
7734-
checkTypeAssignableTo(caseType, expressionType, clause.expression, /*headMessage*/ undefined);
7753+
checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
77357754
}
77367755
}
77377756
forEach(clause.statements, checkSourceElement);
@@ -7748,7 +7767,7 @@ module ts {
77487767

77497768
function checkTryStatement(node: TryStatement) {
77507769
checkBlock(node.tryBlock);
7751-
if (node.catchBlock) checkBlock(node.catchBlock);
7770+
if (node.catchClause) checkBlock(node.catchClause.block);
77527771
if (node.finallyBlock) checkBlock(node.finallyBlock);
77537772
}
77547773

@@ -8586,7 +8605,7 @@ module ts {
85868605
case SyntaxKind.ThrowStatement:
85878606
case SyntaxKind.TryStatement:
85888607
case SyntaxKind.TryBlock:
8589-
case SyntaxKind.CatchBlock:
8608+
case SyntaxKind.CatchClause:
85908609
case SyntaxKind.FinallyBlock:
85918610
case SyntaxKind.VariableDeclaration:
85928611
case SyntaxKind.ClassDeclaration:
@@ -8748,8 +8767,8 @@ module ts {
87488767
copySymbol(location.symbol, meaning);
87498768
}
87508769
break;
8751-
case SyntaxKind.CatchBlock:
8752-
if ((<CatchBlock>location).variable.text) {
8770+
case SyntaxKind.CatchClause:
8771+
if ((<CatchClause>location).name.text) {
87538772
copySymbol(location.symbol, meaning);
87548773
}
87558774
break;

src/compiler/emitter.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ module ts {
943943
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
944944
writeTextOfNode(currentSourceFile, node.name);
945945
// If optional property emit ?
946-
if (node.kind === SyntaxKind.Property && (node.flags & NodeFlags.QuestionMark)) {
946+
if (node.kind === SyntaxKind.Property && hasQuestionToken(node)) {
947947
write("?");
948948
}
949949
if (node.kind === SyntaxKind.Property && node.parent.kind === SyntaxKind.TypeLiteral) {
@@ -1124,7 +1124,7 @@ module ts {
11241124
}
11251125
else {
11261126
writeTextOfNode(currentSourceFile, node.name);
1127-
if (node.flags & NodeFlags.QuestionMark) {
1127+
if (hasQuestionToken(node)) {
11281128
write("?");
11291129
}
11301130
}
@@ -1252,11 +1252,11 @@ module ts {
12521252
function emitParameterDeclaration(node: ParameterDeclaration) {
12531253
increaseIndent();
12541254
emitJsDocComments(node);
1255-
if (node.flags & NodeFlags.Rest) {
1255+
if (node.dotDotDotToken) {
12561256
write("...");
12571257
}
12581258
writeTextOfNode(currentSourceFile, node.name);
1259-
if (node.initializer || (node.flags & NodeFlags.QuestionMark)) {
1259+
if (node.initializer || hasQuestionToken(node)) {
12601260
write("?");
12611261
}
12621262
decreaseIndent();
@@ -2142,8 +2142,8 @@ module ts {
21422142
return false;
21432143
case SyntaxKind.LabeledStatement:
21442144
return (<LabeledStatement>node.parent).label === node;
2145-
case SyntaxKind.CatchBlock:
2146-
return (<CatchBlock>node.parent).variable === node;
2145+
case SyntaxKind.CatchClause:
2146+
return (<CatchClause>node.parent).name === node;
21472147
}
21482148
}
21492149

@@ -2624,7 +2624,7 @@ module ts {
26242624
function emitCaseOrDefaultClause(node: CaseOrDefaultClause) {
26252625
if (node.kind === SyntaxKind.CaseClause) {
26262626
write("case ");
2627-
emit(node.expression);
2627+
emit((<CaseClause>node).expression);
26282628
write(":");
26292629
}
26302630
else {
@@ -2650,23 +2650,23 @@ module ts {
26502650
function emitTryStatement(node: TryStatement) {
26512651
write("try ");
26522652
emit(node.tryBlock);
2653-
emit(node.catchBlock);
2653+
emit(node.catchClause);
26542654
if (node.finallyBlock) {
26552655
writeLine();
26562656
write("finally ");
26572657
emit(node.finallyBlock);
26582658
}
26592659
}
26602660

2661-
function emitCatchBlock(node: CatchBlock) {
2661+
function emitCatchClause(node: CatchClause) {
26622662
writeLine();
26632663
var endPos = emitToken(SyntaxKind.CatchKeyword, node.pos);
26642664
write(" ");
26652665
emitToken(SyntaxKind.OpenParenToken, endPos);
2666-
emit(node.variable);
2667-
emitToken(SyntaxKind.CloseParenToken, node.variable.end);
2666+
emit(node.name);
2667+
emitToken(SyntaxKind.CloseParenToken, node.name.end);
26682668
write(" ");
2669-
emitBlock(node);
2669+
emitBlock(node.block);
26702670
}
26712671

26722672
function emitDebuggerStatement(node: Node) {
@@ -3599,8 +3599,8 @@ module ts {
35993599
return emitThrowStatement(<ThrowStatement>node);
36003600
case SyntaxKind.TryStatement:
36013601
return emitTryStatement(<TryStatement>node);
3602-
case SyntaxKind.CatchBlock:
3603-
return emitCatchBlock(<CatchBlock>node);
3602+
case SyntaxKind.CatchClause:
3603+
return emitCatchClause(<CatchClause>node);
36043604
case SyntaxKind.DebuggerStatement:
36053605
return emitDebuggerStatement(node);
36063606
case SyntaxKind.VariableDeclaration:

0 commit comments

Comments
 (0)