Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 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
  •  
  •  
  •  
29 changes: 26 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,8 @@ import {
VariableLikeDeclaration,
VariableStatement,
VarianceFlags,
Version,
versionMajorMinor,
visitEachChild as visitEachChildWorker,
visitNode,
visitNodes,
Expand Down Expand Up @@ -47998,9 +48000,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) {
// 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);
}
else {
// 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);
}
}
}

Expand Down
24 changes: 14 additions & 10 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1820,11 +1820,11 @@
"category": "Error",
"code": 1539
},
"A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.": {
"category": "Suggestion",
"code": 1540,
"reportsDeprecated": true
},
"A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.": {
"category": "Suggestion",
"code": 1540,
"reportsDeprecated": true
},
"Type-only import of an ECMAScript module from a CommonJS module must have a 'resolution-mode' attribute.": {
"category": "Error",
"code": 1541
Expand All @@ -1845,11 +1845,15 @@
"category": "Error",
"code": 1545
},
"'await using' declarations are not allowed in ambient contexts.": {
"category": "Error",
"code": 1546
},

"'await using' declarations are not allowed in ambient contexts.": {
"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",
"code": 2200
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
Loading