8000 Merge pull request #2291 from Microsoft/letConstInSwitchStatements · predever/TypeScript@76dcfb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 76dcfb6

Browse files
committed
Merge pull request microsoft#2291 from Microsoft/letConstInSwitchStatements
introduce CaseBlock as a block-scoped container for switch statements
2 parents aa08300 + 59c71ac commit 76dcfb6

22 files changed

+392
-186
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ module ts {
534534
case SyntaxKind.ForStatement:
535535
case SyntaxKind.ForInStatement:
536536
case SyntaxKind.ForOfStatement:
537-
case SyntaxKind.SwitchStatement:
537+
case SyntaxKind.CaseBlock:
538538
bindChildren(node, 0, /*isBlockScopeContainer*/ true);
539539
break;
540540
default:

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9049,7 +9049,7 @@ module ts {
90499049
var hasDuplicateDefaultClause = false;
90509050

90519051
var expressionType = checkExpression(node.expression);
9052-
forEach(node.clauses, clause => {
9052+
forEach(node.caseBlock.clauses, clause => {
90539053
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
90549054
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
90559055
if (firstDefaultClause === undefined) {
@@ -10165,6 +10165,7 @@ module ts {
1016510165
case SyntaxKind.BreakStatement:
1016610166
case SyntaxKind.ReturnStatement:
1016710167
case SyntaxKind.SwitchStatement:
10168+
case SyntaxKind.CaseBlock:
1016810169
case SyntaxKind.CaseClause:
1016910170
case SyntaxKind.DefaultClause:
1017010171
case SyntaxKind.LabeledStatement:

src/compiler/emitter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,7 +3708,11 @@ module ts {
37083708
emit(node.expression);
37093709
endPos = emitToken(SyntaxKind.CloseParenToken, node.expression.end);
37103710
write(" ");
3711-
emitToken(SyntaxKind.OpenBraceToken, endPos);
3711+
emitCaseBlock(node.caseBlock, endPos)
3712+
}
3713+
3714+
function emitCaseBlock(node: CaseBlock, startPos: number): void {
3715+
emitToken(SyntaxKind.OpenBraceToken, startPos);
37123716
increaseIndent();
37133717
emitLines(node.clauses);
37143718
decreaseIndent();
@@ -4120,7 +4124,7 @@ module ts {
41204124
}
41214125
switch (current.kind) {
41224126
case SyntaxKind.SourceFile:
4123-
case SyntaxKind.SwitchKeyword:
4127+
case SyntaxKind.CaseBlock:
41244128
case SyntaxKind.CatchClause:
41254129
case SyntaxKind.ModuleDeclaration:
41264130
case SyntaxKind.ForStatement:

src/compiler/parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ module ts {
209209
visitNode(cbNode, (<WithStatement>node).statement);
210210
case SyntaxKind.SwitchStatement:
211211
return visitNode(cbNode, (<SwitchStatement>node).expression) ||
212-
visitNodes(cbNodes, (<SwitchStatement>node).clauses);
212+
visitNode(cbNode, (<SwitchStatement>node).caseBlock);
213+
case SyntaxKind.CaseBlock:
214+
return visitNodes(cbNodes, (<CaseBlock>node).clauses);
213215
case SyntaxKind.CaseClause:
214216
return visitNode(cbNode, (<CaseClause>node).expression) ||
215217
visitNodes(cbNodes, (<CaseClause>node).statements);
@@ -3954,9 +3956,11 @@ module ts {
39543956
parseExpected(SyntaxKind.OpenParenToken);
39553957
node.expression = allowInAnd(parseExpression);
39563958
parseExpected(SyntaxKind.CloseParenToken);
3959+
var caseBlock = <CaseBlock>createNode(SyntaxKind.CaseBlock, scanner.getStartPos());
39573960
parseExpected(SyntaxKind.OpenBraceToken);
3958-
node.clauses = parseList(ParsingContext.SwitchClauses, /*checkForStrictMode*/ false, parseCaseOrDefaultClause);
3961+
caseBlock.clauses = parseList(ParsingContext.SwitchClauses, /*checkForStrictMode*/ false, parseCaseOrDefaultClause);
39593962
parseExpected(SyntaxKind.CloseBraceToken);
3963+
node.caseBlock = finishNode(caseBlock);
39603964
return finishNode(node);
39613965
}
39623966

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ module ts {
233233
EnumDeclaration,
234234
ModuleDeclaration,
235235
ModuleBlock,
236+
CaseBlock,
236237
ImportEqualsDeclaration,
237238
ImportDeclaration,
238239
ImportClause,
@@ -790,6 +791,10 @@ module ts {
790791

791792
export interface SwitchStatement extends Statement {
792793
expression: Expression;
794+
caseBlock: CaseBlock;
795+
}
796+
797+
export interface CaseBlock extends Node {
793798
clauses: NodeArray<CaseOrDefaultClause>;
794799
}
795800

src/compiler/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ module ts {
376376
switch (node.kind) {
377377
case SyntaxKind.ReturnStatement:
378378
return visitor(<ReturnStatement>node);
379+
case SyntaxKind.CaseBlock:
379380
case SyntaxKind.Block:
380381
case SyntaxKind.IfStatement:
381382
case SyntaxKind.DoStatement:

src/services/breakpoints.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ module ts.BreakpointResolver {
416416
var classDeclaration = <ClassDeclaration>node.parent;
417417
return spanInNodeIfStartsOnSameLine(findPrecedingToken(node.pos, sourceFile, node.parent), classDeclaration.members.length ? classDeclaration.members[0] : classDeclaration.getLastToken(sourceFile));
418418

419-
case SyntaxKind.SwitchStatement:
420-
return spanInNodeIfStartsOnSameLine(node.parent, (<SwitchStatement>node.parent).clauses[0]);
419+
case SyntaxKind.CaseBlock:
420+
return spanInNodeIfStartsOnSameLine(node.parent.parent, (<CaseBlock>node.parent).clauses[0]);
421421
}
422422

423423
// Default to parent node
@@ -447,10 +447,10 @@ module ts.BreakpointResolver {
447447
case SyntaxKind.CatchClause:
448448
return spanInNode((<Block>node.parent).statements[(<Block>node.parent).statements.length - 1]);;
449449

450-
case SyntaxKind.SwitchStatement:
450+
case SyntaxKind.CaseBlock:
451451
// breakpoint in last statement of the last clause
452-
var switchStatement = <SwitchStatement>node.parent;
453-
var lastClause = switchStatement.clauses[switchStatement.clauses.length - 1];
452+
var caseBlock = <CaseBlock>node.parent;
453+
var lastClause = caseBlock.clauses[caseBlock.clauses.length - 1];
454454
if (lastClause) {
455455
return spanInNode(lastClause.statements[lastClause.statements.length - 1]);
456456
}

src/services/formatting/rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ module ts.formatting {
541541

542542
switch (node.kind) {
543543
case SyntaxKind.Block:
544-
case SyntaxKind.SwitchStatement:
544+
case SyntaxKind.CaseBlock:
545545
case SyntaxKind.ObjectLiteralExpression:
546546
case SyntaxKind.ModuleBlock:
547547
return true;

src/services/formatting/smartIndenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ module ts.formatting {
357357
case SyntaxKind.ModuleBlock:
358358
case SyntaxKind.ObjectLiteralExpression:
359359
case SyntaxKind.TypeLiteral:
360-
case SyntaxKind.SwitchStatement:
360+
case SyntaxKind.CaseBlock:
361361
case SyntaxKind.DefaultClause:
362362
case SyntaxKind.CaseClause:
363363
case SyntaxKind.ParenthesizedExpression:
@@ -431,7 +431,7 @@ module ts.formatting {
431431
case SyntaxKind.ObjectLiteralExpression:
432432
case SyntaxKind.Block:
433433
case SyntaxKind.ModuleBlock:
434-
case SyntaxKind.SwitchStatement:
434+
case SyntaxKind.CaseBlock:
435435
return nodeEndsWith(n, SyntaxKind.CloseBraceToken, sourceFile);
436436
case SyntaxKind.CatchClause:
437437
return isCompletedNode((<CatchClause>n).block, sourceFile);

src/services/outliningElementsCollector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ module ts {
104104
case SyntaxKind.InterfaceDeclaration:
105105
case SyntaxKind.EnumDeclaration:
106106
case SyntaxKind.ObjectLiteralExpression:
107-
case SyntaxKind.SwitchStatement:
107+
case SyntaxKind.CaseBlock:
108108
var openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile);
109109
var closeBrace = findChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile);
110110
addOutliningSpan(n, openBrace, closeBrace, autoCollapse(n));

0 commit comments

Comments
 (0)
0