Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1262,8 +1262,9 @@ namespace ts {
if (meaning & result.flags & SymbolFlags.Variable) {
// expression inside parameter will lookup as normal variable scope when targeting es2015+
const functionLocation = <FunctionLikeDeclaration>location;
if (compilerOptions.target && compilerOptions.target >= ScriptTarget.ES2015 && isParameter(lastLocation) &&
functionLocation.body && result.valueDeclaration.pos >= functionLocation.body.pos && result.valueDeclaration.end <= functionLocation.body.end) {
if (compilerOptions.target && compilerOptions.target >= ScriptTarget.ES2015 &&
functionLocation.body && result.valueDeclaration.pos >= functionLocation.body.pos && result.valueDeclaration.end <= functionLocation.body.end &&
((findAncestor(originalLocation, isFunctionLikeDeclaration) !== functionLocation) || isParameter(lastLocation))) {
useResult = false;
}
else if (result.flags & SymbolFlags.FunctionScopedVariable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(13
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(21,18): error TS2372: Parameter 'a' cannot be referenced in its initializer.
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(25,22): error TS2372: Parameter 'async' cannot be referenced in its initializer.
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(29,15): error TS2448: Block-scoped variable 'foo' used before its declaration.
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(40,25): error TS2373: Initializer of parameter 'bar' cannot reference identifier 'bazzzz' declared after it.


==== tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts (6 errors) ====
==== tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts (7 errors) ====
let foo: string = "";

function f1 (bar = foo) { // unexpected compiler error; works at runtime
Expand Down Expand Up @@ -57,4 +58,12 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(29
}

function f8(foo1: string, bar = foo1) { }

let bazzzz = 2;
function f9(bar = () => bazzzz) {
~~~~~~
!!! error TS2373: Initializer of parameter 'bar' cannot reference identifier 'bazzzz' declared after it.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this error here? It doesn't reference the inner var bazzzz (var doesn't work any different than let and const in this case). And the outer let bazzzz is definitely not declared after the parameter.

var bazzzz = 1;
return bar();
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Foo {
}

function f8(foo1: string, bar = foo1) { }

let bazzzz = 2;
function f9(bar = () => bazzzz) {
var bazzzz = 1;
return bar();
}


//// [parameterInitializersForwardReferencing1.js]
Expand Down Expand Up @@ -86,3 +92,9 @@ var Foo = /** @class */ (function () {
function f8(foo1, bar) {
if (bar === void 0) { bar = foo1; }
}
var bazzzz = 2;
function f9(bar) {
if (bar === void 0) { bar = function () { return bazzzz; }; }
var bazzzz = 1;
return bar();
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ function f8(foo1: string, bar = foo1) { }
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 36, 25))
>foo1 : Symbol(foo1, Decl(parameterInitializersForwardReferencing1.ts, 36, 12))

let bazzzz = 2;
>bazzzz : Symbol(bazzzz, Decl(parameterInitializersForwardReferencing1.ts, 38, 3))

function f9(bar = () => bazzzz) {
>f9 : Symbol(f9, Decl(parameterInitializersForwardReferencing1.ts, 38, 15))
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 39, 12))
>bazzzz : Symbol(bazzzz, Decl(parameterInitializersForwardReferencing1.ts, 40, 7))

var bazzzz = 1;
>bazzzz : Symbol(bazzzz, Decl(parameterInitializersForwardReferencing1.ts, 40, 7))

return bar();
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 39, 12))
}

Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,22 @@ function f8(foo1: string, bar = foo1) { }
>bar : string
>foo1 : string

let bazzzz = 2;
>bazzzz : number
>2 : 2

function f9(bar = () => bazzzz) {
>f9 : (bar?: () => number) => number
>bar : () => number
>() => bazzzz : () => number
>bazzzz : number

var bazzzz = 1;
>bazzzz : number
>1 : 1

return bar();
>bar() : number
>bar : () => number
}

Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.t
}

function f8(foo1: string, bar = foo1) { }

let bazzzz = 2;
function f9(bar = () => bazzzz) {
var bazzzz = 1;
return bar();
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Foo {
}

function f8(foo1: string, bar = foo1) { }

let bazzzz = 2;
function f9(bar = () => bazzzz) {
var bazzzz = 1;
return bar();
}


//// [parameterInitializersForwardReferencing1_es6.js]
Expand Down Expand Up @@ -70,3 +76,8 @@ class Foo {
}
}
function f8(foo1, bar = foo1) { }
let bazzzz = 2;
function f9(bar = () => bazzzz) {
var bazzzz = 1;
return bar();
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ function f8(foo1: string, bar = foo1) { }
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 36, 25))
>foo1 : Symbol(foo1, Decl(parameterInitializersForwardReferencing1_es6.ts, 36, 12))

let bazzzz = 2;
>bazzzz : Symbol(bazzzz, Decl(parameterInitializersForwardReferencing1_es6.ts, 38, 3))

function f9(bar = () => bazzzz) {
>f9 : Symbol(f9, Decl(parameterInitializersForwardReferencing1_es6.ts, 38, 15))
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 39, 12))
>bazzzz : Symbol(bazzzz, Decl(parameterInitializersForwardReferencing1_es6.ts, 38, 3))

var bazzzz = 1;
>bazzzz : Symbol(bazzzz, Decl(parameterInitializersForwardReferencing1_es6.ts, 40, 7))

return bar();
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 39, 12))
}

Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,22 @@ function f8(foo1: string, bar = foo1) { }
>bar : string
>foo1 : string

let bazzzz = 2;
>bazzzz : number
>2 : 2

function f9(bar = () => bazzzz) {
>f9 : (bar?: () => number) => number
>bar : () => number
>() => bazzzz : () => number
>bazzzz : number

var bazzzz = 1;
>bazzzz : number
>1 : 1

return bar();
>bar() : number
>bar : () => number
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ class Foo {
}

function f8(foo1: string, bar = foo1) { }

let bazzzz = 2;
function f9(bar = () => bazzzz) {
var bazzzz = 1;
return bar();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ class Foo {
}

function f8(foo1: string, bar = foo1) { }

let bazzzz = 2;
function f9(bar = () => bazzzz) {
var bazzzz = 1;
return bar();
}