Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ban non-ambient namespaces written with module keyword #61450

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 4 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47309,6 +47309,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
error(node.name, Diagnostics.Augmentations_for_the_global_scope_should_have_declare_modifier_unless_they_appear_in_already_ambient_context);
}

if ((node.flags & NodeFlags.Namespace) === NodeFlags.None && node.name.kind !== SyntaxKind.StringLiteral && !isGlobalScopeAugmentation(node)) {
error(node.name, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
}

const isAmbientExternalModule: boolean = isAmbientModule(node);
const contextErrorMessage = isAmbientExternalModule
? Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file
Expand Down Expand Up @@ -47345,9 +47349,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
&& !inAmbientContext
&& isInstantiatedModule(node, shouldPreserveConstEnums(compilerOptions))
) {
if (compilerOptions.erasableSyntaxOnly) {
error(node.name, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
}

if (getIsolatedModules(compilerOptions) && !getSourceFileOfNode(node).externalModuleIndicator) {
// This could be loosened a little if needed. The only problem we are trying to avoid is unqualified
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.d.ts(1,16): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== module.d.ts (1 errors) ====
declare module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var Origin: { x: number; y: number; }
}

==== function.d.ts (0 errors) ====
declare function Point(): { x: number; y: number; }

==== test.ts (0 errors) ====
var cl: { x: number; y: number; }
var cl = Point();
var cl = Point.Origin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class.d.ts(1,16): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
module.d.ts(1,16): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
module.d.ts(2,19): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== module.d.ts (2 errors) ====
declare module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var Origin: {
x: number;
y: number;
}
}
}

==== class.d.ts (1 errors) ====
declare module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export class Point {
constructor(x: number, y: number);
x: number;
y: number;
}
}

==== test.ts (0 errors) ====
var p: { x: number; y: number; }
var p = A.Point.Origin;
var p = new A.Point(0, 0); // unexpected error here, bug 840000

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
classPoint.ts(1,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
module.d.ts(1,16): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
module.d.ts(2,19): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== module.d.ts (2 errors) ====
declare module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var Origin: {
x: number;
y: number;
}
}
}

==== classPoint.ts (1 errors) ====
module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export class Point {
constructor(public x: number, public y: number) { }
}
}

==== test.ts (0 errors) ====
var p: { x: number; y: number; }
var p = A.Point.Origin;
var p = new A.Point(0, 0); // unexpected error here, bug 840000
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.d.ts(1,16): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== module.d.ts (1 errors) ====
declare module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var Origin: { x: number; y: number; }
}

==== function.ts (0 errors) ====
function Point() {
return { x: 0, y: 0 };
}

==== test.ts (0 errors) ====
var cl: { x: number; y: number; }
var cl = Point();
var cl = Point.Origin;
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(9,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(10,19): error TS2304: Cannot find name 'T'.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(19,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(20,12): error TS2304: Cannot find name 'T'.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(22,23): error TS2304: Cannot find name 'T'.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(34,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(35,26): error TS2304: Cannot find name 'T'.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(44,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(46,15): error TS2304: Cannot find name 'T'.


==== ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts (5 errors) ====
==== ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts (9 errors) ====
// all expected to be errors

class clodule1<T>{
Expand All @@ -15,6 +19,8 @@ ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(46,15): err
}

module clodule1 {
~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
function f(x: T) { }
~
!!! error TS2304: Cannot find name 'T'.
Expand All @@ -27,6 +33,8 @@ ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(46,15): err
}

module clodule2 {
~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
var x: T;
~
!!! error TS2304: Cannot find name 'T'.
Expand All @@ -46,6 +54,8 @@ ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(46,15): err
}

module clodule3 {
~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var y = { id: T };
~
!!! error TS2304: Cannot find name 'T'.
Expand All @@ -58,6 +68,8 @@ ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts(46,15): err
}

module clodule4 {
~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
class D {
name: T;
~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts(5,12): error TS2300: Duplicate identifier 'fn'.
ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts(8,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts(10,21): error TS2300: Duplicate identifier 'fn'.


==== ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts (2 errors) ====
==== ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts (3 errors) ====
class clodule<T> {
id: string;
value: T;
Expand All @@ -13,6 +14,8 @@ ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFu
}

module clodule {
~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
// error: duplicate identifier expected
export function fn<T>(x: T, y: T): T {
~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts(5,12): error TS2300: Duplicate identifier 'fn'.
ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts(8,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts(10,21): error TS2300: Duplicate identifier 'fn'.


==== ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts (2 errors) ====
==== ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts (3 errors) ====
class clodule<T> {
id: string;
value: T;
Expand All @@ -13,6 +14,8 @@ ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStati
}

module clodule {
~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
// error: duplicate identifier expected
export function fn<T>(x: T, y: T): T {
~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.ts(8,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.ts(11,24): error TS2341: Property 'sfn' is private and only accessible within class 'clodule<T>'.


==== ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.ts (1 errors) ====
==== ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.ts (2 errors) ====
class clodule<T> {
id: string;
value: T;
Expand All @@ -10,6 +11,8 @@ ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics
}

module clodule {
~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
// error: duplicate identifier expected
export function fn<T>(x: T, y: T): number {
return clodule.sfn('a');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(4,12): error TS2300: Duplicate identifier 'Origin'.
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(7,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(8,21): error TS2300: Duplicate identifier 'Origin'.
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(12,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(16,16): error TS2300: Duplicate identifier 'Origin'.
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(19,19): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(20,25): error TS2300: Duplicate identifier 'Origin'.


==== ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts (4 errors) ====
==== ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts (7 errors) ====
class Point {
constructor(public x: number, public y: number) { }

Expand All @@ -14,13 +17,17 @@ ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(20
}

module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export function Origin() { return null; } //expected duplicate identifier error
~~~~~~
!!! error TS2300: Duplicate identifier 'Origin'.
}


module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export class Point {
constructor(public x: number, public y: number) { }

Expand All @@ -30,6 +37,8 @@ ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts(20
}

export module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export function Origin() { return ""; }//expected duplicate identifier error
~~~~~~
!!! error TS2300: Duplicate identifier 'Origin'.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.ts(7,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.ts(12,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.ts(19,19): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.ts (3 errors) ====
class Point {
constructor(public x: number, public y: number) { }

static Origin(): Point { return { x: 0, y: 0 }; }
}

module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
function Origin() { return ""; }// not an error, since not exported
}


module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export class Point {
constructor(public x: number, public y: number) { }

static Origin(): Point { return { x: 0, y: 0 }; }
}

export module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
function Origin() { return ""; }// not an error since not exported
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(4,12): error TS2300: Duplicate identifier 'Origin'.
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(7,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(8,16): error TS2300: Duplicate identifier 'Origin'.
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(12,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(16,16): error TS2300: Duplicate identifier 'Origin'.
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(19,19): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(20,20): error TS2300: Duplicate identifier 'Origin'.


==== ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts (4 errors) ====
==== ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts (7 errors) ====
class Point {
constructor(public x: number, public y: number) { }

Expand All @@ -14,13 +17,17 @@ ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(20,20):
}

module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var Origin = ""; //expected duplicate identifier error
~~~~~~
!!! error TS2300: Duplicate identifier 'Origin'.
}


module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export class Point {
constructor(public x: number, public y: number) { }

Expand All @@ -30,6 +37,8 @@ ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts(20,20):
}

export module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export var Origin = ""; //expected duplicate identifier error
~~~~~~
!!! error TS2300: Duplicate identifier 'Origin'.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.ts(7,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.ts(12,8): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.ts(19,19): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.ts (3 errors) ====
class Point {
constructor(public x: number, public y: number) { }

static Origin: Point = { x: 0, y: 0 };
}

module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
var Origin = ""; // not an error, since not exported
}


module A {
~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
export class Point {
constructor(public x: number, public y: number) { }

static Origin: Point = { x: 0, y: 0 };
}

export module Point {
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
var Origin = ""; // not an error since not exported
}
}
Loading
Loading