Report grammatic and type-based unreachable code errors in the same way

This commit is contained in:
Anders Hejlsberg
2019-09-16 15:30:45 -07:00
parent def5e37e6a
commit 6d6c620cc2
3 changed files with 11 additions and 6 deletions

View File

@@ -672,6 +672,9 @@ namespace ts {
bindJSDoc(node);
return;
}
if (node.kind >= SyntaxKind.FirstStatement && node.kind <= SyntaxKind.LastStatement) {
node.flowNode = currentFlow;
}
switch (node.kind) {
case SyntaxKind.WhileStatement:
bindWhileStatement(<WhileStatement>node);

View File

@@ -17057,11 +17057,7 @@ namespace ts {
// on empty arrays are possible without implicit any errors and new element types can be inferred without
// type mismatch errors.
const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType);
if (resultType === unreachableNeverType) {
error(reference, Diagnostics.Unreachable_code_detected);
return declaredType;
}
if (reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) {
if (resultType === unreachableNeverType|| reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) {
return declaredType;
}
return resultType;
@@ -30533,6 +30529,10 @@ namespace ts {
cancellationToken.throwIfCancellationRequested();
}
}
if (kind >= SyntaxKind.FirstStatement && kind <= SyntaxKind.LastStatement &&
!compilerOptions.allowUnreachableCode && node.flowNode && !isReachableFlowNode(node.flowNode)) {
errorOrSuggestion(compilerOptions.allowUnreachableCode === false, node, Diagnostics.Unreachable_code_detected);
}
switch (kind) {
case SyntaxKind.TypeParameter:

View File

@@ -363,8 +363,8 @@ namespace ts {
SemicolonClassElement,
// Element
Block,
VariableStatement,
EmptyStatement,
VariableStatement,
ExpressionStatement,
IfStatement,
DoStatement,
@@ -514,6 +514,8 @@ namespace ts {
LastTemplateToken = TemplateTail,
FirstBinaryOperator = LessThanToken,
LastBinaryOperator = CaretEqualsToken,
FirstStatement = VariableStatement,
LastStatement = DebuggerStatement,
FirstNode = QualifiedName,
FirstJSDocNode = JSDocTypeExpression,
LastJSDocNode = JSDocPropertyTag,