Skip to content

Commit 5c44241

Browse files
authored
Include arrow functions as javascript initializers (#23068)
This means that they are treated as valid js containers, methods, etc.
1 parent 6d9a825 commit 5c44241

6 files changed

+95
-43
lines changed

src/compiler/utilities.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,9 @@ namespace ts {
15421542
const e = skipParentheses(initializer.expression);
15431543
return e.kind === SyntaxKind.FunctionExpression || e.kind === SyntaxKind.ArrowFunction ? initializer : undefined;
15441544
}
1545-
if (initializer.kind === SyntaxKind.FunctionExpression || initializer.kind === SyntaxKind.ClassExpression) {
1545+
if (initializer.kind === SyntaxKind.FunctionExpression ||
1546+
initializer.kind === SyntaxKind.ClassExpression ||
1547+
initializer.kind === SyntaxKind.ArrowFunction) {
15461548
return initializer as Expression;
15471549
}
15481550
if (isObjectLiteralExpression(initializer) && (initializer.properties.length === 0 || isPrototypeAssignment)) {

tests/baselines/reference/typeFromPropertyAssignment19.symbols

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ exports = module.exports = C
2424
>exports : Symbol("tests/cases/conformance/salsa/semver", Decl(semver.js, 0, 0))
2525
>module : Symbol(export=, Decl(semver.js, 1, 9))
2626
>exports : Symbol(export=, Decl(semver.js, 1, 9))
27-
>C : Symbol(C, Decl(semver.js, 2, 16))
27+
>C : Symbol(C, Decl(semver.js, 2, 16), Decl(semver.js, 1, 28))
2828

2929
C.f = n => n + 1
3030
>C.f : Symbol(C.f, Decl(semver.js, 1, 28))
31-
>C : Symbol(C, Decl(semver.js, 2, 16))
31+
>C : Symbol(C, Decl(semver.js, 2, 16), Decl(semver.js, 1, 28))
3232
>f : Symbol(C.f, Decl(semver.js, 1, 28))
3333
>n : Symbol(n, Decl(semver.js, 2, 5))
3434
>n : Symbol(n, Decl(semver.js, 2, 5))
3535

3636
function C() {
37-
>C : Symbol(C, Decl(semver.js, 2, 16))
37+
>C : Symbol(C, Decl(semver.js, 2, 16), Decl(semver.js, 1, 28))
3838

3939
this.p = 1
4040
>p : Symbol(C.p, Decl(semver.js, 3, 14))

tests/baselines/reference/typeFromPropertyAssignment19.types

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
=== tests/cases/conformance/salsa/index.js ===
22
/// <reference path='./types.d.ts'/>
33
const C = require("./semver")
4-
>C : { (): void; f: (n: any) => any; }
5-
>require("./semver") : { (): void; f: (n: any) => any; }
4+
>C : typeof C
5+
>require("./semver") : typeof C
66
>require : any
77
>"./semver" : "./semver"
88

99
var two = C.f(1)
1010
>two : any
1111
>C.f(1) : any
1212
>C.f : (n: any) => any
13-
>C : { (): void; f: (n: any) => any; }
13+
>C : typeof C
1414
>f : (n: any) => any
1515
>1 : 1
1616

@@ -24,18 +24,18 @@ declare var module: any;
2424
=== tests/cases/conformance/salsa/semver.js ===
2525
/// <reference path='./types.d.ts'/>
2626
exports = module.exports = C
27-
>exports = module.exports = C : { (): void; f: (n: any) => any; }
27+
>exports = module.exports = C : typeof C
2828
>exports : any
29-
>module.exports = C : { (): void; f: (n: any) => any; }
29+
>module.exports = C : typeof C
3030
>module.exports : any
3131
>module : any
3232
>exports : any
33-
>C : { (): void; f: (n: any) => any; }
33+
>C : typeof C
3434

3535
C.f = n => n + 1
3636
>C.f = n => n + 1 : (n: any) => any
3737
>C.f : (n: any) => any
38-
>C : { (): void; f: (n: any) => any; }
38+
>C : typeof C
3939
>f : (n: any) => any
4040
>n => n + 1 : (n: any) => any
4141
>n : any
@@ -44,7 +44,7 @@ C.f = n => n + 1
4444
>1 : 1
4545

4646
function C() {
47-
>C : { (): void; f: (n: any) => any; }
47+
>C : typeof C
4848

4949
this.p = 1
5050
>this.p = 1 : 1
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
=== tests/cases/conformance/salsa/a.js ===
2-
class Ex {
3-
>Ex : Symbol(Ex, Decl(a.js, 0, 0))
2+
class B {
3+
>B : Symbol(B, Decl(a.js, 0, 0))
44

5+
constructor () {
6+
this.n = 1
7+
>this.n : Symbol(B.n, Decl(a.js, 1, 20))
8+
>this : Symbol(B, Decl(a.js, 0, 0))
9+
>n : Symbol(B.n, Decl(a.js, 1, 20))
10+
}
511
foo() {
6-
>foo : Symbol(Ex.foo, Decl(a.js, 0, 10))
12+
>foo : Symbol(B.foo, Decl(a.js, 3, 5))
713
}
814
}
915

10-
class MyClass extends Ex {
11-
>MyClass : Symbol(MyClass, Decl(a.js, 3, 1))
12-
>Ex : Symbol(Ex, Decl(a.js, 0, 0))
16+
class C extends B { }
17+
>C : Symbol(C, Decl(a.js, 6, 1))
18+
>B : Symbol(B, Decl(a.js, 0, 0))
1319

20+
// this override should be fine (even if it's a little odd)
21+
C.prototype.foo = function() {
22+
>C.prototype.foo : Symbol(C.foo, Decl(a.js, 8, 21))
23+
>C.prototype : Symbol(C.foo, Decl(a.js, 8, 21))
24+
>C : Symbol(C, Decl(a.js, 6, 1))
25+
>prototype : Symbol(C.prototype)
26+
>foo : Symbol(C.foo, Decl(a.js, 8, 21))
1427
}
1528

16-
// this override should be fine (even if it's a little odd)
17-
MyClass.prototype.foo = function() {
18-
>MyClass.prototype.foo : Symbol(MyClass.foo, Decl(a.js, 7, 1))
19-
>MyClass.prototype : Symbol(MyClass.foo, Decl(a.js, 7, 1))
20-
>MyClass : Symbol(MyClass, Decl(a.js, 3, 1))
21-
>prototype : Symbol(MyClass.prototype)
22-
>foo : Symbol(MyClass.foo, Decl(a.js, 7, 1))
29+
class D extends B { }
30+
>D : Symbol(D, Decl(a.js, 12, 1))
31+
>B : Symbol(B, Decl(a.js, 0, 0))
32+
33+
D.prototype.foo = () => {
34+
>D.prototype.foo : Symbol(D.foo, Decl(a.js, 14, 21))
35+
>D.prototype : Symbol(D.foo, Decl(a.js, 14, 21))
36+
>D : Symbol(D, Decl(a.js, 12, 1))
37+
>prototype : Symbol(D.prototype)
38+
>foo : Symbol(D.foo, Decl(a.js, 14, 21))
39+
40+
this.n = 'not checked, so no error'
2341
}
2442

Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
=== tests/cases/conformance/salsa/a.js ===
2-
class Ex {
3-
>Ex : Ex
2+
class B {
3+
>B : B
44

5+
constructor () {
6+
this.n = 1
7+
>this.n = 1 : 1
8+
>this.n : number
9+
>this : this
10+
>n : number
11+
>1 : 1
12+
}
513
foo() {
614
>foo : () => void
715
}
816
}
917

10-
class MyClass extends Ex {
11-
>MyClass : MyClass
12-
>Ex : Ex
13-
14-
}
18+
class C extends B { }
19+
>C : C
20+
>B : B
1521

1622
// this override should be fine (even if it's a little odd)
17-
MyClass.prototype.foo = function() {
18-
>MyClass.prototype.foo = function() {} : () => void
19-
>MyClass.prototype.foo : () => void
20-
>MyClass.prototype : MyClass
21-
>MyClass : typeof MyClass
22-
>prototype : MyClass
23+
C.prototype.foo = function() {
24+
>C.prototype.foo = function() {} : () => void
25+
>C.prototype.foo : () => void
26+
>C.prototype : C
27+
>C : typeof C
28+
>prototype : C
2329
>foo : () => void
2430
>function() {} : () => void
2531
}
2632

33+
class D extends B { }
34+
>D : D
35+
>B : B
36+
37+
D.prototype.foo = () => {
38+
>D.prototype.foo = () => { this.n = 'not checked, so no error'} : () => void
39+
>D.prototype.foo : () => void
40+
>D.prototype : D
41+
>D : typeof D
42+
>prototype : D
43+
>foo : () => void
44+
>() => { this.n = 'not checked, so no error'} : () => void
45+
46+
this.n = 'not checked, so no error'
47+
>this.n = 'not checked, so no error' : "not checked, so no error"
48+
>this.n : any
49+
>this : any
50+
>n : any
51+
>'not checked, so no error' : "not checked, so no error"
52+
}
53+
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
// @noEmit: true
2-
// @strict: true
32
// @checkJs: true
43
// @allowJs: true
54
// @Filename: a.js
6-
class Ex {
5+
class B {
6+
constructor () {
7+
this.n = 1
8+
}
79
foo() {
810
}
911
}
1012

11-
class MyClass extends Ex {
13+
class C extends B { }
1214

15+
// this override should be fine (even if it's a little odd)
16+
C.prototype.foo = function() {
1317
}
1418

15-
// this override should be fine (even if it's a little odd)
16-
MyClass.prototype.foo = function() {
19+
class D extends B { }
20+
D.prototype.foo = () => {
21+
this.n = 'not checked, so no error'
1722
}

0 commit comments

Comments
 (0)