Unify the way anonymous class symbols are printed in error messages#61943
Unify the way anonymous class symbols are printed in error messages#61943Andarist wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
1 similar comment
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
There was a problem hiding this comment.
Pull Request Overview
This PR unifies the way anonymous class symbols are printed in error messages to ensure consistency across the compiler error outputs.
- Replaces calls to factory-based naming routines with symbolToString for both source and target symbols.
- Updates baseline tests to reflect the new naming format for anonymous classes in types, symbols, and error messages.
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/cases/compiler/privateNamesUnique-6.ts | Adds a test case for anonymous classes with private fields. |
| tests/cases/compiler/privateNameMethodClassExpression2.ts | Adds tests for anonymous class expressions with private methods and fields. |
| tests/baselines/reference/*.types | Updates expected type outputs to the new anonymous class naming. |
| tests/baselines/reference/*.symbols | Updates expected symbol outputs to reflect unified naming. |
| tests/baselines/reference/*.errors.txt | Updates error messages to use consistent symbol representation. |
| src/compiler/checker.ts | Replaces existing naming functions with symbolToString for uniformity in error reporting. |
| right, | ||
| Diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier, | ||
| diagName, | ||
| diagnosticName(typeClass.name || anon), |
There was a problem hiding this comment.
I think anon can now be completely removed. Only checkNoTypeArguments relies on it - but I can't see how it would actually end up in that branch (and there are no tests proving otherwise). I have refrained myself from doing changes there though.
There was a problem hiding this comment.
Perhaps this proposed patch makes it a little bit more clear why I think this anon branch there is essentially never hit:
diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 7716a39688..ef89daf9b9 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -1141,7 +1141,6 @@ import * as moduleSpecifiers from "./_namespaces/ts.moduleSpecifiers.js";
import * as performance from "./_namespaces/ts.performance.js";
const ambientModuleSymbolRegex = /^".+"$/;
-const anon = "(anonymous)" as __String & string;
const enum ReferenceHint {
Unspecified,
@@ -17161,9 +17160,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return !!(node.flags & NodeFlags.JSDoc) && (node.kind === SyntaxKind.TypeReference || node.kind === SyntaxKind.ImportType);
}
- function checkNoTypeArguments(node: NodeWithTypeArguments, symbol?: Symbol) {
+ function checkNoTypeArguments(node: NodeWithTypeArguments, symbol: Symbol): boolean
+ function checkNoTypeArguments(node: TypeReferenceNode): boolean
+ function checkNoTypeArguments(node: TypeReferenceNode | NodeWithTypeArguments, symbol?: Symbol) {
if (node.typeArguments) {
- error(node, Diagnostics.Type_0_is_not_generic, symbol ? symbolToString(symbol) : (node as TypeReferenceNode).typeName ? declarationNameToString((node as TypeReferenceNode).typeName) : anon);
+ let name
+ if (isTypeReferenceNode(node)) {
+ name = declarationNameToString(node.typeName);
+ } else {
+ Debug.assertIsDefined(symbol);
+ name = symbolToString(symbol);
+ }
+ error(node, Diagnostics.Type_0_is_not_generic, name);
return false;
}
return true;This function is always called with a symbol when the argument is NodeWithTypeArguments and it's never called with a symbol when the argument is TypeReferenceNode. And TypeReferenceNode always has a typeName property.
ports microsoft/typescript-go#731
cc @jakebailey