collectImports
Collect Java imports from a program node and return the exact names and wildcard packages separately.
Returns
Example
isTypeImported
Return whether fullyQualifiedName is present as an exact import, or its package is covered by a wildcard import.
Parameters
ImportState
required
The value returned by
collectImports.object
required
simpleName— simple type name, such asWidget.fullyQualifiedName— name to match, such ascom.example.Widget. The check uses this name and its package.simpleNameis part of the options object and is not compared on its own.
hasConflictingSimpleImport
Return whether an exact import already binds simpleName to a fully qualified name other than expectedFullyQualifiedName. Wildcard imports are not treated as conflicts.
Parameters
ImportState
required
The value returned by
collectImports.object
required
simpleName— simple type name to check.expectedFullyQualifiedName— the fully qualified name that should own that simple name.
createImportCleanupEdits
Build edits that drop unreferenced imports and add imports whose simple name is used. Returns an empty array when the source already has the right imports. Apply the edits with rootNode.commitEdits. A name counts as used only when it appears as its own identifier, so com.other.Widget does not count as a use of Widget.
Parameters
ImportCleanupOptions
required
removeIfUnreferenced— fully qualified names to drop when that simple name is unused.addIfReferenced— fully qualified names to insert when that simple name is used and the import is missing.
Edit[]
Example
cleanupImports
Run the same cleanup as createImportCleanupEdits on source text and return the rewritten source. Returns the original source when nothing changes.
Parameters
string
required
Java source to rewrite.
ImportCleanupOptions
required
Same fields as
createImportCleanupEdits.simpleName
Return the text after the last . in a fully qualified name. com.example.Widget returns Widget. A name with no . is returned unchanged.
getImportPath
Return the imported name from a Java import_declaration node. A wildcard import keeps the .* suffix, so import com.example.*; yields com.example.*. Returns null when the declaration has no scoped name.
parseImportDeclaration
Return the imported name from a single import ...; declaration, or null when the text is not one. import com.example.Widget; returns com.example.Widget. A wildcard keeps the .* suffix.
referencesIdentifier
Return whether name appears as its own identifier or type identifier under rootNode. Names inside import declarations are ignored. A name that is only part of a fully qualified type, such as Widget in com.other.Widget, is ignored.
findVisibleDeclarationBeforeUsage
Find the parameter, local, or field named name that is visible at usageNode. A local or parameter declared before the use wins over a field of the same name. Returns null when nothing visible matches.
Parameters
object
required
usageNode— the node where the name is used.name— the identifier to resolve.
DeclarationInfo | null
Example
findEnclosingNode
Return the nearest ancestor of node whose tree-sitter kind is kind, or null when none matches.
findDirectChild
Return the child of parent that contains descendant, or null when descendant is not under parent.
Example
findTypeNode
Return the named child of node that holds its type, or null when there is none. Matches generic types, type identifiers, scoped types, and primitive types such as int, boolean, and void.
replaceTypeIdentifierSafely
Return an edit that replaces a type name, or null when the node should stay as it is. Import declarations are left alone. A name that is only one part of a fully qualified type is left alone. Generic arguments on the replaced type are kept, so ListenableFuture<String> can become CompletableFuture<String>.
Example
baseTypeName
Return a type name with its generic arguments removed. List<String> returns List. A name with no < is returned trimmed.
replaceBaseTypeName
Return typeText with its base name replaced and its generic arguments kept. List<String> replaced with Set returns Set<String>.
isTypeShadowed
Return whether simpleName is already bound to a different type. A conflicting exact import counts. A class in the same file with that simple name also counts.
Parameters
object
required
simpleName— simple type name to check.expectedFullyQualifiedName— the fully qualified name that should own that simple name.
isKnownType
Return whether typeText refers to fullyQualifiedName. An exact fully qualified typeText matches. A simple name matches when that type is imported, no conflicting import exists, and no class in the file shadows the name.
Example
getMethodInvocationParts
Split a method invocation into its receiver, name, and arguments. Returns null when invocation is not a method_invocation node.
Returns
MethodInvocationParts | null
Example
getReceiverIdentifier
Return the identifier text for a receiver. For a field access, return the field name. Returns null when receiver is null or has no identifier.
getAnonymousClassMethod
Return the method named methodName inside an anonymous class creation, or null when the node is not an object_creation_expression or that method is missing.
getAnonymousClassMethods
Return a map from each requested method name to its method node. Returns null if any requested method is missing.
Example
getSingleParameterName
Return the parameter name when a method has exactly one parameter, or null when it has any other number of parameters.
getMethodBodyContent
Return the text inside a method body, without the surrounding braces. Returns null when the method has no block.
Parameters
{ from: string; to: string }
When set, identifiers equal to
from are renamed to to in the body before the text is returned.renameIdentifiersInNode
Rename identifiers equal to from inside node and return the rewritten text. Returns the original text when no identifier matches.