Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3539,7 +3539,11 @@ module ts {
emit(node.expression);
endPos = emitToken(SyntaxKind.CloseParenToken, node.expression.end);
write(" ");
emitToken(SyntaxKind.OpenBraceToken, endPos);
emitCaseBlock(node.caseBlock, endPos)
}

function emitCaseBlock(node: CaseBlock, startPos: number): void {
emitToken(SyntaxKind.OpenBraceToken, startPos);
increaseIndent();
emitLines(node.clauses);
decreaseIndent();
Expand Down Expand Up @@ -3938,7 +3942,7 @@ module ts {
}
switch (current.kind) {
case SyntaxKind.SourceFile:
case SyntaxKind.SwitchKeyword:
case SyntaxKind.CaseBlock:
case SyntaxKind.CatchClause:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.ForStatement:
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ module ts {
EnumDeclaration,
ModuleDeclaration,
ModuleBlock,
CaseBlock,
ImportEqualsDeclaration,
ImportDeclaration,
ImportClause,
Expand Down Expand Up @@ -790,6 +791,10 @@ module ts {

export interface SwitchStatement extends Statement {
expression: Expression;
caseBlock: CaseBlock;
}

export interface CaseBlock extends Node {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just add a comment that this is more like a SwitchBlock? It's somewhat misleading

clauses: NodeArray<CaseOrDefaultClause>;
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions src/services/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ module ts.formatting {
case SyntaxKind.ModuleBlock:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.TypeLiteral:
case SyntaxKind.SwitchStatement:
case SyntaxKind.CaseBlock:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CaseClause Never mind

case SyntaxKind.DefaultClause:
case SyntaxKind.CaseClause:
case SyntaxKind.ParenthesizedExpression:
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/services/outliningElementsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 25 additions & 21 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,27 +261,28 @@ declare module "typescript" {
EnumDeclaration = 199,
ModuleDeclaration = 200,
ModuleBlock = 201,
ImportEqualsDeclaration = 202,
ImportDeclaration = 203,
ImportClause = 204,
NamespaceImport = 205,
NamedImports = 206,
ImportSpecifier = 207,
ExportAssignment = 208,
ExportDeclaration = 209,
NamedExports = 210,
ExportSpecifier = 211,
ExternalModuleReference = 212,
CaseClause = 213,
DefaultClause = 214,
HeritageClause = 215,
CatchClause = 216,
PropertyAssignment = 217,
ShorthandPropertyAssignment = 218,
EnumMember = 219,
SourceFile = 220,
SyntaxList = 221,
Count = 222,
CaseBlock = 202,
ImportEqualsDeclaration = 203,
ImportDeclaration = 204,
ImportClause = 205,
NamespaceImport = 206,
NamedImports = 207,
ImportSpecifier = 208,
ExportAssignment = 209,
ExportDeclaration = 210,
NamedExports = 211,
ExportSpecifier = 212,
ExternalModuleReference = 213,
CaseClause = 214,
DefaultClause = 215,
HeritageClause = 216,
CatchClause = 217,
PropertyAssignment = 218,
ShorthandPropertyAssignment = 219,
EnumMember = 220,
SourceFile = 221,
SyntaxList = 222,
Count = 223,
FirstAssignment = 52,
LastAssignment = 63,
FirstReservedWord = 65,
Expand Down Expand Up @@ -656,6 +657,9 @@ declare module "typescript" {
}
interface SwitchStatement extends Statement {
expression: Expression;
caseBlock: CaseBlock;
}
interface CaseBlock extends Node {
clauses: NodeArray<CaseOrDefaultClause>;
}
interface CaseClause extends Node {
Expand Down
53 changes: 32 additions & 21 deletions tests/baselines/reference/APISample_compile.types
Original file line number Diff line number Diff line change
Expand Up @@ -801,67 +801,70 @@ declare module "typescript" {
ModuleBlock = 201,
>ModuleBlock : SyntaxKind

ImportEqualsDeclaration = 202,
CaseBlock = 202,
>CaseBlock : SyntaxKind

ImportEqualsDeclaration = 203,
>ImportEqualsDeclaration : SyntaxKind

ImportDeclaration = 203,
ImportDeclaration = 204,
>ImportDeclaration : SyntaxKind

ImportClause = 204,
ImportClause = 205,
>ImportClause : SyntaxKind

NamespaceImport = 205,
NamespaceImport = 206,
>NamespaceImport : SyntaxKind

NamedImports = 206,
NamedImports = 207,
>NamedImports : SyntaxKind

ImportSpecifier = 207,
ImportSpecifier = 208,
>ImportSpecifier : SyntaxKind

ExportAssignment = 208,
ExportAssignment = 209,
>ExportAssignment : SyntaxKind

ExportDeclaration = 209,
ExportDeclaration = 210,
>ExportDeclaration : SyntaxKind

NamedExports = 210,
NamedExports = 211,
>NamedExports : SyntaxKind

ExportSpecifier = 211,
ExportSpecifier = 212,
>ExportSpecifier : SyntaxKind

ExternalModuleReference = 212,
ExternalModuleReference = 213,
>ExternalModuleReference : SyntaxKind

CaseClause = 213,
CaseClause = 214,
>CaseClause : SyntaxKind

DefaultClause = 214,
DefaultClause = 215,
>DefaultClause : SyntaxKind

HeritageClause = 215,
HeritageClause = 216,
>HeritageClause : SyntaxKind

CatchClause = 216,
CatchClause = 217,
>CatchClause : SyntaxKind

PropertyAssignment = 217,
PropertyAssignment = 218,
>PropertyAssignment : SyntaxKind

ShorthandPropertyAssignment = 218,
ShorthandPropertyAssignment = 219,
>ShorthandPropertyAssignment : SyntaxKind

EnumMember = 219,
EnumMember = 220,
>EnumMember : SyntaxKind

SourceFile = 220,
SourceFile = 221,
>SourceFile : SyntaxKind

SyntaxList = 221,
SyntaxList = 222,
>SyntaxList : SyntaxKind

Count = 222,
Count = 223,
>Count : SyntaxKind

FirstAssignment = 52,
Expand Down Expand Up @@ -1980,6 +1983,14 @@ declare module "typescript" {
>expression : Expression
>Expression : Expression

caseBlock: CaseBlock;
>caseBlock : CaseBlock
>CaseBlock : CaseBlock
}
interface CaseBlock extends Node {
>CaseBlock : CaseBlock
>Node : Node

clauses: NodeArray<CaseOrDefaultClause>;
>clauses : NodeArray<CaseClause | DefaultClause>
>NodeArray : NodeArray<T>
Expand Down
Loading