Skip to content

Commit d6c05a1

Browse files
committed
Fix getEffectiveTypeAnnotationNode
Prevent it from using the (return) type of a function. Could also check the condition before calling the function, but there are two places that need the check, and OTOH, all calls check the result so returning `undefined` should work. (This problem was introduced in PR#32553.) Fixes microsoft#33741.
1 parent 5cc58de commit d6c05a1

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

src/compiler/utilities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3865,9 +3865,11 @@ namespace ts {
38653865

38663866
/**
38673867
* Gets the effective type annotation of a variable, parameter, or property. If the node was
3868-
* parsed in a JavaScript file, gets the type annotation from JSDoc.
3868+
* parsed in a JavaScript file, gets the type annotation from JSDoc. Also gets the type of
3869+
* functions only the JSDoc case.
38693870
*/
38703871
export function getEffectiveTypeAnnotationNode(node: Node): TypeNode | undefined {
3872+
if (!isInJSFile(node) && isFunctionDeclaration(node)) return undefined;
38713873
const type = (node as HasType).type;
38723874
if (type || !isInJSFile(node)) return type;
38733875
return isJSDocPropertyLikeTag(node) ? node.typeExpression && node.typeExpression.type : getJSDocType(node);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [propertyAssignmentUseParentType3.ts]
2+
// don't use the parent type if it's a function declaration (#33741)
3+
4+
function foo1(): number {
5+
return 123;
6+
}
7+
foo1.toFixed = "";
8+
9+
function foo2(): any[] {
10+
return [];
11+
}
12+
foo2.join = "";
13+
14+
function foo3(): string {
15+
return "";
16+
}
17+
foo3.trim = "";
18+
19+
function foo4(): ({x: number}) {
20+
return {x: 123};
21+
}
22+
foo4.x = "456";
23+
24+
25+
//// [propertyAssignmentUseParentType3.js]
26+
// don't use the parent type if it's a function declaration (#33741)
27+
function foo1() {
28+
return 123;
29+
}
30+
foo1.toFixed = "";
31+
function foo2() {
32+
return [];
33+
}
34+
foo2.join = "";
35+
function foo3() {
36+
return "";
37+
}
38+
foo3.trim = "";
39+
function foo4() {
40+
return { x: 123 };
41+
}
42+
foo4.x = "456";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/conformance/salsa/propertyAssignmentUseParentType3.ts ===
2+
// don't use the parent type if it's a function declaration (#33741)
3+
4+
function foo1(): number {
5+
>foo1 : Symbol(foo1, Decl(propertyAssignmentUseParentType3.ts, 0, 0), Decl(propertyAssignmentUseParentType3.ts, 4, 1))
6+
7+
return 123;
8+
}
9+
foo1.toFixed = "";
10+
>foo1.toFixed : Symbol(foo1.toFixed, Decl(propertyAssignmentUseParentType3.ts, 4, 1))
11+
>foo1 : Symbol(foo1, Decl(propertyAssignmentUseParentType3.ts, 0, 0), Decl(propertyAssignmentUseParentType3.ts, 4, 1))
12+
>toFixed : Symbol(foo1.toFixed, Decl(propertyAssignmentUseParentType3.ts, 4, 1))
13+
14+
function foo2(): any[] {
15+
>foo2 : Symbol(foo2, Decl(propertyAssignmentUseParentType3.ts, 5, 18), Decl(propertyAssignmentUseParentType3.ts, 9, 1))
16+
17+
return [];
18+
}
19+
foo2.join = "";
20+
>foo2.join : Symbol(foo2.join, Decl(propertyAssignmentUseParentType3.ts, 9, 1))
21+
>foo2 : Symbol(foo2, Decl(propertyAssignmentUseParentType3.ts, 5, 18), Decl(propertyAssignmentUseParentType3.ts, 9, 1))
22+
>join : Symbol(foo2.join, Decl(propertyAssignmentUseParentType3.ts, 9, 1))
23+
24+
function foo3(): string {
25+
>foo3 : Symbol(foo3, Decl(propertyAssignmentUseParentType3.ts, 10, 15), Decl(propertyAssignmentUseParentType3.ts, 14, 1))
26+
27+
return "";
28+
}
29+
foo3.trim = "";
30+
>foo3.trim : Symbol(foo3.trim, Decl(propertyAssignmentUseParentType3.ts, 14, 1))
31+
>foo3 : Symbol(foo3, Decl(propertyAssignmentUseParentType3.ts, 10, 15), Decl(propertyAssignmentUseParentType3.ts, 14, 1))
32+
>trim : Symbol(foo3.trim, Decl(propertyAssignmentUseParentType3.ts, 14, 1))
33+
34+
function foo4(): ({x: number}) {
35+
>foo4 : Symbol(foo4, Decl(propertyAssignmentUseParentType3.ts, 15, 15), Decl(propertyAssignmentUseParentType3.ts, 19, 1))
36+
>x : Symbol(x, Decl(propertyAssignmentUseParentType3.ts, 17, 19))
37+
38+
return {x: 123};
39+
>x : Symbol(x, Decl(propertyAssignmentUseParentType3.ts, 18, 12))
40+
}
41+
foo4.x = "456";
42+
>foo4.x : Symbol(foo4.x, Decl(propertyAssignmentUseParentType3.ts, 19, 1))
43+
>foo4 : Symbol(foo4, Decl(propertyAssignmentUseParentType3.ts, 15, 15), Decl(propertyAssignmentUseParentType3.ts, 19, 1))
44+
>x : Symbol(foo4.x, Decl(propertyAssignmentUseParentType3.ts, 19, 1))
45+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== tests/cases/conformance/salsa/propertyAssignmentUseParentType3.ts ===
2+
// don't use the parent type if it's a function declaration (#33741)
3+
4+
function foo1(): number {
5+
>foo1 : typeof foo1
6+
7+
return 123;
8+
>123 : 123
9+
}
10+
foo1.toFixed = "";
11+
>foo1.toFixed = "" : ""
12+
>foo1.toFixed : string
13+
>foo1 : typeof foo1
14+
>toFixed : string
15+
>"" : ""
16+
17+
function foo2(): any[] {
18+
>foo2 : typeof foo2
19+
20+
return [];
21+
>[] : undefined[]
22+
}
23+
foo2.join = "";
24+
>foo2.join = "" : ""
25+
>foo2.join : string
26+
>foo2 : typeof foo2
27+
>join : string
28+
>"" : ""
29+
30+
function foo3(): string {
31+
>foo3 : typeof foo3
32+
33+
return "";
34+
>"" : ""
35+
}
36+
foo3.trim = "";
37+
>foo3.trim = "" : ""
38+
>foo3.trim : string
39+
>foo3 : typeof foo3
40+
>trim : string
41+
>"" : ""
42+
43+
function foo4(): ({x: number}) {
44+
>foo4 : typeof foo4
45+
>x : number
46+
47+
return {x: 123};
48+
>{x: 123} : { x: number; }
49+
>x : number
50+
>123 : 123
51+
}
52+
foo4.x = "456";
53+
>foo4.x = "456" : "456"
54+
>foo4.x : string
55+
>foo4 : typeof foo4
56+
>x : string
57+
>"456" : "456"
58+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// don't use the parent type if it's a function declaration (#33741)
2+
3+
function foo1(): number {
4+
return 123;
5+
}
6+
foo1.toFixed = "";
7+
8+
function foo2(): any[] {
9+
return [];
10+
}
11+
foo2.join = "";
12+
13+
function foo3(): string {
14+
return "";
15+
}
16+
foo3.trim = "";
17+
18+
function foo4(): ({x: number}) {
19+
return {x: 123};
20+
}
21+
foo4.x = "456";

0 commit comments

Comments
 (0)