diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 51eeb0fdb48..36c6d71f672 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1197,16 +1197,22 @@ namespace ts { : false; } if (meaning & SymbolFlags.Value && result.flags & SymbolFlags.FunctionScopedVariable) { - // 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 = + // parameter initializer will lookup as normal variable scope when targeting es2015+ + if (compilerOptions.target && compilerOptions.target >= ScriptTarget.ES2015 && isParameter(lastLocation) && lastLocation.initializer === originalLocation && result.valueDeclaration !== lastLocation) { + useResult = false; + } + else { + // 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.kind === SyntaxKind.Parameter || ( lastLocation === (location).type && !!findAncestor(result.valueDeclaration, isParameter) ); + } } } else if (location.kind === SyntaxKind.ConditionalType) { diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1.errors.txt b/tests/baselines/reference/parameterInitializersForwardReferencing1.errors.txt new file mode 100644 index 00000000000..9879ec320ca --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1.errors.txt @@ -0,0 +1,45 @@ +tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(3,20): error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it. +tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(8,27): error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it. +tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts(13,20): error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it. +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 (5 errors) ==== + let foo: string = ""; + + function f1 (bar = foo) { // unexpected compiler error; works at runtime + ~~~ +!!! error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it. + var foo: number = 2; + return bar; // returns 1 + } + + function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime + ~~~ +!!! error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it. + var foo: number = 2; + return bar(); // returns 1 + } + + function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime + ~~~ +!!! error TS2373: Initializer of parameter 'bar' cannot reference identifier 'foo' declared after it. + return bar; + } + + function f4 (foo, bar = foo) { + return bar + } + + function f5 (a = a) { + ~ +!!! error TS2372: Parameter 'a' cannot be referenced in its initializer. + return a + } + + function f6 (async = async) { + ~~~~~ +!!! error TS2372: Parameter 'async' cannot be referenced in its initializer. + return async + } \ No newline at end of file diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1.js b/tests/baselines/reference/parameterInitializersForwardReferencing1.js new file mode 100644 index 00000000000..43f303a94cd --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1.js @@ -0,0 +1,61 @@ +//// [parameterInitializersForwardReferencing1.ts] +let foo: string = ""; + +function f1 (bar = foo) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar; // returns 1 +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar(); // returns 1 +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime + return bar; +} + +function f4 (foo, bar = foo) { + return bar +} + +function f5 (a = a) { + return a +} + +function f6 (async = async) { + return async +} + +//// [parameterInitializersForwardReferencing1.js] +var foo = ""; +function f1(bar) { + if (bar === void 0) { bar = foo; } + var foo = 2; + return bar; // returns 1 +} +function f2(bar) { + if (bar === void 0) { bar = function (baz) { + if (baz === void 0) { baz = foo; } + return baz; + }; } + var foo = 2; + return bar(); // returns 1 +} +function f3(bar, foo) { + if (bar === void 0) { bar = foo; } + if (foo === void 0) { foo = 2; } + return bar; +} +function f4(foo, bar) { + if (bar === void 0) { bar = foo; } + return bar; +} +function f5(a) { + if (a === void 0) { a = a; } + return a; +} +function f6(async) { + if (async === void 0) { async = async; } + return async; +} diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1.symbols b/tests/baselines/reference/parameterInitializersForwardReferencing1.symbols new file mode 100644 index 00000000000..6860e39fd8d --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1.symbols @@ -0,0 +1,67 @@ +=== tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts === +let foo: string = ""; +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 0, 3)) + +function f1 (bar = foo) { // unexpected compiler error; works at runtime +>f1 : Symbol(f1, Decl(parameterInitializersForwardReferencing1.ts, 0, 21)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 2, 13)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 3, 7)) + + var foo: number = 2; +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 3, 7)) + + return bar; // returns 1 +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 2, 13)) +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime +>f2 : Symbol(f2, Decl(parameterInitializersForwardReferencing1.ts, 5, 1)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 7, 13)) +>baz : Symbol(baz, Decl(parameterInitializersForwardReferencing1.ts, 7, 20)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 8, 7)) +>baz : Symbol(baz, Decl(parameterInitializersForwardReferencing1.ts, 7, 20)) + + var foo: number = 2; +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 8, 7)) + + return bar(); // returns 1 +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 7, 13)) +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime +>f3 : Symbol(f3, Decl(parameterInitializersForwardReferencing1.ts, 10, 1)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 12, 13)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 12, 23)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 12, 23)) + + return bar; +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 12, 13)) +} + +function f4 (foo, bar = foo) { +>f4 : Symbol(f4, Decl(parameterInitializersForwardReferencing1.ts, 14, 1)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 16, 13)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 16, 17)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1.ts, 16, 13)) + + return bar +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1.ts, 16, 17)) +} + +function f5 (a = a) { +>f5 : Symbol(f5, Decl(parameterInitializersForwardReferencing1.ts, 18, 1)) +>a : Symbol(a, Decl(parameterInitializersForwardReferencing1.ts, 20, 13)) +>a : Symbol(a, Decl(parameterInitializersForwardReferencing1.ts, 20, 13)) + + return a +>a : Symbol(a, Decl(parameterInitializersForwardReferencing1.ts, 20, 13)) +} + +function f6 (async = async) { +>f6 : Symbol(f6, Decl(parameterInitializersForwardReferencing1.ts, 22, 1)) +>async : Symbol(async, Decl(parameterInitializersForwardReferencing1.ts, 24, 13)) +>async : Symbol(async, Decl(parameterInitializersForwardReferencing1.ts, 24, 13)) + + return async +>async : Symbol(async, Decl(parameterInitializersForwardReferencing1.ts, 24, 13)) +} diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1.types b/tests/baselines/reference/parameterInitializersForwardReferencing1.types new file mode 100644 index 00000000000..c33c227a37a --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1.types @@ -0,0 +1,73 @@ +=== tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts === +let foo: string = ""; +>foo : string +>"" : "" + +function f1 (bar = foo) { // unexpected compiler error; works at runtime +>f1 : (bar?: number) => number +>bar : number +>foo : number + + var foo: number = 2; +>foo : number +>2 : 2 + + return bar; // returns 1 +>bar : number +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime +>f2 : (bar?: (baz?: number) => number) => number +>bar : (baz?: number) => number +>(baz = foo) => baz : (baz?: number) => number +>baz : number +>foo : number +>baz : number + + var foo: number = 2; +>foo : number +>2 : 2 + + return bar(); // returns 1 +>bar() : number +>bar : (baz?: number) => number +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime +>f3 : (bar?: number, foo?: number) => number +>bar : number +>foo : number +>foo : number +>2 : 2 + + return bar; +>bar : number +} + +function f4 (foo, bar = foo) { +>f4 : (foo: any, bar?: any) => any +>foo : any +>bar : any +>foo : any + + return bar +>bar : any +} + +function f5 (a = a) { +>f5 : (a?: any) => any +>a : any +>a : any + + return a +>a : any +} + +function f6 (async = async) { +>f6 : (async?: any) => any +>async : any +>async : any + + return async +>async : any +} diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.errors.txt b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.errors.txt new file mode 100644 index 00000000000..cffa83707e0 --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.errors.txt @@ -0,0 +1,36 @@ +tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts(21,18): error TS2372: Parameter 'a' cannot be referenced in its initializer. +tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts(25,22): error TS2372: Parameter 'async' cannot be referenced in its initializer. + + +==== tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts (2 errors) ==== + let foo: string = ""; + + function f1 (bar = foo) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar; // returns 1 + } + + function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime + var fooo: number = 2; + return bar(); // returns 1 + } + + function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime + return bar; + } + + function f4 (foo, bar = foo) { + return bar + } + + function f5 (a = a) { + ~ +!!! error TS2372: Parameter 'a' cannot be referenced in its initializer. + return a + } + + function f6 (async = async) { + ~~~~~ +!!! error TS2372: Parameter 'async' cannot be referenced in its initializer. + return async + } \ No newline at end of file diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.js b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.js new file mode 100644 index 00000000000..add0837e312 --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.js @@ -0,0 +1,51 @@ +//// [parameterInitializersForwardReferencing1_es6.ts] +let foo: string = ""; + +function f1 (bar = foo) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar; // returns 1 +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime + var fooo: number = 2; + return bar(); // returns 1 +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime + return bar; +} + +function f4 (foo, bar = foo) { + return bar +} + +function f5 (a = a) { + return a +} + +function f6 (async = async) { + return async +} + +//// [parameterInitializersForwardReferencing1_es6.js] +let foo = ""; +function f1(bar = foo) { + var foo = 2; + return bar; // returns 1 +} +function f2(bar = (baz = foo) => baz) { + var fooo = 2; + return bar(); // returns 1 +} +function f3(bar = foo, foo = 2) { + return bar; +} +function f4(foo, bar = foo) { + return bar; +} +function f5(a = a) { + return a; +} +function f6(async = async) { + return async; +} diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.symbols b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.symbols new file mode 100644 index 00000000000..c38a392c4d8 --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.symbols @@ -0,0 +1,67 @@ +=== tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts === +let foo: string = ""; +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3)) + +function f1 (bar = foo) { // unexpected compiler error; works at runtime +>f1 : Symbol(f1, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 21)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 2, 13)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3)) + + var foo: number = 2; +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 3, 7)) + + return bar; // returns 1 +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 2, 13)) +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime +>f2 : Symbol(f2, Decl(parameterInitializersForwardReferencing1_es6.ts, 5, 1)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 7, 13)) +>baz : Symbol(baz, Decl(parameterInitializersForwardReferencing1_es6.ts, 7, 20)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3)) +>baz : Symbol(baz, Decl(parameterInitializersForwardReferencing1_es6.ts, 7, 20)) + + var fooo: number = 2; +>fooo : Symbol(fooo, Decl(parameterInitializersForwardReferencing1_es6.ts, 8, 7)) + + return bar(); // returns 1 +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 7, 13)) +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime +>f3 : Symbol(f3, Decl(parameterInitializersForwardReferencing1_es6.ts, 10, 1)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 12, 13)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 12, 23)) + + return bar; +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 12, 13)) +} + +function f4 (foo, bar = foo) { +>f4 : Symbol(f4, Decl(parameterInitializersForwardReferencing1_es6.ts, 14, 1)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 16, 13)) +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 16, 17)) +>foo : Symbol(foo, Decl(parameterInitializersForwardReferencing1_es6.ts, 0, 3)) + + return bar +>bar : Symbol(bar, Decl(parameterInitializersForwardReferencing1_es6.ts, 16, 17)) +} + +function f5 (a = a) { +>f5 : Symbol(f5, Decl(parameterInitializersForwardReferencing1_es6.ts, 18, 1)) +>a : Symbol(a, Decl(parameterInitializersForwardReferencing1_es6.ts, 20, 13)) +>a : Symbol(a, Decl(parameterInitializersForwardReferencing1_es6.ts, 20, 13)) + + return a +>a : Symbol(a, Decl(parameterInitializersForwardReferencing1_es6.ts, 20, 13)) +} + +function f6 (async = async) { +>f6 : Symbol(f6, Decl(parameterInitializersForwardReferencing1_es6.ts, 22, 1)) +>async : Symbol(async, Decl(parameterInitializersForwardReferencing1_es6.ts, 24, 13)) +>async : Symbol(async, Decl(parameterInitializersForwardReferencing1_es6.ts, 24, 13)) + + return async +>async : Symbol(async, Decl(parameterInitializersForwardReferencing1_es6.ts, 24, 13)) +} diff --git a/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.types b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.types new file mode 100644 index 00000000000..bd17f482335 --- /dev/null +++ b/tests/baselines/reference/parameterInitializersForwardReferencing1_es6.types @@ -0,0 +1,73 @@ +=== tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts === +let foo: string = ""; +>foo : string +>"" : "" + +function f1 (bar = foo) { // unexpected compiler error; works at runtime +>f1 : (bar?: string) => string +>bar : string +>foo : string + + var foo: number = 2; +>foo : number +>2 : 2 + + return bar; // returns 1 +>bar : string +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime +>f2 : (bar?: (baz?: string) => string) => string +>bar : (baz?: string) => string +>(baz = foo) => baz : (baz?: string) => string +>baz : string +>foo : string +>baz : string + + var fooo: number = 2; +>fooo : number +>2 : 2 + + return bar(); // returns 1 +>bar() : string +>bar : (baz?: string) => string +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime +>f3 : (bar?: string, foo?: number) => string +>bar : string +>foo : string +>foo : number +>2 : 2 + + return bar; +>bar : string +} + +function f4 (foo, bar = foo) { +>f4 : (foo: any, bar?: string) => string +>foo : any +>bar : string +>foo : string + + return bar +>bar : string +} + +function f5 (a = a) { +>f5 : (a?: any) => any +>a : any +>a : any + + return a +>a : any +} + +function f6 (async = async) { +>f6 : (async?: any) => any +>async : any +>async : any + + return async +>async : any +} diff --git a/tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts b/tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts new file mode 100644 index 00000000000..8492a053856 --- /dev/null +++ b/tests/cases/conformance/functions/parameterInitializersForwardReferencing1.ts @@ -0,0 +1,27 @@ +let foo: string = ""; + +function f1 (bar = foo) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar; // returns 1 +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar(); // returns 1 +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime + return bar; +} + +function f4 (foo, bar = foo) { + return bar +} + +function f5 (a = a) { + return a +} + +function f6 (async = async) { + return async +} \ No newline at end of file diff --git a/tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts b/tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts new file mode 100644 index 00000000000..dfd6b8d4e1a --- /dev/null +++ b/tests/cases/conformance/functions/parameterInitializersForwardReferencing1_es6.ts @@ -0,0 +1,29 @@ +// @target: es2015 + +let foo: string = ""; + +function f1 (bar = foo) { // unexpected compiler error; works at runtime + var foo: number = 2; + return bar; // returns 1 +} + +function f2 (bar = (baz = foo) => baz) { // unexpected compiler error; works at runtime + var fooo: number = 2; + return bar(); // returns 1 +} + +function f3 (bar = foo, foo = 2) { // correct compiler error, error at runtime + return bar; +} + +function f4 (foo, bar = foo) { + return bar +} + +function f5 (a = a) { + return a +} + +function f6 (async = async) { + return async +} \ No newline at end of file