-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Fix symbol display exception when handling incomplete class #44936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
When a class declaration lacks a name, don't throw an exception when
producing the display parts (e.g. for QuickInfo).
Remaining issues:
1. The name shows as "__missing", the name of the underlying symbol,
rather than "(Missing)", as it is for the corresponding function
declaration case (because the parse constructs a missing identifier
node for the function declaration).
2. "(Missing)" is hard-coded, rather than being a localizable resource
string.
3. When an anonymous class declaration is a default export, the
corresponding symbol is named "default", resulting in the confusing
display string "class default".
Since display parts are built using existing `symbolToString`
functionality, it wasn't clear whether detecting special symbol names
and replacing them with user-friendly strings could be done without
breaking other functionality.
Similarly, changing the shape of the parse tree seemed riskier than the
problem justified (the user experience is just not getting QuickInfo for
the incomplete declaration, which seems acceptable).
|
Stack was |
| }, | ||
| "displayParts": [ | ||
| { | ||
| "text": "class", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class default is weird but correct, at least according to commonjs:
- assignment to a variable gives an anonymous function the name of the variable, eg
var f = function () { } - JS classes are just functions.
export default xtranslates toexports.default = xin commonjs.
I think this is in the standard, but at least node and TS agree on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node appears to agree even in ESM mode:
foo.mjs
export default class {}bar.mjs
import foo from './foo.mjs';
console.log(foo);> node bar.mjs
[class default]
| // for class and function declarations, use the `default` modifier | ||
| // when the declaration is unnamed. | ||
| const defaultModifier = find(node.modifiers!, isDefaultModifier); | ||
| const defaultModifier = node.modifiers && find(node.modifiers, isDefaultModifier); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also do
| const defaultModifier = node.modifiers && find(node.modifiers, isDefaultModifier); | |
| const defaultModifier = forEach(node.modifiers, isDefaultModifier); |
or
| const defaultModifier = node.modifiers && find(node.modifiers, isDefaultModifier); | |
| const defaultModifier = node.modifiers?.find(isDefaultModifier); |
…t#44936) When a class declaration lacks a name, don't throw an exception when producing the display parts (e.g. for QuickInfo). Remaining issues: 1. The name shows as "__missing", the name of the underlying symbol, rather than "(Missing)", as it is for the corresponding function declaration case (because the parse constructs a missing identifier node for the function declaration). 2. "(Missing)" is hard-coded, rather than being a localizable resource string. 3. When an anonymous class declaration is a default export, the corresponding symbol is named "default", resulting in the confusing display string "class default". Since display parts are built using existing `symbolToString` functionality, it wasn't clear whether detecting special symbol names and replacing them with user-friendly strings could be done without breaking other functionality. Similarly, changing the shape of the parse tree seemed riskier than the problem justified (the user experience is just not getting QuickInfo for the incomplete declaration, which seems acceptable).


When a class declaration lacks a name, don't throw an exception when
producing the display parts (e.g. for QuickInfo).
Remaining issues:
rather than "(Missing)", as it is for the corresponding function
declaration case (because the parse constructs a missing identifier
node for the function declaration).
string.
corresponding symbol is named "default", resulting in the confusing
display string "class default".
Since display parts are built using existing
symbolToStringfunctionality, it wasn't clear whether detecting special symbol names
and replacing them with user-friendly strings could be done without
breaking other functionality.
Similarly, changing the shape of the parse tree seemed riskier than the
problem justified (the user experience is just not getting QuickInfo for
the incomplete declaration, which seems acceptable).