Andrew's Digital Garden

Tips for ts-morph

findReferencesAsNodes can be a useful traversal pattern. Rather than searching every file, start at the declaration and populate your file list with the references. Note that it gets every reference, not every file that has a reference.

isKind(SyntaxKind.??) is useful to narrow things down, and acts as a type guard.

JsxElement.getAttribute('name') is useful for getting a specific prop on a JSX element getName and getInitializer can be used for the key and value, respectively

sourceFile.fixUnusedIdentifiers can remove unused imports without having to do it yourself

print is a helpful debug tool, as well as getKind and getKindName

[[codemods]] [[staticcodeanalysis]] [[ts]]

Tips for ts-morph