Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 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
  •  
  •  
  •  
27 changes: 24 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47998,9 +47998,30 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const sourceFile = getSourceFileOfNode(node);
const pos = getNonModifierTokenPosOfNode(node);
const span = getSpanOfTokenAtPosition(sourceFile, pos);
suggestionDiagnostics.add(
createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead),
);

// Check if ignoreDeprecations should suppress this error
const shouldSuppress = compilerOptions.ignoreDeprecations === "6.0";

if (shouldSuppress) {
// When suppressed by ignoreDeprecations, keep as suggestion
const suggestionDiagnostic = createFileDiagnostic(
sourceFile,
span.start,
span.length,
Diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead,
);
suggestionDiagnostics.add(suggestionDiagnostic);
}
else {
// Generate error for module keyword usage in namespace declarations
const errorDiagnostic = createFileDiagnostic(
sourceFile,
span.start,
span.length,
Diagnostics.The_module_keyword_is_not_allowed_for_namespace_declarations_Use_the_namespace_keyword_instead,
);
diagnostics.add(errorDiagnostic);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,10 @@
"category": "Error",
"code": 1546
},
"The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.": {
"category": "Error",
"code": 1547
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.d.ts(1,9): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== module.d.ts (1 errors) ====
declare module Point {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
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,19 @@
module.d.ts(1,9): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== module.d.ts (1 errors) ====
declare module Point {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
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,7 +1,8 @@
ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.ts(8,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
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 TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
// 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
@@ -0,0 +1,22 @@
EnumAndModuleWithSameNameAndCommonRoot.ts(5,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== EnumAndModuleWithSameNameAndCommonRoot.ts (1 errors) ====
enum enumdule {
Red, Blue
}

module enumdule {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

export class Point {
constructor(public x: number, public y: number) { }
}
}

var x: enumdule;
var x = enumdule.Red;

var y: { x: number; y: number };
var y = new enumdule.Point(0, 0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ExportClassWhichExtendsInterfaceWithInaccessibleType.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ExportClassWhichExtendsInterfaceWithInaccessibleType.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

interface Point {
x: number;
y: number;

fromOrigin(p: Point): number;
}

export class Point2d implements Point {
constructor(public x: number, public y: number) { }

fromOrigin(p: Point) {
return 1;
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ExportFunctionWithAccessibleTypesInParameterAndReturnTypeAnnotation.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ExportFunctionWithAccessibleTypesInParameterAndReturnTypeAnnotation.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

export class Point {
x: number;
y: number;
}

export class Line {
constructor(public start: Point, public end: Point) { }
}

export function fromOrigin(p: Point): Line {
return new Line({ x: 0, y: 0 }, p);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ExportFunctionWithInaccessibleTypesInParameterTypeAnnotation.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ExportFunctionWithInaccessibleTypesInParameterTypeAnnotation.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

class Point {
x: number;
y: number;
}

export class Line {
constructor(public start: Point, public end: Point) { }
}

export function fromOrigin(p: Point): Line {
return new Line({ x: 0, y: 0 }, p);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ExportFunctionWithInaccessibleTypesInReturnTypeAnnotation.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ExportFunctionWithInaccessibleTypesInReturnTypeAnnotation.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

export class Point {
x: number;
y: number;
}

class Line {
constructor(public start: Point, public end: Point) { }
}

export function fromOrigin(p: Point): Line {
return new Line({ x: 0, y: 0 }, p);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInNestedMemberTypeAnnotations.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInNestedMemberTypeAnnotations.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

class Point {
constructor(public x: number, public y: number) { }
}

export var UnitSquare : {
top: { left: Point, right: Point },
bottom: { left: Point, right: Point }
} = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ExportVariableWithInaccessibleTypeInTypeAnnotation.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ExportVariableWithInaccessibleTypeInTypeAnnotation.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

export interface Point {
x: number;
y: number;
}

// valid since Point is exported
export var Origin: Point = { x: 0, y: 0 };

interface Point3d extends Point {
z: number;
}

// invalid Point3d is not exported
export var Origin3d: Point3d = { x: 0, y: 0, z: 0 };
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
module.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
module.ts(2,12): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== function.ts (1 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export function Point() {
return { x: 0, y: 0 };
}
}

==== module.ts (2 errors) ====
module B {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export module Point {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export var Origin = { x: 0, y: 0 };
}
}

==== test.ts (0 errors) ====
var fn: () => { x: number; y: number };
var fn = A.Point;

var cl: { x: number; y: number; }
var cl = B.Point.Origin;

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ModuleAndEnumWithSameNameAndCommonRoot.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== ModuleAndEnumWithSameNameAndCommonRoot.ts (1 errors) ====
module enumdule {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.

export class Point {
constructor(public x: number, public y: number) { }
}
}

enum enumdule {
Red, Blue
}

var x: enumdule;
var x = enumdule.Red;

var y: { x: number; y: number };
var y = new enumdule.Point(0, 0);
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
ModuleWithExportedAndNonExportedImportAlias.ts(1,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
ModuleWithExportedAndNonExportedImportAlias.ts(12,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
ModuleWithExportedAndNonExportedImportAlias.ts(18,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
ModuleWithExportedAndNonExportedImportAlias.ts(37,21): error TS2339: Property 'Lines' does not exist on type 'typeof Geometry'.


==== ModuleWithExportedAndNonExportedImportAlias.ts (1 errors) ====
==== ModuleWithExportedAndNonExportedImportAlias.ts (4 errors) ====
module A {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export interface Point {
x: number;
y: number;
Expand All @@ -14,12 +19,16 @@ ModuleWithExportedAndNonExportedImportAlias.ts(37,21): error TS2339: Property 'L
}

module B {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export class Line {
constructor(public start: A.Point, public end: A.Point) { }
}
}

module Geometry {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export import Points = A;
import Lines = B;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
accessorsInAmbientContext.ts(1,9): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
accessorsInAmbientContext.ts(3,17): error TS1183: An implementation cannot be declared in ambient contexts.
accessorsInAmbientContext.ts(4,18): error TS1183: An implementation cannot be declared in ambient contexts.
accessorsInAmbientContext.ts(6,24): error TS1183: An implementation cannot be declared in ambient contexts.
Expand All @@ -8,8 +9,10 @@ accessorsInAmbientContext.ts(15,20): error TS1183: An implementation cannot be d
accessorsInAmbientContext.ts(16,21): error TS1183: An implementation cannot be declared in ambient contexts.


==== accessorsInAmbientContext.ts (8 errors) ====
==== accessorsInAmbientContext.ts (9 errors) ====
declare module M {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
class C {
get X() { return 1; }
~
Expand Down
5 changes: 4 additions & 1 deletion tests/baselines/reference/aliasesInSystemModule1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
aliasesInSystemModule1.ts(1,24): error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
aliasesInSystemModule1.ts(9,1): error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.


==== aliasesInSystemModule1.ts (1 errors) ====
==== aliasesInSystemModule1.ts (2 errors) ====
import alias = require('foo');
~~~~~
!!! error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
Expand All @@ -13,6 +14,8 @@ aliasesInSystemModule1.ts(1,24): error TS2792: Cannot find module 'foo'. Did you
let z = new cls2();

module M {
~~~~~~
!!! error TS1547: The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.
export import cls = alias.Class;
let x = new alias.Class();
let y = new cls();
Expand Down
Loading