benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeParameterDeclarations are not added to scope.types

jedwards1211 opened this issue · comments

For instance in

async function withStatus<T>(fn: () => Promise<T>): Promise<T> {

T doesn't get added to the function's scope.types.

Uhoh...

@benjamn In ECMAScript, only functions and catch clauses introduce new scopes. But flow types throw a wrench into the system: the scope of a type parameter is limited to the declaring class, function, interface, or type alias. This means ast-types is currently unable to represent type parameter scope correctly.

I propose that we solve this by attaching scopes to the paths for class, function, etc. nodes, but flag those scopes such that they only allow type parameters to be declared on them.

Context

I'm trying to use ast-types to locate all undeclared identifiers in JS/Flow code. Right now it's not working on code that uses type parameters.

Yeah, I think it should suffice to add scopes to class, interface, and type alias nodes, and in the scan method for those, only add type parameters to the scope instead of scanning for value bindings.

Though I think it may be possible for type aliases within a function body to shadow type parameters of the function... 😱