MCPcopy Index your code
hub / github.com/nodejs/node / checkSwitchStatement

Function checkSwitchStatement

test/fixtures/snapshot/typescript.js:83694–83741  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

83692 }
83693 }
83694 function checkSwitchStatement(node) {
83695 // Grammar checking
83696 checkGrammarStatementInAmbientContext(node);
83697 var firstDefaultClause;
83698 var hasDuplicateDefaultClause = false;
83699 var expressionType = checkExpression(node.expression);
83700 var expressionIsLiteral = isLiteralType(expressionType);
83701 ts.forEach(node.caseBlock.clauses, function (clause) {
83702 // Grammar check for duplicate default clauses, skip if we already report duplicate default clause
83703 if (clause.kind === 290 /* SyntaxKind.DefaultClause */ && !hasDuplicateDefaultClause) {
83704 if (firstDefaultClause === undefined) {
83705 firstDefaultClause = clause;
83706 }
83707 else {
83708 grammarErrorOnNode(clause, ts.Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement);
83709 hasDuplicateDefaultClause = true;
83710 }
83711 }
83712 if (clause.kind === 289 /* SyntaxKind.CaseClause */) {
83713 addLazyDiagnostic(createLazyCaseClauseDiagnostics(clause));
83714 }
83715 ts.forEach(clause.statements, checkSourceElement);
83716 if (compilerOptions.noFallthroughCasesInSwitch && clause.fallthroughFlowNode && isReachableFlowNode(clause.fallthroughFlowNode)) {
83717 error(clause, ts.Diagnostics.Fallthrough_case_in_switch);
83718 }
83719 function createLazyCaseClauseDiagnostics(clause) {
83720 return function () {
83721 // TypeScript 1.0 spec (April 2014): 5.9
83722 // In a 'switch' statement, each 'case' expression must be of a type that is comparable
83723 // to or from the type of the 'switch' expression.
83724 var caseType = checkExpression(clause.expression);
83725 var caseIsLiteral = isLiteralType(caseType);
83726 var comparedExpressionType = expressionType;
83727 if (!caseIsLiteral || !expressionIsLiteral) {
83728 caseType = caseIsLiteral ? getBaseTypeOfLiteralType(caseType) : caseType;
83729 comparedExpressionType = getBaseTypeOfLiteralType(expressionType);
83730 }
83731 if (!isTypeEqualityComparableTo(comparedExpressionType, caseType)) {
83732 // expressionType is not comparable to caseType, try the reversed check and report errors if it fails
83733 checkTypeComparableTo(caseType, comparedExpressionType, clause.expression, /*headMessage*/ undefined);
83734 }
83735 };
83736 }
83737 });
83738 if (node.caseBlock.locals) {
83739 registerForUnusedIdentifiersCheck(node.caseBlock);
83740 }
83741 }
83742 function checkLabeledStatement(node) {
83743 // Grammar checking
83744 if (!checkGrammarStatementInAmbientContext(node)) {

Callers 1

checkSourceElementWorkerFunction · 0.85

Calls 10

isLiteralTypeFunction · 0.85
grammarErrorOnNodeFunction · 0.85
addLazyDiagnosticFunction · 0.85
isReachableFlowNodeFunction · 0.85
checkExpressionFunction · 0.70
errorFunction · 0.70
forEachMethod · 0.65

Tested by

no test coverage detected