benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API
MIT License
1.13k stars 195 forks source link

Failing to traverse identifier in type annotation in TypeScript #510

Open Ayc0 opened 3 years ago

Ayc0 commented 3 years ago

Same as https://github.com/benjamn/ast-types/issues/242 but for TS instead of flow.

I left this comment https://github.com/benjamn/ast-types/issues/242#issuecomment-696789947 initially. But as the issue is closed, I'm re-opening one.

I was using JSCodeShift and noticed that I couldn't select nodes in type hints:

If the input is:

const [state, setState] = React.useState<Enum.A | Enum.B | null>(null);

I cannot find the 2 enums:

root.find("TSQualifiedName")
// []

I created issue facebook/jscodeshift#389 on their repo, but I think the .traverse function comes from this lib instead, so I'm reposting here.

There is a bit more info on the initial issue in jscodeshift (like the full AST, or a code sandbox repro)

polgfred commented 3 years ago

Experiencing the same issue in jscodeshift, and tracked it down to here as well. I'm trying to do a camel case rename, and I've got:

const p = new Promise<{ foo_bar: string }>(resolve => resolve({ foo_bar: 'baz' }))

root.find(j.Identifier) finds the foo_bar in the Promise function, but not the one in the type parameter.

The AST is generated with @babel/parser (jscodeshift --parser=tsx).

ryami333 commented 2 years ago

@benjamn, would you be able to help out here by any chance? Hate to @ you, but this issue is 11 months old so at this point it would appear that the issue has gone unnoticed and adding my thumbs-up alone is unlikely to help surface it :D