Improve non-ambient class and function merge error (#44352)

* Improve non-ambient class and function merge error

* Update baselines

* Update tests

Co-authored-by: Austin Cummings <austin@austincummings.com>
This commit is contained in:
Orta Therox
2021-06-03 14:08:04 +01:00
committed by GitHub
parent 9d345e7734
commit 6baa1bec64
19 changed files with 167 additions and 160 deletions

View File

@@ -34057,13 +34057,22 @@ namespace ts {
});
}
if (hasNonAmbientClass && !isConstructor && symbol.flags & SymbolFlags.Function) {
// A non-ambient class cannot be an implementation for a non-constructor function/class merge
// TODO: The below just replicates our older error from when classes and functions were
// entirely unable to merge - a more helpful message like "Class declaration cannot implement overload list"
// might be warranted. :shrug:
if (hasNonAmbientClass && !isConstructor && symbol.flags & SymbolFlags.Function && declarations) {
const relatedDiagnostics = filter(declarations, d => d.kind === SyntaxKind.ClassDeclaration)
.map(d => createDiagnosticForNode(d, Diagnostics.Consider_adding_a_declare_modifier_to_this_class));
forEach(declarations, declaration => {
addDuplicateDeclarationError(declaration, Diagnostics.Duplicate_identifier_0, symbolName(symbol), declarations);
const diagnostic = declaration.kind === SyntaxKind.ClassDeclaration
? Diagnostics.Class_declaration_cannot_implement_overload_list_for_0
: declaration.kind === SyntaxKind.FunctionDeclaration
? Diagnostics.Function_with_bodies_can_only_merge_with_classes_that_are_ambient
: undefined;
if (diagnostic) {
addRelatedInfo(
error(getNameOfDeclaration(declaration) || declaration, diagnostic, symbolName(symbol)),
...relatedDiagnostics
);
}
});
}

View File

@@ -3336,6 +3336,14 @@
"category": "Error",
"code": 2812
},
"Class declaration cannot implement overload list for '{0}'.": {
"category": "Error",
"code": 2813
},
"Function with bodies can only merge with classes that are ambient.": {
"category": "Error",
"code": 2814
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
@@ -5172,6 +5180,11 @@
"category": "Message",
"code": 6505
},
"Consider adding a 'declare' modifier to this class.": {
"category": "Message",
"code": 6506
},
"Include 'undefined' in index signature results": {
"category": "Message",
@@ -5185,7 +5198,7 @@
"category": "Message",
"code": 6802
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005