Skip to content

Commit 31039a3

Browse files
committed
disallow references to function locals from return type annotations
1 parent e0a8af0 commit 31039a3

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/compiler/checker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,16 @@ namespace ts {
492492
: false;
493493
}
494494
if (meaning & SymbolFlags.Value && result.flags & SymbolFlags.FunctionScopedVariable) {
495-
// function scoped variables are visible only inside function body and parameter list
496-
// technically here we might mix parameters and variables declared in function,
497-
// however this case is detected separately when checking initializers of parameters
495+
// parameters are visible only inside function body, parameter list and return type
496+
// technically for parameter list case here we might mix parameters and variables declared in function,
497+
// however it is detected separately when checking initializers of parameters
498498
// to make sure that they reference no variables declared after them.
499-
useResult = lastLocation === (<FunctionLikeDeclaration>location).type ||
500-
lastLocation.kind === SyntaxKind.Parameter;
499+
useResult =
500+
lastLocation.kind === SyntaxKind.Parameter ||
501+
(
502+
lastLocation === (<FunctionLikeDeclaration>location).type &&
503+
result.valueDeclaration.kind === SyntaxKind.Parameter
504+
);
501505
}
502506
}
503507

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts(1,24): error TS2304: Cannot find name 'b'.
2+
3+
4+
==== tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts (1 errors) ====
5+
function bar(): typeof b {
6+
~
7+
!!! error TS2304: Cannot find name 'b'.
8+
var b = 1;
9+
return undefined;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [functionVariableInReturnTypeAnnotation.ts]
2+
function bar(): typeof b {
3+
var b = 1;
4+
return undefined;
5+
}
6+
7+
//// [functionVariableInReturnTypeAnnotation.js]
8+
function bar() {
9+
var b = 1;
10+
return undefined;
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function bar(): typeof b {
2+
var b = 1;
3+
return undefined;
4+
}

0 commit comments

Comments
 (0)