diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 3c0f714ede0..00bf031c183 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -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); } diff --git a/tests/baselines/reference/moduleCrashInJSFile.errors.txt b/tests/baselines/reference/moduleCrashInJSFile.errors.txt new file mode 100644 index 00000000000..1dba2131d2f --- /dev/null +++ b/tests/baselines/reference/moduleCrashInJSFile.errors.txt @@ -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 = {})); \ No newline at end of file diff --git a/tests/baselines/reference/moduleCrashInJSFile.js b/tests/baselines/reference/moduleCrashInJSFile.js new file mode 100644 index 00000000000..fb87e04c8f0 --- /dev/null +++ b/tests/baselines/reference/moduleCrashInJSFile.js @@ -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 = {})); diff --git a/tests/baselines/reference/moduleCrashInJSFile.symbols b/tests/baselines/reference/moduleCrashInJSFile.symbols new file mode 100644 index 00000000000..0056d1cf887 --- /dev/null +++ b/tests/baselines/reference/moduleCrashInJSFile.symbols @@ -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)) + diff --git a/tests/baselines/reference/moduleCrashInJSFile.types b/tests/baselines/reference/moduleCrashInJSFile.types new file mode 100644 index 00000000000..a9d034ca3f4 --- /dev/null +++ b/tests/baselines/reference/moduleCrashInJSFile.types @@ -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 +>{} : {} + diff --git a/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.errors.txt b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.errors.txt new file mode 100644 index 00000000000..2706b6ef60b --- /dev/null +++ b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.errors.txt @@ -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. + } + \ No newline at end of file diff --git a/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.js b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.js new file mode 100644 index 00000000000..4735d38fdef --- /dev/null +++ b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.js @@ -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 = {})); diff --git a/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.symbols b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.symbols new file mode 100644 index 00000000000..5ada23b2237 --- /dev/null +++ b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.symbols @@ -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; +} + diff --git a/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.types b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.types new file mode 100644 index 00000000000..3ec8db55634 --- /dev/null +++ b/tests/baselines/reference/thisAssignmentInNamespaceDeclaration1.types @@ -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 +} + diff --git a/tests/cases/compiler/thisAssignmentInNamespaceDeclaration1.ts b/tests/cases/compiler/thisAssignmentInNamespaceDeclaration1.ts new file mode 100644 index 00000000000..4f17a3432d4 --- /dev/null +++ b/tests/cases/compiler/thisAssignmentInNamespaceDeclaration1.ts @@ -0,0 +1,12 @@ +// @checkJs: true +// @outDir: out/ + +// @filename: a.js +module foo { + this.bar = 4; +} + +// @filename: b.js +namespace blah { + this.prop = 42; +}