Skip to content

Commit

Permalink
Cherry-pick PR #35198 into release-3.7 (#35240)
Browse files Browse the repository at this point in the history
Component commits:
9c0fab9 Fix crash with Object.defineProperty for imported alias (--allowJs)
Fixes #35196
  • Loading branch information
typescript-bot authored and sandersn committed Nov 20, 2019
1 parent c18d72f commit b9d5231
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Expand Up @@ -2792,7 +2792,7 @@ namespace ts {

function bindObjectDefinePrototypeProperty(node: BindableObjectDefinePropertyCall) {
const namespaceSymbol = lookupSymbolForPropertyAccess((node.arguments[0] as PropertyAccessExpression).expression as EntityNameExpression);
if (namespaceSymbol) {
if (namespaceSymbol && namespaceSymbol.valueDeclaration) {
// Ensure the namespace symbol becomes class-like
addDeclarationToSymbol(namespaceSymbol, namespaceSymbol.valueDeclaration, SymbolFlags.Class);
}
Expand Down
11 changes: 8 additions & 3 deletions tests/baselines/reference/importAliasModuleExports.errors.txt
@@ -1,8 +1,9 @@
tests/cases/conformance/salsa/main.js(2,13): error TS2339: Property 'foo' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(3,13): error TS2339: Property 'func' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(3,38): error TS2339: Property '_func' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(5,9): error TS2339: Property 'foo' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'func' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'foo' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(7,9): error TS2339: Property 'func' does not exist on type 'Alias'.
tests/cases/conformance/salsa/main.js(8,9): error TS2339: Property 'def' does not exist on type 'Alias'.


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

==== tests/cases/conformance/salsa/main.js (5 errors) ====
==== tests/cases/conformance/salsa/main.js (6 errors) ====
import A from './mod1'
A.prototype.foo = 0
~~~
Expand All @@ -21,11 +22,15 @@ tests/cases/conformance/salsa/main.js(6,9): error TS2339: Property 'func' does n
!!! error TS2339: Property 'func' does not exist on type 'Alias'.
~~~~~
!!! error TS2339: Property '_func' does not exist on type 'Alias'.
Object.defineProperty(A.prototype, "def", { value: 0 });
new A().bar
new A().foo
~~~
!!! error TS2339: Property 'foo' does not exist on type 'Alias'.
new A().func()
~~~~
!!! error TS2339: Property 'func' does not exist on type 'Alias'.
new A().def
~~~
!!! error TS2339: Property 'def' does not exist on type 'Alias'.

12 changes: 12 additions & 0 deletions tests/baselines/reference/importAliasModuleExports.symbols
Expand Up @@ -26,6 +26,15 @@ A.prototype.func = function() { this._func = 0; }
>prototype : Symbol(A.prototype)
>this : Symbol(A, Decl(mod1.js, 0, 0))

Object.defineProperty(A.prototype, "def", { value: 0 });
>Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
>A.prototype : Symbol(A.prototype)
>A : Symbol(A, Decl(main.js, 0, 6))
>prototype : Symbol(A.prototype)
>value : Symbol(value, Decl(main.js, 3, 43))

new A().bar
>new A().bar : Symbol(A.bar, Decl(mod1.js, 0, 13))
>A : Symbol(A, Decl(main.js, 0, 6))
Expand All @@ -37,3 +46,6 @@ new A().foo
new A().func()
>A : Symbol(A, Decl(main.js, 0, 6))

new A().def
>A : Symbol(A, Decl(main.js, 0, 6))

19 changes: 19 additions & 0 deletions tests/baselines/reference/importAliasModuleExports.types
Expand Up @@ -40,6 +40,19 @@ A.prototype.func = function() { this._func = 0; }
>_func : any
>0 : 0

Object.defineProperty(A.prototype, "def", { value: 0 });
>Object.defineProperty(A.prototype, "def", { value: 0 }) : any
>Object.defineProperty : (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any
>Object : ObjectConstructor
>defineProperty : (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any
>A.prototype : A
>A : typeof A
>prototype : A
>"def" : "def"
>{ value: 0 } : { value: number; }
>value : number
>0 : 0

new A().bar
>new A().bar : () => number
>new A() : A
Expand All @@ -59,3 +72,9 @@ new A().func()
>A : typeof A
>func : any

new A().def
>new A().def : any
>new A() : A
>A : typeof A
>def : any

2 changes: 2 additions & 0 deletions tests/cases/conformance/salsa/importAliasModuleExports.ts
Expand Up @@ -12,6 +12,8 @@ module.exports = Alias;
import A from './mod1'
A.prototype.foo = 0
A.prototype.func = function() { this._func = 0; }
Object.defineProperty(A.prototype, "def", { value: 0 });
new A().bar
new A().foo
new A().func()
new A().def

0 comments on commit b9d5231

Please sign in to comment.