Skip to content

Commit 7d1966b

Browse files
authored
Merge pull request microsoft#15176 from HerringtonDarkholme/namespace-error
fix microsoft#15155: improve namespace module error message
2 parents d129199 + e9fd2bc commit 7d1966b

20 files changed

Lines changed: 80 additions & 53 deletions

src/compiler/checker.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ namespace ts {
10561056
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) &&
10571057
!checkAndReportErrorForExtendingInterface(errorLocation) &&
10581058
!checkAndReportErrorForUsingTypeAsNamespace(errorLocation, name, meaning) &&
1059-
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) {
1059+
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) &&
1060+
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning)) {
10601061
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
10611062
}
10621063
}
@@ -1200,6 +1201,24 @@ namespace ts {
12001201
return false;
12011202
}
12021203

1204+
function checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation: Node, name: string, meaning: SymbolFlags): boolean {
1205+
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule & ~SymbolFlags.Type)) {
1206+
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.NamespaceModule & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
1207+
if (symbol) {
1208+
error(errorLocation, Diagnostics.Cannot_use_namespace_0_as_a_value, name);
1209+
return true;
1210+
}
1211+
}
1212+
else if (meaning & (SymbolFlags.Type & ~SymbolFlags.NamespaceModule & ~SymbolFlags.Value)) {
1213+
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.NamespaceModule & ~SymbolFlags.Type, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
1214+
if (symbol) {
1215+
error(errorLocation, Diagnostics.Cannot_use_namespace_0_as_a_type, name);
1216+
return true;
1217+
}
1218+
}
1219+
return false;
1220+
}
1221+
12031222
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {
12041223
Debug.assert(!!(result.flags & SymbolFlags.BlockScopedVariable || result.flags & SymbolFlags.Class || result.flags & SymbolFlags.Enum));
12051224
// Block-scoped variables cannot be used before their definition

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,14 @@
20992099
"category": "Error",
21002100
"code": 2707
21012101
},
2102+
"Cannot use namespace '{0}' as a value.": {
2103+
"category": "Error",
2104+
"code": 2708
2105+
},
2106+
"Cannot use namespace '{0}' as a type.": {
2107+
"category": "Error",
2108+
"code": 2709
2109+
},
21022110

21032111
"Import declaration '{0}' is using private name '{1}'.": {
21042112
"category": "Error",
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/internalModules/moduleDeclarations/InvalidNonInstantiatedModule.ts(5,9): error TS2304: Cannot find name 'M'.
2-
tests/cases/conformance/internalModules/moduleDeclarations/InvalidNonInstantiatedModule.ts(7,15): error TS2304: Cannot find name 'M'.
1+
tests/cases/conformance/internalModules/moduleDeclarations/InvalidNonInstantiatedModule.ts(5,9): error TS2708: Cannot use namespace 'M' as a value.
2+
tests/cases/conformance/internalModules/moduleDeclarations/InvalidNonInstantiatedModule.ts(7,15): error TS2708: Cannot use namespace 'M' as a value.
33

44

55
==== tests/cases/conformance/internalModules/moduleDeclarations/InvalidNonInstantiatedModule.ts (2 errors) ====
@@ -9,9 +9,9 @@ tests/cases/conformance/internalModules/moduleDeclarations/InvalidNonInstantiate
99

1010
var m = M; // Error, not instantiated can not be used as var
1111
~
12-
!!! error TS2304: Cannot find name 'M'.
12+
!!! error TS2708: Cannot use namespace 'M' as a value.
1313

1414
var x: typeof M; // Error only a namespace
1515
~
16-
!!! error TS2304: Cannot find name 'M'.
16+
!!! error TS2708: Cannot use namespace 'M' as a value.
1717

tests/baselines/reference/aliasOnMergedModuleInterface.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/aliasOnMergedModuleInterface_1.ts(5,16): error TS2304: Cannot find name 'foo'.
1+
tests/cases/compiler/aliasOnMergedModuleInterface_1.ts(5,16): error TS2708: Cannot use namespace 'foo' as a value.
22

33

44
==== tests/cases/compiler/aliasOnMergedModuleInterface_1.ts (1 errors) ====
@@ -8,7 +8,7 @@ tests/cases/compiler/aliasOnMergedModuleInterface_1.ts(5,16): error TS2304: Cann
88
z.bar("hello"); // This should be ok
99
var x: foo.A = foo.bar("hello"); // foo.A should be ok but foo.bar should be error
1010
~~~
11-
!!! error TS2304: Cannot find name 'foo'.
11+
!!! error TS2708: Cannot use namespace 'foo' as a value.
1212

1313
==== tests/cases/compiler/aliasOnMergedModuleInterface_0.ts (0 errors) ====
1414
declare module "foo"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/compiler/assignToModule.ts(2,1): error TS2304: Cannot find name 'A'.
1+
tests/cases/compiler/assignToModule.ts(2,1): error TS2708: Cannot use namespace 'A' as a value.
22

33

44
==== tests/cases/compiler/assignToModule.ts (1 errors) ====
55
module A {}
66
A = undefined; // invalid LHS
77
~
8-
!!! error TS2304: Cannot find name 'A'.
8+
!!! error TS2708: Cannot use namespace 'A' as a value.

tests/baselines/reference/assignmentToReferenceTypes.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/assignmentToReferenceTypes.ts(5,1): error TS2304: Cannot find name 'M'.
1+
tests/cases/compiler/assignmentToReferenceTypes.ts(5,1): error TS2708: Cannot use namespace 'M' as a value.
22
tests/cases/compiler/assignmentToReferenceTypes.ts(9,1): error TS2539: Cannot assign to 'C' because it is not a variable.
33
tests/cases/compiler/assignmentToReferenceTypes.ts(13,1): error TS2539: Cannot assign to 'E' because it is not a variable.
44
tests/cases/compiler/assignmentToReferenceTypes.ts(16,1): error TS2539: Cannot assign to 'f' because it is not a variable.
@@ -11,7 +11,7 @@ tests/cases/compiler/assignmentToReferenceTypes.ts(16,1): error TS2539: Cannot a
1111
}
1212
M = null;
1313
~
14-
!!! error TS2304: Cannot find name 'M'.
14+
!!! error TS2708: Cannot use namespace 'M' as a value.
1515

1616
class C {
1717
}

tests/baselines/reference/assignments.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(11,1): error TS2304: Cannot find name 'M'.
1+
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(11,1): error TS2708: Cannot use namespace 'M' as a value.
22
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(14,1): error TS2539: Cannot assign to 'C' because it is not a variable.
33
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(17,1): error TS2539: Cannot assign to 'E' because it is not a variable.
44
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(18,3): error TS2540: Cannot assign to 'A' because it is a constant or a read-only property.
@@ -19,7 +19,7 @@ tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(31,1): er
1919
module M { }
2020
M = null; // Error
2121
~
22-
!!! error TS2304: Cannot find name 'M'.
22+
!!! error TS2708: Cannot use namespace 'M' as a value.
2323

2424
class C { }
2525
C = null; // Error

tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tests/cases/compiler/defaultArgsInFunctionExpressions.ts(11,1): error TS2322: Ty
55
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(14,51): error TS2352: Type 'string' cannot be converted to type 'number'.
66
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(17,41): error TS2322: Type '""' is not assignable to type 'number'.
77
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(20,62): error TS2352: Type 'string' cannot be converted to type 'number'.
8-
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(28,15): error TS2304: Cannot find name 'T'.
8+
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(28,15): error TS2708: Cannot use namespace 'T' as a value.
99

1010

1111
==== tests/cases/compiler/defaultArgsInFunctionExpressions.ts (8 errors) ====
@@ -52,7 +52,7 @@ tests/cases/compiler/defaultArgsInFunctionExpressions.ts(28,15): error TS2304: C
5252

5353
var f6 = (t = T) => { };
5454
~
55-
!!! error TS2304: Cannot find name 'T'.
55+
!!! error TS2708: Cannot use namespace 'T' as a value.
5656
var f7 = (t = U) => { return t; };
5757

5858
f7().x;

tests/baselines/reference/genericTypeReferenceWithoutTypeArgument2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
1515
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(18,38): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
1616
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(20,17): error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'?
1717
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(23,21): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
18-
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(29,18): error TS2304: Cannot find name 'M'.
18+
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(29,18): error TS2708: Cannot use namespace 'M' as a value.
1919
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(30,24): error TS2314: Generic type 'E<T>' requires 1 type argument(s).
2020
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(31,24): error TS2694: Namespace 'M' has no exported member 'C'.
2121
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(33,22): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
@@ -89,7 +89,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
8989

9090
class D2 extends M.C { }
9191
~
92-
!!! error TS2304: Cannot find name 'M'.
92+
!!! error TS2708: Cannot use namespace 'M' as a value.
9393
interface D3<T extends M.E> { }
9494
~~~
9595
!!! error TS2314: Generic type 'E<T>' requires 1 type argument(s).

tests/baselines/reference/importDeclWithClassModifiers.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
tests/cases/compiler/importDeclWithClassModifiers.ts(5,8): error TS1044: 'public' modifier cannot appear on a module or namespace element.
2-
tests/cases/compiler/importDeclWithClassModifiers.ts(5,26): error TS2304: Cannot find name 'x'.
2+
tests/cases/compiler/importDeclWithClassModifiers.ts(5,26): error TS2708: Cannot use namespace 'x' as a value.
33
tests/cases/compiler/importDeclWithClassModifiers.ts(5,28): error TS2694: Namespace 'x' has no exported member 'c'.
44
tests/cases/compiler/importDeclWithClassModifiers.ts(6,8): error TS1044: 'private' modifier cannot appear on a module or namespace element.
5-
tests/cases/compiler/importDeclWithClassModifiers.ts(6,27): error TS2304: Cannot find name 'x'.
5+
tests/cases/compiler/importDeclWithClassModifiers.ts(6,27): error TS2708: Cannot use namespace 'x' as a value.
66
tests/cases/compiler/importDeclWithClassModifiers.ts(6,29): error TS2694: Namespace 'x' has no exported member 'c'.
77
tests/cases/compiler/importDeclWithClassModifiers.ts(7,8): error TS1044: 'static' modifier cannot appear on a module or namespace element.
8-
tests/cases/compiler/importDeclWithClassModifiers.ts(7,26): error TS2304: Cannot find name 'x'.
8+
tests/cases/compiler/importDeclWithClassModifiers.ts(7,26): error TS2708: Cannot use namespace 'x' as a value.
99
tests/cases/compiler/importDeclWithClassModifiers.ts(7,28): error TS2694: Namespace 'x' has no exported member 'c'.
1010

1111

@@ -18,21 +18,21 @@ tests/cases/compiler/importDeclWithClassModifiers.ts(7,28): error TS2694: Namesp
1818
~~~~~~
1919
!!! error TS1044: 'public' modifier cannot appear on a module or namespace element.
2020
~
21-
!!! error TS2304: Cannot find name 'x'.
21+
!!! error TS2708: Cannot use namespace 'x' as a value.
2222
~
2323
!!! error TS2694: Namespace 'x' has no exported member 'c'.
2424
export private import b = x.c;
2525
~~~~~~~
2626
!!! error TS1044: 'private' modifier cannot appear on a module or namespace element.
2727
~
28-
!!! error TS2304: Cannot find name 'x'.
28+
!!! error TS2708: Cannot use namespace 'x' as a value.
2929
~
3030
!!! error TS2694: Namespace 'x' has no exported member 'c'.
3131
export static import c = x.c;
3232
~~~~~~
3333
!!! error TS1044: 'static' modifier cannot appear on a module or namespace element.
3434
~
35-
!!! error TS2304: Cannot find name 'x'.
35+
!!! error TS2708: Cannot use namespace 'x' as a value.
3636
~
3737
!!! error TS2694: Namespace 'x' has no exported member 'c'.
3838
var b: a;

0 commit comments

Comments
 (0)