Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db6928e

Browse files
committed
Merge pull request microsoft#3227 from Microsoft/fixFunctionExpressionCheckForDecorators
Fix for microsoft#2971, adds missing logic in checkFunctionExpressionBodies
2 parents 0d5653e + 4b031ea commit db6928e

8 files changed

+233
-1
lines changed

src/compiler/checker.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6605,7 +6605,7 @@ module ts {
66056605
result.splice(spliceIndex, 0, signature);
66066606
}
66076607
}
6608-
6608+
66096609
function getSpreadArgumentIndex(args: Expression[]): number {
66106610
for (let i = 0; i < args.length; i++) {
66116611
if (args[i].kind === SyntaxKind.SpreadElementExpression) {
@@ -10914,6 +10914,7 @@ module ts {
1091410914
break;
1091510915
case SyntaxKind.MethodDeclaration:
1091610916
case SyntaxKind.MethodSignature:
10917+
forEach(node.decorators, checkFunctionExpressionBodies);
1091710918
forEach((<MethodDeclaration>node).parameters, checkFunctionExpressionBodies);
1091810919
if (isObjectLiteralMethod(node)) {
1091910920
checkFunctionExpressionOrObjectLiteralMethodBody(<MethodDeclaration>node);
@@ -10928,6 +10929,7 @@ module ts {
1092810929
case SyntaxKind.WithStatement:
1092910930
checkFunctionExpressionBodies((<WithStatement>node).expression);
1093010931
break;
10932+
case SyntaxKind.Decorator:
1093110933
case SyntaxKind.Parameter:
1093210934
case SyntaxKind.PropertyDeclaration:
1093310935
case SyntaxKind.PropertySignature:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/conformance/decorators/class/decoratorChecksFunctionBodies.ts(9,14): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
2+
3+
4+
==== tests/cases/conformance/decorators/class/decoratorChecksFunctionBodies.ts (1 errors) ====
5+
6+
// from #2971
7+
function func(s: string): void {
8+
}
9+
10+
class A {
11+
@(x => {
12+
var a = 3;
13+
func(a);
14+
~
15+
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
16+
return x;
17+
})
18+
m() {
19+
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [decoratorChecksFunctionBodies.ts]
2+
3+
// from #2971
4+
function func(s: string): void {
5+
}
6+
7+
class A {
8+
@(x => {
9+
var a = 3;
10+
func(a);
11+
return x;
12+
})
13+
m() {
14+
15+
}
16+
}
17+
18+
//// [decoratorChecksFunctionBodies.js]
19+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
20+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
21+
switch (arguments.length) {
22+
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
23+
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
24+
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
25+
}
26+
};
27+
// from #2971
28+
function func(s) {
29+
}
30+
var A = (function () {
31+
function A() {
32+
}
33+
A.prototype.m = function () {
34+
};
35+
Object.defineProperty(A.prototype, "m",
36+
__decorate([
37+
(function (x) {
38+
var a = 3;
39+
func(a);
40+
return x;
41+
})
42+
], A.prototype, "m", Object.getOwnPropertyDescriptor(A.prototype, "m")));
43+
return A;
44+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/conformance/decorators/class/decoratorInstantiateModulesInFunctionBodies.ts] ////
2+
3+
//// [a.ts]
4+
5+
// from #3108
6+
export var test = 'abc';
7+
8+
//// [b.ts]
9+
import { test } from './a';
10+
11+
function filter(handler: any) {
12+
return function (target: any) {
13+
// ...
14+
};
15+
}
16+
17+
class Wat {
18+
@filter(() => test == 'abc')
19+
static whatever() {
20+
// ...
21+
}
22+
}
23+
24+
//// [a.js]
25+
// from #3108
26+
exports.test = 'abc';
27+
//// [b.js]
28+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
29+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
30+
switch (arguments.length) {
31+
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
32+
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
33+
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
34+
}
35+
};
36+
var a_1 = require('./a');
37+
function filter(handler) {
38+
return function (target) {
39+
// ...
40+
};
41+
}
42+
var Wat = (function () {
43+
function Wat() {
44+
}
45+
Wat.whatever = function () {
46+
// ...
47+
};
48+
Object.defineProperty(Wat, "whatever",
49+
__decorate([
50+
filter(function () { return a_1.test == 'abc'; })
51+
], Wat, "whatever", Object.getOwnPropertyDescriptor(Wat, "whatever")));
52+
return Wat;
53+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/conformance/decorators/class/a.ts ===
2+
3+
// from #3108
4+
export var test = 'abc';
5+
>test : Symbol(test, Decl(a.ts, 2, 10))
6+
7+
=== tests/cases/conformance/decorators/class/b.ts ===
8+
import { test } from './a';
9+
>test : Symbol(test, Decl(b.ts, 0, 8))
10+
11+
function filter(handler: any) {
12+
>filter : Symbol(filter, Decl(b.ts, 0, 27))
13+
>handler : Symbol(handler, Decl(b.ts, 2, 16))
14+
15+
return function (target: any) {
16+
>target : Symbol(target, Decl(b.ts, 3, 21))
17+
18+
// ...
19+
};
20+
}
21+
22+
class Wat {
23+
>Wat : Symbol(Wat, Decl(b.ts, 6, 1))
24+
25+
@filter(() => test == 'abc')
26+
>filter : Symbol(filter, Decl(b.ts, 0, 27))
27+
>test : Symbol(test, Decl(b.ts, 0, 8))
28+
29+
static whatever() {
30+
>whatever : Symbol(Wat.whatever, Decl(b.ts, 8, 11))
31+
32+
// ...
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/conformance/decorators/class/a.ts ===
2+
3+
// from #3108
4+
export var test = 'abc';
5+
>test : string
6+
>'abc' : string
7+
8+
=== tests/cases/conformance/decorators/class/b.ts ===
9+
import { test } from './a';
10+
>test : string
11+
12+
function filter(handler: any) {
13+
>filter : (handler: any) => (target: any) => void
14+
>handler : any
15+
16+
return function (target: any) {
17+
>function (target: any) { // ... } : (target: any) => void
18+
>target : any
19+
20+
// ...
21+
};
22+
}
23+
24+
class Wat {
25+
>Wat : Wat
26+
27+
@filter(() => test == 'abc')
28+
>filter(() => test == 'abc') : (target: any) => void
29+
>filter : (handler: any) => (target: any) => void
30+
>() => test == 'abc' : () => boolean
31+
>test == 'abc' : boolean
32+
>test : string
33+
>'abc' : string
34+
35+
static whatever() {
36+
>whatever : () => void
37+
38+
// ...
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @target:es5
2+
3+
// from #2971
4+
function func(s: string): void {
5+
}
6+
7+
class A {
8+
@(x => {
9+
var a = 3;
10+
func(a);
11+
return x;
12+
})
13+
m() {
14+
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @target:es5
2+
// @module:commonjs
3+
// @filename: a.ts
4+
5+
// from #3108
6+
export var test = 'abc';
7+
8+
// @filename: b.ts
9+
import { test } from './a';
10+
11+
function filter(handler: any) {
12+
return function (target: any) {
13+
// ...
14+
};
15+
}
16+
17+
class Wat {
18+
@filter(() => test == 'abc')
19+
static whatever() {
20+
// ...
21+
}
22+
}

0 commit comments

Comments
 (0)