Fix #11660: wrong reports that block-scoped variable used before its … (#11692)

* Fix #11660: wrong reports that block-scoped variable used before its declaration

* Fix code style in checker.ts

* Add unit test for #11660

* Fix the unit test for #11660
This commit is contained in:
Dom Chen
2016-10-20 04:07:49 +08:00
committed by Mohamed Hegazy
parent 06afadda72
commit 0365c96e37
5 changed files with 139 additions and 4 deletions

View File

@@ -591,7 +591,11 @@ namespace ts {
// nodes are in different files and order cannot be determines
return true;
}
// declaration is after usage
// can be legal if usage is deferred (i.e. inside function or in initializer of instance property)
if (isUsedInFunctionOrNonStaticProperty(usage)) {
return true;
}
const sourceFiles = host.getSourceFiles();
return indexOf(sourceFiles, declarationFile) <= indexOf(sourceFiles, useFile);
}
@@ -605,7 +609,8 @@ namespace ts {
// declaration is after usage
// can be legal if usage is deferred (i.e. inside function or in initializer of instance property)
return isUsedInFunctionOrNonStaticProperty(declaration, usage);
const container = getEnclosingBlockScopeContainer(declaration);
return isUsedInFunctionOrNonStaticProperty(usage, container);
function isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration: VariableDeclaration, usage: Node): boolean {
const container = getEnclosingBlockScopeContainer(declaration);
@@ -634,8 +639,7 @@ namespace ts {
return false;
}
function isUsedInFunctionOrNonStaticProperty(declaration: Declaration, usage: Node): boolean {
const container = getEnclosingBlockScopeContainer(declaration);
function isUsedInFunctionOrNonStaticProperty(usage: Node, container?: Node): boolean {
let current = usage;
while (current) {
if (current === container) {