Skip to content

Commit fa1884e

Browse files
authored
Fix crash in expando assignment to alias (microsoft#34566)
* Fix crash in expando assignment to alias This PR disallows expando assignments Fixes microsoft#34493, but disallows the prototype assignment nonetheless. * Revert mistaken changes
1 parent 1d5add5 commit fa1884e

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2870,7 +2870,7 @@ namespace ts {
28702870
}
28712871
});
28722872
}
2873-
if (containerIsClass && namespaceSymbol) {
2873+
if (containerIsClass && namespaceSymbol && namespaceSymbol.valueDeclaration) {
28742874
addDeclarationToSymbol(namespaceSymbol, namespaceSymbol.valueDeclaration, SymbolFlags.Class);
28752875
}
28762876
return namespaceSymbol;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/conformance/salsa/main.js(2,13): error TS2339: Property 'foo' does not exist on type 'Alias'.
2+
tests/cases/conformance/salsa/main.js(4,9): error TS2339: Property 'foo' does not exist on type 'Alias'.
3+
4+
5+
==== tests/cases/conformance/salsa/mod1.js (0 errors) ====
6+
class Alias {
7+
bar() { return 1 }
8+
}
9+
module.exports = Alias;
10+
11+
==== tests/cases/conformance/salsa/main.js (2 errors) ====
12+
import A from './mod1'
13+
A.prototype.foo = 0
14+
~~~
15+
!!! error TS2339: Property 'foo' does not exist on type 'Alias'.
16+
new A().bar
17+
new A().foo
18+
~~~
19+
!!! error TS2339: Property 'foo' does not exist on type 'Alias'.
20+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/conformance/salsa/mod1.js ===
2+
class Alias {
3+
>Alias : Symbol(Alias, Decl(mod1.js, 0, 0))
4+
5+
bar() { return 1 }
6+
>bar : Symbol(Alias.bar, Decl(mod1.js, 0, 13))
7+
}
8+
module.exports = Alias;
9+
>module.exports : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0))
10+
>module : Symbol(export=, Decl(mod1.js, 2, 1))
11+
>exports : Symbol(export=, Decl(mod1.js, 2, 1))
12+
>Alias : Symbol(Alias, Decl(mod1.js, 0, 0))
13+
14+
=== tests/cases/conformance/salsa/main.js ===
15+
import A from './mod1'
16+
>A : Symbol(A, Decl(main.js, 0, 6))
17+
18+
A.prototype.foo = 0
19+
>A.prototype : Symbol(A.prototype)
20+
>A : Symbol(A, Decl(main.js, 0, 6))
21+
>prototype : Symbol(A.prototype)
22+
23+
new A().bar
24+
>new A().bar : Symbol(A.bar, Decl(mod1.js, 0, 13))
25+
>A : Symbol(A, Decl(main.js, 0, 6))
26+
>bar : Symbol(A.bar, Decl(mod1.js, 0, 13))
27+
28+
new A().foo
29+
>A : Symbol(A, Decl(main.js, 0, 6))
30+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/conformance/salsa/mod1.js ===
2+
class Alias {
3+
>Alias : Alias
4+
5+
bar() { return 1 }
6+
>bar : () => number
7+
>1 : 1
8+
}
9+
module.exports = Alias;
10+
>module.exports = Alias : typeof Alias
11+
>module.exports : typeof Alias
12+
>module : { "tests/cases/conformance/salsa/mod1": typeof Alias; }
13+
>exports : typeof Alias
14+
>Alias : typeof Alias
15+
16+
=== tests/cases/conformance/salsa/main.js ===
17+
import A from './mod1'
18+
>A : typeof A
19+
20+
A.prototype.foo = 0
21+
>A.prototype.foo = 0 : 0
22+
>A.prototype.foo : any
23+
>A.prototype : A
24+
>A : typeof A
25+
>prototype : A
26+
>foo : any
27+
>0 : 0
28+
29+
new A().bar
30+
>new A().bar : () => number
31+
>new A() : A
32+
>A : typeof A
33+
>bar : () => number
34+
35+
new A().foo
36+
>new A().foo : any
37+
>new A() : A
38+
>A : typeof A
39+
>foo : any
40+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
// @esModuleInterop: true
5+
// @filename: mod1.js
6+
class Alias {
7+
bar() { return 1 }
8+
}
9+
module.exports = Alias;
10+
11+
// @filename: main.js
12+
import A from './mod1'
13+
A.prototype.foo = 0
14+
new A().bar
15+
new A().foo

0 commit comments

Comments
 (0)