Skip to content

Commit 8869f39

Browse files
committed
accept more case
1 parent d758075 commit 8869f39

11 files changed

+99
-18
lines changed

src/compiler/checker.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1196,12 +1196,12 @@ namespace ts {
11961196
// local types not visible outside the function body
11971197
: false;
11981198
}
1199-
if (meaning & SymbolFlags.Value && result.flags & SymbolFlags.FunctionScopedVariable) {
1199+
if (meaning & SymbolFlags.Value && result.flags & SymbolFlags.Variable) {
12001200
// parameter initializer will lookup as normal variable scope when targeting es2015+
1201-
if (compilerOptions.target && compilerOptions.target >= ScriptTarget.ES2015 && isParameter(lastLocation) && lastLocation.initializer === originalLocation && result.valueDeclaration !== lastLocation) {
1201+
if (compilerOptions.target && compilerOptions.target >= ScriptTarget.ES2015 && isParameter(lastLocation) && result.valueDeclaration !== lastLocation) {
12021202
useResult = false;
12031203
}
1204-
else {
1204+
else if (result.flags & SymbolFlags.FunctionScopedVariable) {
12051205
// parameters are visible only inside function body, parameter list and return type
12061206
// technically for parameter list case here we might mix parameters and variables declared in function,
12071207
// however it is detected separately when checking initializers of parameters

tests/baselines/reference/parameterInitializersForwardReferencing1.errors.txt

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(8,
33
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(13,20): error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it.
44
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(21,18): error TS2372: Parameter 'a' cannot be referenced in its initializer.
55
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(25,22): error TS2372: Parameter 'async' cannot be referenced in its initializer.
6+
tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(29,15): error TS2448: Block-scoped variable 'foo' used before its declaration.
67

78

8-
==== tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts (5 errors) ====
9+
==== tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts (6 errors) ====
910
let foo: string = "";
1011

1112
function f1 (bar = foo) { // unexpected compiler error; works at runtime
@@ -42,4 +43,12 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(25
4243
~~~~~
4344
!!! error TS2372: Parameter 'async' cannot be referenced in its initializer.
4445
return async
45-
}
46+
}
47+
48+
function f7({[foo]: bar}: any[]) {
49+
~~~
50+
!!! error TS2448: Block-scoped variable 'foo' used before its declaration.
51+
!!! related TS2728 tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts:30:9: 'foo' is declared here.
52+
let foo: number = 2;
53+
}
54+

tests/baselines/reference/parameterInitializersForwardReferencing1.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ function f5 (a = a) {
2525

2626
function f6 (async = async) {
2727
return async
28-
}
28+
}
29+
30+
function f7({[foo]: bar}: any[]) {
31+
let foo: number = 2;
32+
}
33+
2934

3035
//// [parameterInitializersForwardReferencing1.js]
3136
var foo = "";
@@ -59,3 +64,7 @@ function f6(async) {
5964
if (async === void 0) { async = async; }
6065
return async;
6166
}
67+
function f7(_a) {
68+
var _b = foo, bar = _a[_b];
69+
var foo = 2;
70+
}

tests/baselines/reference/parameterInitializersForwardReferencing1.symbols

+10
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ function f6 (async = async) {
6565
return async
6666
>async : Symbol(async, Decl(parameterInitializersForwardReferencing1.ts, 24, 13))
6767
}
68+
69+
function f7({[foo]: bar}: any[]) {
70+
>f7 : Symbol(f7, Decl(parameterInitializersForwardReferencing1.ts, 26, 1))
71+
>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 29, 7))
72+
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 28, 13))
73+
74+
let foo: number = 2;
75+
>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 29, 7))
76+
}
77+

tests/baselines/reference/parameterInitializersForwardReferencing1.types

+11
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ function f6 (async = async) {
7171
return async
7272
>async : any
7373
}
74+
75+
function f7({[foo]: bar}: any[]) {
76+
>f7 : ({ [foo]: bar }: any[]) => void
77+
>foo : number
78+
>bar : any
79+
80+
let foo: number = 2;
81+
>foo : number
82+
>2 : 2
83+
}
84+

tests/baselines/reference/parameterInitializersForwardReferencing1_es6.errors.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.t
1111
}
1212

1313
function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime
14-
var fooo: number = 2;
14+
var foo: number = 2;
1515
return bar(); // returns 1
1616
}
1717

@@ -33,4 +33,9 @@ tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.t
3333
~~~~~
3434
!!! error TS2372: Parameter 'async' cannot be referenced in its initializer.
3535
return async
36-
}
36+
}
37+
38+
function f7({[foo]: bar}: any[]) {
39+
let foo: number = 2;
40+
}
41+

tests/baselines/reference/parameterInitializersForwardReferencing1_es6.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function f1 (bar = foo) { // unexpected compiler error; works at runtime
77
}
88

99
function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime
10-
var fooo: number = 2;
10+
var foo: number = 2;
1111
return bar(); // returns 1
1212
}
1313

@@ -25,7 +25,12 @@ function f5 (a = a) {
2525

2626
function f6 (async = async) {
2727
return async
28-
}
28+
}
29+
30+
function f7({[foo]: bar}: any[]) {
31+
let foo: number = 2;
32+
}
33+
2934

3035
//// [parameterInitializersForwardReferencing1_es6.js]
3136
let foo = "";
@@ -34,7 +39,7 @@ function f1(bar = foo) {
3439
return bar; // returns 1
3540
}
3641
function f2(bar = (baz = foo) => baz) {
37-
var fooo = 2;
42+
var foo = 2;
3843
return bar(); // returns 1
3944
}
4045
function f3(bar = foo, foo = 2) {
@@ -49,3 +54,6 @@ function f5(a = a) {
4954
function f6(async = async) {
5055
return async;
5156
}
57+
function f7({ [foo]: bar }) {
58+
let foo = 2;
59+
}

tests/baselines/reference/parameterInitializersForwardReferencing1_es6.symbols

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at
2121
>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3))
2222
>baz : Symbol(baz, Decl(parameterInitializersForwardReferencing1_es6.ts, 7, 20))
2323

24-
var fooo: number = 2;
25-
>fooo : Symbol(fooo, Decl(parameterInitializersForwardReferencing1_es6.ts, 8, 7))
24+
var foo: number = 2;
25+
>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 8, 7))
2626

2727
return bar(); // returns 1
2828
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 7, 13))
@@ -65,3 +65,13 @@ function f6 (async = async) {
6565
return async
6666
>async : Symbol(async, Decl(parameterInitializersForwardReferencing1_es6.ts, 24, 13))
6767
}
68+
69+
function f7({[foo]: bar}: any[]) {
70+
>f7 : Symbol(f7, Decl(parameterInitializersForwardReferencing1_es6.ts, 26, 1))
71+
>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3))
72+
>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 28, 13))
73+
74+
let foo: number = 2;
75+
>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 29, 7))
76+
}
77+

tests/baselines/reference/parameterInitializersForwardReferencing1_es6.types

+13-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at
2424
>foo : string
2525
>baz : string
2626

27-
var fooo: number = 2;
28-
>fooo : number
27+
var foo: number = 2;
28+
>foo : number
2929
>2 : 2
3030

3131
return bar(); // returns 1
@@ -71,3 +71,14 @@ function f6 (async = async) {
7171
return async
7272
>async : any
7373
}
74+
75+
function f7({[foo]: bar}: any[]) {
76+
>f7 : ({ [foo]: bar }: any[]) => void
77+
>foo : string
78+
>bar : any
79+
80+
let foo: number = 2;
81+
>foo : number
82+
>2 : 2
83+
}
84+

tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ function f5 (a = a) {
2424

2525
function f6 (async = async) {
2626
return async
27-
}
27+
}
28+
29+
function f7({[foo]: bar}: any[]) {
30+
let foo: number = 2;
31+
}

tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function f1 (bar = foo) { // unexpected compiler error; works at runtime
88
}
99

1010
function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime
11-
var fooo: number = 2;
11+
var foo: number = 2;
1212
return bar(); // returns 1
1313
}
1414

@@ -26,4 +26,8 @@ function f5 (a = a) {
2626

2727
function f6 (async = async) {
2828
return async
29-
}
29+
}
30+
31+
function f7({[foo]: bar}: any[]) {
32+
let foo: number = 2;
33+
}

0 commit comments

Comments
 (0)