Skip to content

Commit

Permalink
Fix(49702): LS crash in binder for module in a JS file (#52537)
Browse files Browse the repository at this point in the history
  • Loading branch information
navya9singh committed Mar 8, 2023
1 parent 0ce5517 commit 74a37f8
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,9 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes);
}
break;

// Namespaces are not allowed in javascript files, so do nothing here
case SyntaxKind.ModuleDeclaration:
break;
default:
Debug.failBadSyntaxKind(thisContainer);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/moduleCrashInJSFile.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/compiler/a.js(2,8): error TS2300: Duplicate identifier 'foo'.
tests/cases/compiler/a.js(2,8): error TS8006: 'module' declarations can only be used in TypeScript files.
tests/cases/compiler/a.js(3,5): error TS2331: 'this' cannot be referenced in a module or namespace body.
tests/cases/compiler/a.js(7,5): error TS2300: Duplicate identifier 'foo'.


==== tests/cases/compiler/a.js (4 errors) ====
//// [thisKeyword.ts]
module foo {
~~~
!!! error TS2300: Duplicate identifier 'foo'.
~~~
!!! error TS8006: 'module' declarations can only be used in TypeScript files.
this.bar = 4;
~~~~
!!! error TS2331: 'this' cannot be referenced in a module or namespace body.
}

//// [thisKeyword.js]
var foo;
~~~
!!! error TS2300: Duplicate identifier 'foo'.
(function (foo) {
this.bar = 4;
})(foo || (foo = {}));
23 changes: 23 additions & 0 deletions tests/baselines/reference/moduleCrashInJSFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [a.js]
//// [thisKeyword.ts]
module foo {
this.bar = 4;
}

//// [thisKeyword.js]
var foo;
(function (foo) {
this.bar = 4;
})(foo || (foo = {}));

//// [a.js]
//// [thisKeyword.ts]
var foo;
(function (foo) {
this.bar = 4;
})(foo || (foo = {}));
//// [thisKeyword.js]
var foo;
(function (foo) {
this.bar = 4;
})(foo || (foo = {}));
24 changes: 24 additions & 0 deletions tests/baselines/reference/moduleCrashInJSFile.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/a.js ===
//// [thisKeyword.ts]
module foo {
>foo : Symbol(foo, Decl(a.js, 0, 0))

this.bar = 4;
}

//// [thisKeyword.js]
var foo;
>foo : Symbol(foo, Decl(a.js, 6, 3))

(function (foo) {
>foo : Symbol(foo, Decl(a.js, 7, 11))

this.bar = 4;
>this.bar : Symbol((Anonymous function).bar, Decl(a.js, 7, 17))
>this : Symbol((Anonymous function), Decl(a.js, 7, 1))
>bar : Symbol((Anonymous function).bar, Decl(a.js, 7, 17))

})(foo || (foo = {}));
>foo : Symbol(foo, Decl(a.js, 0, 0))
>foo : Symbol(foo, Decl(a.js, 0, 0))

38 changes: 38 additions & 0 deletions tests/baselines/reference/moduleCrashInJSFile.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== tests/cases/compiler/a.js ===
//// [thisKeyword.ts]
module foo {
>foo : typeof foo

this.bar = 4;
>this.bar = 4 : 4
>this.bar : any
>this : any
>bar : any
>4 : 4
}

//// [thisKeyword.js]
var foo;
>foo : any

(function (foo) {
>(function (foo) { this.bar = 4;})(foo || (foo = {})) : void
>(function (foo) { this.bar = 4;}) : typeof (Anonymous function)
>function (foo) { this.bar = 4;} : typeof (Anonymous function)
>foo : typeof globalThis.foo

this.bar = 4;
>this.bar = 4 : 4
>this.bar : any
>this : this
>bar : any
>4 : 4

})(foo || (foo = {}));
>foo || (foo = {}) : typeof foo
>foo : typeof foo
>(foo = {}) : {}
>foo = {} : {}
>foo : typeof foo
>{} : {}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
tests/cases/compiler/a.js(1,8): error TS8006: 'module' declarations can only be used in TypeScript files.
tests/cases/compiler/a.js(2,5): error TS2331: 'this' cannot be referenced in a module or namespace body.
tests/cases/compiler/b.js(1,11): error TS8006: 'namespace' declarations can only be used in TypeScript files.
tests/cases/compiler/b.js(2,5): error TS2331: 'this' cannot be referenced in a module or namespace body.


==== tests/cases/compiler/a.js (2 errors) ====
module foo {
~~~
!!! error TS8006: 'module' declarations can only be used in TypeScript files.
this.bar = 4;
~~~~
!!! error TS2331: 'this' cannot be referenced in a module or namespace body.
}

==== tests/cases/compiler/b.js (2 errors) ====
namespace blah {
~~~~
!!! error TS8006: 'namespace' declarations can only be used in TypeScript files.
this.prop = 42;
~~~~
!!! error TS2331: 'this' cannot be referenced in a module or namespace body.
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/thisAssignmentInNamespaceDeclaration1.ts] ////

//// [a.js]
module foo {
this.bar = 4;
}

//// [b.js]
namespace blah {
this.prop = 42;
}


//// [a.js]
var foo;
(function (foo) {
this.bar = 4;
})(foo || (foo = {}));
//// [b.js]
var blah;
(function (blah) {
this.prop = 42;
})(blah || (blah = {}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/a.js ===
module foo {
>foo : Symbol(foo, Decl(a.js, 0, 0))

this.bar = 4;
}

=== tests/cases/compiler/b.js ===
namespace blah {
>blah : Symbol(blah, Decl(b.js, 0, 0))

this.prop = 42;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/a.js ===
module foo {
>foo : typeof foo

this.bar = 4;
>this.bar = 4 : 4
>this.bar : any
>this : any
>bar : any
>4 : 4
}

=== tests/cases/compiler/b.js ===
namespace blah {
>blah : typeof blah

this.prop = 42;
>this.prop = 42 : 42
>this.prop : any
>this : any
>prop : any
>42 : 42
}

12 changes: 12 additions & 0 deletions tests/cases/compiler/thisAssignmentInNamespaceDeclaration1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @checkJs: true
// @outDir: out/

// @filename: a.js
module foo {
this.bar = 4;
}

// @filename: b.js
namespace blah {
this.prop = 42;
}

0 comments on commit 74a37f8

Please sign in to comment.