[estree] make function declaration id nullable#24854
Conversation
|
@stas-vilchik Thank you for submitting this PR! 🔔 @RReverser - please review this PR in the next few days. Be sure to explicitly select If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead. |
|
For compatibility with existing code and better typecheck, I'd prefer this to be forked into separate interface which could only appear in export. |
|
@RReverser I'm not sure what could be the best way to do that. If I created a new interface with nullable export interface ExportDefaultFunctionDeclaration extends BaseFunction, BaseDeclaration {
type: "FunctionDeclaration";
id: Identifier | null;
body: BlockStatement;
}it would share the same const rule: Rule.RuleModule = {
create(context: Rule.RuleContext) {
return {
FunctionDeclaration(node: Node) {
(node as FunctionDeclaration).id; // nullable!
},In this case I'd need to remember (or even know) that I can't safely cast |
types/estree/index.d.ts
Outdated
| export interface FunctionDeclaration extends BaseFunction, BaseDeclaration { | ||
| type: "FunctionDeclaration"; | ||
| id: Identifier; | ||
| // `id` is null when a function declaration is a part of the `export default function` statement |
There was a problem hiding this comment.
This could be a JSDoc comment
| // Declarations | ||
| var functionDeclaration: ESTree.FunctionDeclaration; | ||
| identifier = functionDeclaration.id; | ||
| var identifierOrNull: ESTree.Identifier | null = functionDeclaration.id; |
There was a problem hiding this comment.
functionDeclaration.id = null would be a better test, as the identifierOrNull test wouldn't fail even without the change to the declaration.
There was a problem hiding this comment.
I think I'll keep both. In any case you can assign nullable functionDeclaration.id to a not nullable identifier.
|
Not sure what is |
|
The only way to do that would be to know what the type of the parent node is, and that is not part of the type of a node. Even if you do add another interface, say, By the way, while we're at it, I just noticed that Possible idea to do something close to what you want: change |
|
@RReverser my bad, I should have mentioned that it was an example of eslint rule. Its visitor relies on the node type and returns a generic @Kovensky thanks for the review comments and for the Anyway I think it'd better if the |
2b8f795 to
b1d6e26
Compare
|
I surely understand the complexities of updating this in a clean way, but want to find a way that would break as little code out there as possible. |
|
@RReverser I understand, I just don't know how to achieve that. |
|
A definition author has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped! |
|
The TypeScript syntax tree suffers the same problem and it's a big source of bugs. I think the fix as-is is honestly the right one - attempting to split out the tree differently doesn't really work because it's really a matrix (export, default, named, async, etc) rather than a directable graph. @RReverser without a concrete better PR I think this one should be merged. Any objections? |
|
Yeah, I think at this point trying to split out turns out to be too complicated to be worth it. We can always try to improve AST in future. @stas-vilchik sorry for the delay on this one! |
|
🌟 🎈 🎉 🏆 🎂 ✨ ⭐️ Congratulations on your first DefinitelyTyped contribution! 🌟 🎈 🎉 🏆 🎂 ✨ ⭐️ |
|
Great, thanks! |
A
FunctionDeclarationidcan benullif this function declaration is a part of theExportDefaultDeclaration(ast explorer gist):This PR updates the
FunctionDeclarationinterface.