Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 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
  •  
  •  
  •  
40 changes: 37 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,8 @@
VariableLikeDeclaration,
VariableStatement,
VarianceFlags,
Version,

Check warning on line 1126 in src/compiler/checker.ts

View workflow job for this annotation

GitHub Actions / copilot

'Version' is defined but never used. Allowed unused vars must match /^(_+$|_[^_])/u
versionMajorMinor,

Check warning on line 1127 in src/compiler/checker.ts

View workflow job for this annotation

GitHub Actions / copilot

'versionMajorMinor' is defined but never used. Allowed unused vars must match /^(_+$|_[^_])/u
visitEachChild as visitEachChildWorker,
visitNode,
visitNodes,
Expand Down Expand Up @@ -47998,9 +48000,41 @@
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 we should generate an error (TS 6.0+) or suggestion (older versions)
const currentVersion = new Version(versionMajorMinor);
const errorVersion = new Version("6.0");
const shouldError = currentVersion.compareTo(errorVersion) >= Comparison.EqualTo;

// Check if ignoreDeprecations should suppress this error
let shouldSuppress = false;
if (shouldError && compilerOptions.ignoreDeprecations) {
// Only valid ignoreDeprecations values: "5.0" and "6.0"
if (compilerOptions.ignoreDeprecations === "6.0") {
shouldSuppress = true;
}
}

if (shouldError && !shouldSuppress) {
// In TypeScript 6.0+, this is an error unless suppressed by ignoreDeprecations
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 {
// In older versions or when suppressed, 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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,10 @@
"code": 1540,
"reportsDeprecated": true
},
"The 'module' keyword is not allowed for namespace declarations. Use the 'namespace' keyword instead.": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place this in numerical sort order according to code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the error message to the correct numerical position (after code 1546, before the jump to 2200). Commit: 3a60026

"category": "Error",
"code": 1547
},
"Type-only import of an ECMAScript module from a CommonJS module must have a 'resolution-mode' attribute.": {
"category": "Error",
"code": 1541
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
Loading