disallow references to function locals from return type annotations

This commit is contained in:
Vladimir Matveev 2015-11-17 15:22:26 -08:00
parent e0a8af00a7
commit 31039a3fff
4 changed files with 34 additions and 5 deletions

View file

@ -492,12 +492,16 @@ namespace ts {
: false;
}
if (meaning & SymbolFlags.Value && result.flags & SymbolFlags.FunctionScopedVariable) {
// function scoped variables are visible only inside function body and parameter list
// technically here we might mix parameters and variables declared in function,
// however this case is detected separately when checking initializers of parameters
// parameters are visible only inside function body, parameter list and return type
// technically for parameter list case here we might mix parameters and variables declared in function,
// however it is detected separately when checking initializers of parameters
// to make sure that they reference no variables declared after them.
useResult = lastLocation === (<FunctionLikeDeclaration>location).type ||
lastLocation.kind === SyntaxKind.Parameter;
useResult =
lastLocation.kind === SyntaxKind.Parameter ||
(
lastLocation === (<FunctionLikeDeclaration>location).type &&
result.valueDeclaration.kind === SyntaxKind.Parameter
);
}
}

View file

@ -0,0 +1,10 @@
tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts(1,24): error TS2304: Cannot find name 'b'.
==== tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts (1 errors) ====
function bar(): typeof b {
~
!!! error TS2304: Cannot find name 'b'.
var b = 1;
return undefined;
}

View file

@ -0,0 +1,11 @@
//// [functionVariableInReturnTypeAnnotation.ts]
function bar(): typeof b {
var b = 1;
return undefined;
}
//// [functionVariableInReturnTypeAnnotation.js]
function bar() {
var b = 1;
return undefined;
}

View file

@ -0,0 +1,4 @@
function bar(): typeof b {
var b = 1;
return undefined;
}