mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
introduce CaseBlock as a block-scoped container for switch statements
This commit is contained in:
@@ -534,7 +534,7 @@ module ts {
|
||||
case SyntaxKind.ForStatement:
|
||||
case SyntaxKind.ForInStatement:
|
||||
case SyntaxKind.ForOfStatement:
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
bindChildren(node, 0, /*isBlockScopeContainer*/ true);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -9031,7 +9031,7 @@ module ts {
|
||||
var hasDuplicateDefaultClause = false;
|
||||
|
||||
var expressionType = checkExpression(node.expression);
|
||||
forEach(node.clauses, clause => {
|
||||
forEach(node.caseBlock.clauses, clause => {
|
||||
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
|
||||
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
|
||||
if (firstDefaultClause === undefined) {
|
||||
@@ -10147,6 +10147,7 @@ module ts {
|
||||
case SyntaxKind.BreakStatement:
|
||||
case SyntaxKind.ReturnStatement:
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
case SyntaxKind.CaseClause:
|
||||
case SyntaxKind.DefaultClause:
|
||||
case SyntaxKind.LabeledStatement:
|
||||
|
||||
@@ -3541,10 +3541,10 @@ module ts {
|
||||
write(" ");
|
||||
emitToken(SyntaxKind.OpenBraceToken, endPos);
|
||||
increaseIndent();
|
||||
emitLines(node.clauses);
|
||||
emitLines(node.caseBlock.clauses);
|
||||
decreaseIndent();
|
||||
writeLine();
|
||||
emitToken(SyntaxKind.CloseBraceToken, node.clauses.end);
|
||||
emitToken(SyntaxKind.CloseBraceToken, node.caseBlock.clauses.end);
|
||||
}
|
||||
|
||||
function nodeStartPositionsAreOnSameLine(node1: Node, node2: Node) {
|
||||
@@ -3938,7 +3938,7 @@ module ts {
|
||||
}
|
||||
switch (current.kind) {
|
||||
case SyntaxKind.SourceFile:
|
||||
case SyntaxKind.SwitchKeyword:
|
||||
case SyntaxKind.CaseBlock:
|
||||
case SyntaxKind.CatchClause:
|
||||
case SyntaxKind.ModuleDeclaration:
|
||||
case SyntaxKind.ForStatement:
|
||||
|
||||
@@ -209,7 +209,9 @@ module ts {
|
||||
visitNode(cbNode, (<WithStatement>node).statement);
|
||||
case SyntaxKind.SwitchStatement:
|
||||
return visitNode(cbNode, (<SwitchStatement>node).expression) ||
|
||||
visitNodes(cbNodes, (<SwitchStatement>node).clauses);
|
||||
visitNode(cbNode, (<SwitchStatement>node).caseBlock);
|
||||
case SyntaxKind.CaseBlock:
|
||||
return visitNodes(cbNodes, (<CaseBlock>node).clauses);
|
||||
case SyntaxKind.CaseClause:
|
||||
return visitNode(cbNode, (<CaseClause>node).expression) ||
|
||||
visitNodes(cbNodes, (<CaseClause>node).statements);
|
||||
@@ -3954,9 +3956,11 @@ module ts {
|
||||
parseExpected(SyntaxKind.OpenParenToken);
|
||||
node.expression = allowInAnd(parseExpression);
|
||||
parseExpected(SyntaxKind.CloseParenToken);
|
||||
var caseBlock = <CaseBlock>createNode(SyntaxKind.CaseBlock, scanner.getStartPos());
|
||||
parseExpected(SyntaxKind.OpenBraceToken);
|
||||
node.clauses = parseList(ParsingContext.SwitchClauses, /*checkForStrictMode*/ false, parseCaseOrDefaultClause);
|
||||
caseBlock.clauses = parseList(ParsingContext.SwitchClauses, /*checkForStrictMode*/ false, parseCaseOrDefaultClause);
|
||||
parseExpected(SyntaxKind.CloseBraceToken);
|
||||
node.caseBlock = finishNode(caseBlock);
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
|
||||
@@ -233,6 +233,7 @@ module ts {
|
||||
EnumDeclaration,
|
||||
ModuleDeclaration,
|
||||
ModuleBlock,
|
||||
CaseBlock,
|
||||
ImportEqualsDeclaration,
|
||||
ImportDeclaration,
|
||||
ImportClause,
|
||||
@@ -790,6 +791,10 @@ module ts {
|
||||
|
||||
export interface SwitchStatement extends Statement {
|
||||
expression: Expression;
|
||||
caseBlock: CaseBlock;
|
||||
}
|
||||
|
||||
export interface CaseBlock extends Node {
|
||||
clauses: NodeArray<CaseOrDefaultClause>;
|
||||
}
|
||||
|
||||
|
||||
@@ -376,6 +376,7 @@ module ts {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ReturnStatement:
|
||||
return visitor(<ReturnStatement>node);
|
||||
case SyntaxKind.CaseBlock:
|
||||
case SyntaxKind.Block:
|
||||
case SyntaxKind.IfStatement:
|
||||
case SyntaxKind.DoStatement:
|
||||
|
||||
@@ -416,8 +416,8 @@ module ts.BreakpointResolver {
|
||||
var classDeclaration = <ClassDeclaration>node.parent;
|
||||
return spanInNodeIfStartsOnSameLine(findPrecedingToken(node.pos, sourceFile, node.parent), classDeclaration.members.length ? classDeclaration.members[0] : classDeclaration.getLastToken(sourceFile));
|
||||
|
||||
case SyntaxKind.SwitchStatement:
|
||||
return spanInNodeIfStartsOnSameLine(node.parent, (<SwitchStatement>node.parent).clauses[0]);
|
||||
case SyntaxKind.CaseBlock:
|
||||
return spanInNodeIfStartsOnSameLine(node.parent.parent, (<CaseBlock>node.parent).clauses[0]);
|
||||
}
|
||||
|
||||
// Default to parent node
|
||||
@@ -447,10 +447,10 @@ module ts.BreakpointResolver {
|
||||
case SyntaxKind.CatchClause:
|
||||
return spanInNode((<Block>node.parent).statements[(<Block>node.parent).statements.length - 1]);;
|
||||
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
// breakpoint in last statement of the last clause
|
||||
var switchStatement = <SwitchStatement>node.parent;
|
||||
var lastClause = switchStatement.clauses[switchStatement.clauses.length - 1];
|
||||
var caseBlock = <CaseBlock>node.parent;
|
||||
var lastClause = caseBlock.clauses[caseBlock.clauses.length - 1];
|
||||
if (lastClause) {
|
||||
return spanInNode(lastClause.statements[lastClause.statements.length - 1]);
|
||||
}
|
||||
|
||||
@@ -541,7 +541,7 @@ module ts.formatting {
|
||||
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Block:
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
case SyntaxKind.ModuleBlock:
|
||||
return true;
|
||||
|
||||
@@ -357,7 +357,7 @@ module ts.formatting {
|
||||
case SyntaxKind.ModuleBlock:
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
case SyntaxKind.TypeLiteral:
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
case SyntaxKind.DefaultClause:
|
||||
case SyntaxKind.CaseClause:
|
||||
case SyntaxKind.ParenthesizedExpression:
|
||||
@@ -431,7 +431,7 @@ module ts.formatting {
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
case SyntaxKind.Block:
|
||||
case SyntaxKind.ModuleBlock:
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
return nodeEndsWith(n, SyntaxKind.CloseBraceToken, sourceFile);
|
||||
case SyntaxKind.CatchClause:
|
||||
return isCompletedNode((<CatchClause>n).block, sourceFile);
|
||||
|
||||
@@ -104,7 +104,7 @@ module ts {
|
||||
case SyntaxKind.InterfaceDeclaration:
|
||||
case SyntaxKind.EnumDeclaration:
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
case SyntaxKind.SwitchStatement:
|
||||
case SyntaxKind.CaseBlock:
|
||||
var openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile);
|
||||
var closeBrace = findChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile);
|
||||
addOutliningSpan(n, openBrace, closeBrace, autoCollapse(n));
|
||||
|
||||
@@ -3619,8 +3619,8 @@ module ts {
|
||||
break;
|
||||
case SyntaxKind.CaseKeyword:
|
||||
case SyntaxKind.DefaultKeyword:
|
||||
if (hasKind(parent(parent(node)), SyntaxKind.SwitchStatement)) {
|
||||
return getSwitchCaseDefaultOccurrences(<SwitchStatement>node.parent.parent);
|
||||
if (hasKind(parent(parent(parent(node))), SyntaxKind.SwitchStatement)) {
|
||||
return getSwitchCaseDefaultOccurrences(<SwitchStatement>node.parent.parent.parent);
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.BreakKeyword:
|
||||
@@ -3887,7 +3887,7 @@ module ts {
|
||||
pushKeywordIf(keywords, switchStatement.getFirstToken(), SyntaxKind.SwitchKeyword);
|
||||
|
||||
// Go through each clause in the switch statement, collecting the 'case'/'default' keywords.
|
||||
forEach(switchStatement.clauses, clause => {
|
||||
forEach(switchStatement.caseBlock.clauses, clause => {
|
||||
pushKeywordIf(keywords, clause.getFirstToken(), SyntaxKind.CaseKeyword, SyntaxKind.DefaultKeyword);
|
||||
|
||||
var breaksAndContinues = aggregateAllBreakAndContinueStatements(clause);
|
||||
|
||||
Reference in New Issue
Block a user