Skip to content

Commit b9d5231

Browse files
TypeScript Botsandersn
authored andcommitted
Cherry-pick PR #35198 into release-3.7 (#35240)
Component commits: 9c0fab9 Fix crash with Object.defineProperty for imported alias (--allowJs) Fixes #35196
1 parent c18d72f commit b9d5231

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,7 @@ namespace ts {
27922792

27932793
function bindObjectDefinePrototypeProperty(node: BindableObjectDefinePropertyCall) {
27942794
const namespaceSymbol = lookupSymbolForPropertyAccess((node.arguments[0] as PropertyAccessExpression).expression as EntityNameExpression);
2795-
if (namespaceSymbol) {
2795+
if (namespaceSymbol && namespaceSymbol.valueDeclaration) {
27962796
// Ensure the namespace symbol becomes class-like
27972797
addDeclarationToSymbol(namespaceSymbol, namespaceSymbol.valueDeclaration, SymbolFlags.Class);
27982798
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
tests/cases/conformance/salsa/main.js(2,13): error TS2339: Property 'foo' does not exist on type 'Alias'.
22
tests/cases/conformance/salsa/main.js(3,13): error TS2339: Property 'func' does not exist on type 'Alias'.
33
tests/cases/conformance/salsa/main.js(3,38): error TS2339: Property '_func' does not exist on type 'Alias'.
4-
tests/cases/conformance/salsa/main.js(5,9): error TS2339: Property 'foo' does not exist on type 'Alias'.
5-
tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'func' does not exist on type 'Alias'.
4+
tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'foo' does not exist on type 'Alias'.
5+
tests/cases/conformance/salsa/main.js(7,9): error TS2339: Property 'func' does not exist on type 'Alias'.
6+
tests/cases/conformance/salsa/main.js(8,9): error TS2339: Property 'def' does not exist on type 'Alias'.
67

78

89
==== tests/cases/conformance/salsa/mod1.js (0 errors) ====
@@ -11,7 +12,7 @@ tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'func' does n
1112
}
1213
module.exports = Alias;
1314

14-
==== tests/cases/conformance/salsa/main.js (5 errors) ====
15+
==== tests/cases/conformance/salsa/main.js (6 errors) ====
1516
import A from './mod1'
1617
A.prototype.foo = 0
1718
~~~
@@ -21,11 +22,15 @@ tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'func' does n
2122
!!! error TS2339: Property 'func' does not exist on type 'Alias'.
2223
~~~~~
2324
!!! error TS2339: Property '_func' does not exist on type 'Alias'.
25+
Object.defineProperty(A.prototype, "def", { value: 0 });
2426
new A().bar
2527
new A().foo
2628
~~~
2729
!!! error TS2339: Property 'foo' does not exist on type 'Alias'.
2830
new A().func()
2931
~~~~
3032
!!! error TS2339: Property 'func' does not exist on type 'Alias'.
33+
new A().def
34+
~~~
35+
!!! error TS2339: Property 'def' does not exist on type 'Alias'.
3136

tests/baselines/reference/importAliasModuleExports.symbols

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ A.prototype.func = function() { this._func = 0; }
2626
>prototype : Symbol(A.prototype)
2727
>this : Symbol(A, Decl(mod1.js, 0, 0))
2828

29+
Object.defineProperty(A.prototype, "def", { value: 0 });
30+
>Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
31+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
32+
>defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
33+
>A.prototype : Symbol(A.prototype)
34+
>A : Symbol(A, Decl(main.js, 0, 6))
35+
>prototype : Symbol(A.prototype)
36+
>value : Symbol(value, Decl(main.js, 3, 43))
37+
2938
new A().bar
3039
>new A().bar : Symbol(A.bar, Decl(mod1.js, 0, 13))
3140
>A : Symbol(A, Decl(main.js, 0, 6))
@@ -37,3 +46,6 @@ new A().foo
3746
new A().func()
3847
>A : Symbol(A, Decl(main.js, 0, 6))
3948

49+
new A().def
50+
>A : Symbol(A, Decl(main.js, 0, 6))
51+

tests/baselines/reference/importAliasModuleExports.types

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ A.prototype.func = function() { this._func = 0; }
4040
>_func : any
4141
>0 : 0
4242

43+
Object.defineProperty(A.prototype, "def", { value: 0 });
44+
>Object.defineProperty(A.prototype, "def", { value: 0 }) : any
45+
>Object.defineProperty : (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any
46+
>Object : ObjectConstructor
47+
>defineProperty : (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any
48+
>A.prototype : A
49+
>A : typeof A
50+
>prototype : A
51+
>"def" : "def"
52+
>{ value: 0 } : { value: number; }
53+
>value : number
54+
>0 : 0
55+
4356
new A().bar
4457
>new A().bar : () => number
4558
>new A() : A
@@ -59,3 +72,9 @@ new A().func()
5972
>A : typeof A
6073
>func : any
6174

75+
new A().def
76+
>new A().def : any
77+
>new A() : A
78+
>A : typeof A
79+
>def : any
80+

tests/cases/conformance/salsa/importAliasModuleExports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = Alias;
1212
import A from './mod1'
1313
A.prototype.foo = 0
1414
A.prototype.func = function() { this._func = 0; }
15+
Object.defineProperty(A.prototype, "def", { value: 0 });
1516
new A().bar
1617
new A().foo
1718
new A().func()
19+
new A().def

0 commit comments

Comments
 (0)