Skip to content

Commit 91196fc

Browse files
authored
Ensure functions that have prototype properties assigned by Object.defineProperty get marked as classes (microsoft#34577)
* Ensure functions that have prototype properties assigned by Object.defineProperty get marked as classes * Revert unneeded change
1 parent cdf1ab2 commit 91196fc

4 files changed

+67
-0
lines changed

src/compiler/binder.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,10 @@ namespace ts {
27882788

27892789
function bindObjectDefinePrototypeProperty(node: BindableObjectDefinePropertyCall) {
27902790
const namespaceSymbol = lookupSymbolForPropertyAccess((node.arguments[0] as PropertyAccessExpression).expression as EntityNameExpression);
2791+
if (namespaceSymbol) {
2792+
// Ensure the namespace symbol becomes class-like
2793+
addDeclarationToSymbol(namespaceSymbol, namespaceSymbol.valueDeclaration, SymbolFlags.Class);
2794+
}
27912795
bindPotentiallyNewExpandoMemberToNamespace(node, namespaceSymbol, /*isPrototypeProperty*/ true);
27922796
}
27932797

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== /a.js ===
2+
function Graphic() {
3+
>Graphic : Symbol(Graphic, Decl(a.js, 0, 0))
4+
}
5+
6+
Object.defineProperty(Graphic.prototype, "instance", {
7+
>Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
8+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
9+
>defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
10+
>Graphic.prototype : Symbol(Function.prototype, Decl(lib.es5.d.ts, --, --))
11+
>Graphic : Symbol(Graphic, Decl(a.js, 0, 0))
12+
>prototype : Symbol(Function.prototype, Decl(lib.es5.d.ts, --, --))
13+
>"instance" : Symbol(Graphic.instance, Decl(a.js, 1, 1))
14+
15+
get: function() {
16+
>get : Symbol(get, Decl(a.js, 3, 54))
17+
18+
return this;
19+
>this : Symbol(Graphic, Decl(a.js, 0, 0))
20+
}
21+
});
22+
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== /a.js ===
2+
function Graphic() {
3+
>Graphic : typeof Graphic
4+
}
5+
6+
Object.defineProperty(Graphic.prototype, "instance", {
7+
>Object.defineProperty(Graphic.prototype, "instance", { get: function() { return this; }}) : any
8+
>Object.defineProperty : (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any
9+
>Object : ObjectConstructor
10+
>defineProperty : (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any
11+
>Graphic.prototype : any
12+
>Graphic : typeof Graphic
13+
>prototype : any
14+
>"instance" : "instance"
15+
>{ get: function() { return this; }} : { get: () => this; }
16+
17+
get: function() {
18+
>get : () => this
19+
>function() { return this; } : () => this
20+
21+
return this;
22+
>this : this
23+
}
24+
});
25+
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @allowJs: true
2+
// @checkJs: false
3+
// @noEmit: true
4+
// @Filename: /a.js
5+
6+
function Graphic() {
7+
}
8+
9+
Object.defineProperty(Graphic.prototype, "instance", {
10+
get: function() {
11+
return this;
12+
}
13+
});
14+

0 commit comments

Comments
 (0)