SourceCode class represents parsed source code and provides methods for working with the AST (Abstract Syntax Tree), tokens, comments, and source text.
Constructor
Either the source code text or a configuration object.
The Program node (when using the simple string constructor).
Properties
text
The full source code text.
ast
The Program node of the Abstract Syntax Tree.
lines
Array of lines in the source code (split by line breaks).
hasBOM
Whether the source code has a Unicode Byte Order Mark.
parserServices
Additional services provided by the parser (e.g., TypeScript type information).
scopeManager
The scope manager containing scope analysis results.
visitorKeys
Visitor keys for traversing the AST.
Methods
getText()
Gets the source code text for a node or token.The node or token to get text for. If omitted, returns entire source.
Number of characters before the node to include.
Number of characters after the node to include.
The source code text.
getLines()
Gets the source code lines.Array of source code lines.
getAllComments()
Gets all comments in the source code.Array of comment tokens.
getComments()
Gets comments for a specific node.The node to get comments for.
getLoc()
Gets the location information for a node or token.The node or token.
getRange()
Gets the range for a node or token.The node or token.
The [start, end] character indices (0-based).
getTokens()
Gets tokens for a node.The node to get tokens for.
Number of tokens before the node to include.
Number of tokens after the node to include.
Array of tokens.
getFirstToken()
Gets the first token of a node.The node.
Skip count, filter function, or options object.
The first token, or null if not found.
getLastToken()
Gets the last token of a node.The node.
Skip count, filter function, or options object.
The last token, or null if not found.
getTokenBefore()
Gets the token before a node or token.The node or token.
Skip count, filter function, or options object.
The previous token, or null if not found.
getTokenAfter()
Gets the token after a node or token.The node or token.
Skip count, filter function, or options object.
The next token, or null if not found.
getFirstTokenBetween()
Gets the first token between two nodes or tokens.The left node or token.
The right node or token.
Skip count, filter function, or options object.
The first token between, or null if not found.
getTokensBetween()
Gets all tokens between two nodes or tokens.The left node or token.
The right node or token.
Number of extra tokens to include on each side.
Array of tokens between the two nodes/tokens.
getTokenByRangeStart()
Gets a token by its range start position.The range start index.
Options for the search.
The token at that position, or null.
getNodeByRangeIndex()
Gets the deepest node containing a given index.The character index.
The deepest node containing the index, or null.
isSpaceBetween()
Checks if there is whitespace between two nodes or tokens.The first node or token.
The second node or token.
True if there is whitespace between them.
getScope()
Gets the scope for a node.The node to get the scope for.
The scope object.
markVariableAsUsed()
Marks a variable as used in a specific scope.The variable name.
The node to start the search from.
True if the variable was found and marked.
getAncestors()
Gets the ancestor nodes of a given node.The node.
Array of ancestor nodes from closest to farthest.
getDeclaredVariables()
Gets variables declared by a node.The node.
Array of declared variables.
Examples
- Getting Text
- Working with Tokens
- Checking Spacing
- Scope Analysis
Usage in Rules
TheSourceCode object is available in rules through the context object: