Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
Right now a node's.parent property is loosely typed asTESTree.Node. This is pretty sucky as it means that users need to either add unnecessary logic or explicit casts to refine types in some cases.
There are a number of nodes wherein we know exactly what the parent node will always be due to the defined structure of the AST.
For example, we know that aVariableDeclarator will always have aVariableDeclaration parent.
We should define some of these simpler relationships in our types to improve the experience of consuming the AST types.
Some cases we can define:
AccessorPropertyhas parentClassBody(feat(ast-spec): add parent property to AccessorProperty node types #9487)CatchClausehas parentTryStatementClassBodyhas parentClassDeclaration | ClassExpression | TSAbstractClassDeclarationExportSpecifierhas parentExportNamedDeclarationImportAttributehas parentImportDeclaration | ImportExpressionImportDefaultSpecifierhas parentImportDeclarationImportNamespaceSpecifierhas parentImportDeclarationImportSpecifierhas parentExportAllDeclaration | ExportNamedDeclaration | ImportDeclarationJSXAttributehas parentJSXOpeningElementJSXClosingElementhas parentJSXElementJSXClosingFragmenthas parentJSXFragmentJSXOpeningElementhas parentJSXElementJSXOpeningFragmenthas parentJSXFragmentJSXSpreadAttributehas parentJSXOpeningElementMethodDefinitionhas parentClassBodyPropertyhas parentObjectExpression | ObjectPatternPropertyDefinitionhas parentClassBodySpreadElementhas parentArrayExpression | CallExpression | ObjectExpressionStaticBlockhas parentClassBodySwitchCasehas parentSwitchStatementTemplateElementhas parentTemplateLiteral | TSTemplateLiteralTypeTSAbstractAccessorPropertyhas parentClassBodyTSAbstractMethodDefinitionhas parentClassBodyTSAbstractPropertyDefinitionhas parentClassBodyTSCallSignatureDeclarationhas parentTSInterfaceBody | TSTypeLiteralTSConstructSignatureDeclarationhas parentTSInterfaceBody | TSTypeLiteralTSClassImplementshas parentClassDeclaration | ClassExpressionTSEnumMemberhas parentTSEnumDeclarationTSIndexSignaturehas parentClassBody | TSInterfaceBody | TSTypeLiteralTSInterfaceBodyhas parentTSInterfaceDeclarationTSInterfaceHeritagehas parentTSInterfaceBodyTSMethodSignaturehas parentTSInterfaceBody | TSTypeLiteralTSModuleBlockhas parentTSModuleDeclarationTSParameterPropertyhas parentFunctionLikeTSPropertySignaturehas parentTSInterfaceBody | TSTypeLiteralTSTypeParameterhas parentTSInferType | TSTypeParameterDeclaration | TSMappedTypeVariableDeclaratorhas parentVariableDeclaration(feat(typescript-estree): restrict variable declarator definite/init combinations #9228)
Note: the implementor should double check that I've written these correctly by comparing them against the AST types.
The import thing to note is that we shouldn't define all of the relationships that exist - just a subset of them that we can easily statically define and maintain. I'd probably put a rule around ~3 parent types being the maximum.