Skip to content

Commit 74a37f8

Browse files
authored
Fix(49702): LS crash in binder for module in a JS file (microsoft#52537)
1 parent 0ce5517 commit 74a37f8

10 files changed

+210
-1
lines changed

src/compiler/binder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3213,7 +3213,9 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
32133213
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes);
32143214
}
32153215
break;
3216-
3216+
// Namespaces are not allowed in javascript files, so do nothing here
3217+
case SyntaxKind.ModuleDeclaration:
3218+
break;
32173219
default:
32183220
Debug.failBadSyntaxKind(thisContainer);
32193221
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests/cases/compiler/a.js(2,8): error TS2300: Duplicate identifier 'foo'.
2+
tests/cases/compiler/a.js(2,8): error TS8006: 'module' declarations can only be used in TypeScript files.
3+
tests/cases/compiler/a.js(3,5): error TS2331: 'this' cannot be referenced in a module or namespace body.
4+
tests/cases/compiler/a.js(7,5): error TS2300: Duplicate identifier 'foo'.
5+
6+
7+
==== tests/cases/compiler/a.js (4 errors) ====
8+
//// [thisKeyword.ts]
9+
module foo {
10+
~~~
11+
!!! error TS2300: Duplicate identifier 'foo'.
12+
~~~
13+
!!! error TS8006: 'module' declarations can only be used in TypeScript files.
14+
this.bar = 4;
15+
~~~~
16+
!!! error TS2331: 'this' cannot be referenced in a module or namespace body.
17+
}
18+
19+
//// [thisKeyword.js]
20+
var foo;
21+
~~~
22+
!!! error TS2300: Duplicate identifier 'foo'.
23+
(function (foo) {
24+
this.bar = 4;
25+
})(foo || (foo = {}));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [a.js]
2+
//// [thisKeyword.ts]
3+
module foo {
4+
this.bar = 4;
5+
}
6+
7+
//// [thisKeyword.js]
8+
var foo;
9+
(function (foo) {
10+
this.bar = 4;
11+
})(foo || (foo = {}));
12+
13+
//// [a.js]
14+
//// [thisKeyword.ts]
15+
var foo;
16+
(function (foo) {
17+
this.bar = 4;
18+
})(foo || (foo = {}));
19+
//// [thisKeyword.js]
20+
var foo;
21+
(function (foo) {
22+
this.bar = 4;
23+
})(foo || (foo = {}));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/a.js ===
2+
//// [thisKeyword.ts]
3+
module foo {
4+
>foo : Symbol(foo, Decl(a.js, 0, 0))
5+
6+
this.bar = 4;
7+
}
8+
9+
//// [thisKeyword.js]
10+
var foo;
11+
>foo : Symbol(foo, Decl(a.js, 6, 3))
12+
13+
(function (foo) {
14+
>foo : Symbol(foo, Decl(a.js, 7, 11))
15+
16+
this.bar = 4;
17+
>this.bar : Symbol((Anonymous function).bar, Decl(a.js, 7, 17))
18+
>this : Symbol((Anonymous function), Decl(a.js, 7, 1))
19+
>bar : Symbol((Anonymous function).bar, Decl(a.js, 7, 17))
20+
21+
})(foo || (foo = {}));
22+
>foo : Symbol(foo, Decl(a.js, 0, 0))
23+
>foo : Symbol(foo, Decl(a.js, 0, 0))
24+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/a.js ===
2+
//// [thisKeyword.ts]
3+
module foo {
4+
>foo : typeof foo
5+
6+
this.bar = 4;
7+
>this.bar = 4 : 4
8+
>this.bar : any
9+
>this : any
10+
>bar : any
11+
>4 : 4
12+
}
13+
14+
//// [thisKeyword.js]
15+
var foo;
16+
>foo : any
17+
18+
(function (foo) {
19+
>(function (foo) { this.bar = 4;})(foo || (foo = {})) : void
20+
>(function (foo) { this.bar = 4;}) : typeof (Anonymous function)
21+
>function (foo) { this.bar = 4;} : typeof (Anonymous function)
22+
>foo : typeof globalThis.foo
23+
24+
this.bar = 4;
25+
>this.bar = 4 : 4
26+
>this.bar : any
27+
>this : this
28+
>bar : any
29+
>4 : 4
30+
31+
})(foo || (foo = {}));
32+
>foo || (foo = {}) : typeof foo
33+
>foo : typeof foo
34+
>(foo = {}) : {}
35+
>foo = {} : {}
36+
>foo : typeof foo
37+
>{} : {}
38+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
tests/cases/compiler/a.js(1,8): error TS8006: 'module' declarations can only be used in TypeScript files.
2+
tests/cases/compiler/a.js(2,5): error TS2331: 'this' cannot be referenced in a module or namespace body.
3+
tests/cases/compiler/b.js(1,11): error TS8006: 'namespace' declarations can only be used in TypeScript files.
4+
tests/cases/compiler/b.js(2,5): error TS2331: 'this' cannot be referenced in a module or namespace body.
5+
6+
7+
==== tests/cases/compiler/a.js (2 errors) ====
8+
module foo {
9+
~~~
10+
!!! error TS8006: 'module' declarations can only be used in TypeScript files.
11+
this.bar = 4;
12+
~~~~
13+
!!! error TS2331: 'this' cannot be referenced in a module or namespace body.
14+
}
15+
16+
==== tests/cases/compiler/b.js (2 errors) ====
17+
namespace blah {
18+
~~~~
19+
!!! error TS8006: 'namespace' declarations can only be used in TypeScript files.
20+
this.prop = 42;
21+
~~~~
22+
!!! error TS2331: 'this' cannot be referenced in a module or namespace body.
23+
}
24+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/thisAssignmentInNamespaceDeclaration1.ts] ////
2+
3+
//// [a.js]
4+
module foo {
5+
this.bar = 4;
6+
}
7+
8+
//// [b.js]
9+
namespace blah {
10+
this.prop = 42;
11+
}
12+
13+
14+
//// [a.js]
15+
var foo;
16+
(function (foo) {
17+
this.bar = 4;
18+
})(foo || (foo = {}));
19+
//// [b.js]
20+
var blah;
21+
(function (blah) {
22+
this.prop = 42;
23+
})(blah || (blah = {}));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/a.js ===
2+
module foo {
3+
>foo : Symbol(foo, Decl(a.js, 0, 0))
4+
5+
this.bar = 4;
6+
}
7+
8+
=== tests/cases/compiler/b.js ===
9+
namespace blah {
10+
>blah : Symbol(blah, Decl(b.js, 0, 0))
11+
12+
this.prop = 42;
13+
}
14+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/a.js ===
2+
module foo {
3+
>foo : typeof foo
4+
5+
this.bar = 4;
6+
>this.bar = 4 : 4
7+
>this.bar : any
8+
>this : any
9+
>bar : any
10+
>4 : 4
11+
}
12+
13+
=== tests/cases/compiler/b.js ===
14+
namespace blah {
15+
>blah : typeof blah
16+
17+
this.prop = 42;
18+
>this.prop = 42 : 42
19+
>this.prop : any
20+
>this : any
21+
>prop : any
22+
>42 : 42
23+
}
24+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @checkJs: true
2+
// @outDir: out/
3+
4+
// @filename: a.js
5+
module foo {
6+
this.bar = 4;
7+
}
8+
9+
// @filename: b.js
10+
namespace blah {
11+
this.prop = 42;
12+
}

0 commit comments

Comments
 (0)