Skip to content
Merged
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
19 changes: 17 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3068,13 +3068,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isForInOrOfStatement(grandparent) && isSameScopeDescendentOf(usage, grandparent.expression, declContainer);
}

function isUsedInFunctionOrInstanceProperty(usage: Node, declaration: Node): boolean {
function isUsedInFunctionOrInstanceProperty(usage: Node, declaration: Node) {
return isUsedInFunctionOrInstancePropertyWorker(usage, declaration);
}

function isUsedInFunctionOrInstancePropertyWorker(usage: Node, declaration: Node): boolean {
return !!findAncestor(usage, current => {
if (current === declContainer) {
return "quit";
}
if (isFunctionLike(current)) {
return true;
return !getImmediatelyInvokedFunctionExpression(current);
}
if (isClassStaticBlockDeclaration(current)) {
return declaration.pos < usage.pos;
Expand Down Expand Up @@ -3107,6 +3111,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}

const decorator = tryCast(current.parent, isDecorator);
if (decorator && decorator.expression === current) {
if (isParameter(decorator.parent)) {
return isUsedInFunctionOrInstancePropertyWorker(decorator.parent.parent.parent, declaration) ? true : "quit";
}
if (isMethodDeclaration(decorator.parent)) {
return isUsedInFunctionOrInstancePropertyWorker(decorator.parent.parent, declaration) ? true : "quit";
}
}

return false;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ blockScopedVariablesUseBeforeDef.ts(100,12): error TS2448: Block-scoped variable
blockScopedVariablesUseBeforeDef.ts(111,28): error TS2448: Block-scoped variable 'a' used before its declaration.
blockScopedVariablesUseBeforeDef.ts(112,21): error TS2448: Block-scoped variable 'a' used before its declaration.
blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable 'a' used before its declaration.
blockScopedVariablesUseBeforeDef.ts(128,9): error TS2448: Block-scoped variable 'foo' used before its declaration.
blockScopedVariablesUseBeforeDef.ts(131,9): error TS2448: Block-scoped variable 'foo' used before its declaration.
blockScopedVariablesUseBeforeDef.ts(153,20): error TS2450: Enum 'Enum' used before its declaration.


==== blockScopedVariablesUseBeforeDef.ts (7 errors) ====
==== blockScopedVariablesUseBeforeDef.ts (10 errors) ====
function foo0() {
let a = x;
~
Expand Down Expand Up @@ -157,9 +160,15 @@ blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable
const promise = (async () => {
promise
foo
~~~
!!! error TS2448: Block-scoped variable 'foo' used before its declaration.
!!! related TS2728 blockScopedVariablesUseBeforeDef.ts:134:11: 'foo' is declared here.
await null
promise
foo
~~~
!!! error TS2448: Block-scoped variable 'foo' used before its declaration.
!!! related TS2728 blockScopedVariablesUseBeforeDef.ts:134:11: 'foo' is declared here.
})()

const foo = 1;
Expand All @@ -179,4 +188,15 @@ blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable
yield 1;
})();
}

function foo18() {
let a = (() => Enum.Yes)();
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 blockScopedVariablesUseBeforeDef.ts:154:10: 'Enum' is declared here.
enum Enum {
No = 0,
Yes = 1,
}
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/blockScopedVariablesUseBeforeDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ function wrapI2() {
yield 1;
})();
}

function foo18() {
let a = (() => Enum.Yes)();
enum Enum {
No = 0,
Yes = 1,
}
}


//// [blockScopedVariablesUseBeforeDef.js]
Expand Down Expand Up @@ -389,3 +397,11 @@ function wrapI2() {
});
})();
}
function foo18() {
var a = (function () { return Enum.Yes; })();
var Enum;
(function (Enum) {
Enum[Enum["No"] = 0] = "No";
Enum[Enum["Yes"] = 1] = "Yes";
})(Enum || (Enum = {}));
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/blockScopedVariablesUseBeforeDef.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,23 @@ function wrapI2() {
})();
}

function foo18() {
>foo18 : Symbol(foo18, Decl(blockScopedVariablesUseBeforeDef.ts, 149, 1))

let a = (() => Enum.Yes)();
>a : Symbol(a, Decl(blockScopedVariablesUseBeforeDef.ts, 152, 7))
>Enum.Yes : Symbol(Enum.Yes, Decl(blockScopedVariablesUseBeforeDef.ts, 154, 15))
>Enum : Symbol(Enum, Decl(blockScopedVariablesUseBeforeDef.ts, 152, 31))
>Yes : Symbol(Enum.Yes, Decl(blockScopedVariablesUseBeforeDef.ts, 154, 15))

enum Enum {
>Enum : Symbol(Enum, Decl(blockScopedVariablesUseBeforeDef.ts, 152, 31))

No = 0,
>No : Symbol(Enum.No, Decl(blockScopedVariablesUseBeforeDef.ts, 153, 15))

Yes = 1,
>Yes : Symbol(Enum.Yes, Decl(blockScopedVariablesUseBeforeDef.ts, 154, 15))
}
}

38 changes: 38 additions & 0 deletions tests/baselines/reference/blockScopedVariablesUseBeforeDef.types
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,41 @@ function wrapI2() {
})();
}

function foo18() {
>foo18 : () => void
> : ^^^^^^^^^^

let a = (() => Enum.Yes)();
>a : Enum
> : ^^^^
>(() => Enum.Yes)() : Enum
> : ^^^^
>(() => Enum.Yes) : () => Enum
> : ^^^^^^^^^^
>() => Enum.Yes : () => Enum
> : ^^^^^^^^^^
>Enum.Yes : Enum.Yes
> : ^^^^^^^^
>Enum : typeof Enum
> : ^^^^^^^^^^^
>Yes : Enum.Yes
> : ^^^^^^^^

enum Enum {
>Enum : Enum
> : ^^^^

No = 0,
>No : Enum.No
> : ^^^^^^^
>0 : 0
> : ^

Yes = 1,
>Yes : Enum.Yes
> : ^^^^^^^^
>1 : 1
> : ^
}
}

140 changes: 140 additions & 0 deletions tests/baselines/reference/decoratorUsedBeforeDeclaration.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
decoratorUsedBeforeDeclaration.ts(1,2): error TS2448: Block-scoped variable 'lambda' used before its declaration.
decoratorUsedBeforeDeclaration.ts(1,9): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(2,7): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(4,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
decoratorUsedBeforeDeclaration.ts(4,11): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(4,16): error TS2729: Property 'No' is used before its initialization.
decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(5,14): error TS2729: Property 'No' is used before its initialization.
decoratorUsedBeforeDeclaration.ts(12,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
decoratorUsedBeforeDeclaration.ts(12,11): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(13,9): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(18,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
decoratorUsedBeforeDeclaration.ts(24,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
decoratorUsedBeforeDeclaration.ts(24,18): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(24,33): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(28,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.


==== decoratorUsedBeforeDeclaration.ts (16 errors) ====
@lambda(Enum.No)
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
@deco(Enum.No)
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
class Greeter {
@lambda(Enum.No)
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
~~
!!! error TS2729: Property 'No' is used before its initialization.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
@deco(Enum.No)
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
~~
!!! error TS2729: Property 'No' is used before its initialization.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
greeting: string;

constructor(message: string) {
this.greeting = message;
}

@lambda(Enum.No)
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
@deco(Enum.No)
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
greet() {
return "Hello, " + this.greeting;
}

@lambda
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
@deco
greet1() {
return "Hello, " + this.greeting;
}

greet2(@lambda(Enum.No) @deco(Enum.No) param) {
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
return "Hello, " + this.greeting;
}

greet3(@lambda @deco param) {
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
return "Hello, " + this.greeting;
}
}

function deco(...args: any[]): any {}

enum Enum {
No = 0,
Yes = 1,
}

const lambda = (...args: any[]): any => {};

@lambda(Enum.No)
@deco(Enum.No)
class Greeter1 {
@lambda(Enum.No)
@deco(Enum.No)
greeting: string;

constructor(message: string) {
this.greeting = message;
}

@lambda(Enum.No)
@deco(Enum.No)
greet() {
return "Hello, " + this.greeting;
}

@lambda
@deco
greet1() {
return "Hello, " + this.greeting;
}

greet2(@lambda(Enum.No) @deco(Enum.No) param) {
return "Hello, " + this.greeting;
}

greet3(@lambda @deco param) {
return "Hello, " + this.greeting;
}
}

Loading