Skip to main content
The SourceCode class represents parsed source code and provides methods for working with the AST (Abstract Syntax Tree), tokens, comments, and source text.

Constructor

textOrConfig
string | object
required
Either the source code text or a configuration object.
ast
ASTNode
The Program node (when using the simple string constructor).

Properties

text

text
string
The full source code text.

ast

ast
ASTNode
The Program node of the Abstract Syntax Tree.

lines

lines
string[]
Array of lines in the source code (split by line breaks).

hasBOM

hasBOM
boolean
Whether the source code has a Unicode Byte Order Mark.

parserServices

parserServices
object
Additional services provided by the parser (e.g., TypeScript type information).

scopeManager

scopeManager
ScopeManager
The scope manager containing scope analysis results.

visitorKeys

visitorKeys
object
Visitor keys for traversing the AST.

Methods

getText()

Gets the source code text for a node or token.
nodeOrToken
ASTNode | Token
The node or token to get text for. If omitted, returns entire source.
beforeCount
number
default:"0"
Number of characters before the node to include.
afterCount
number
default:"0"
Number of characters after the node to include.
text
string
The source code text.

getLines()

Gets the source code lines.
lines
string[]
Array of source code lines.

getAllComments()

Gets all comments in the source code.
comments
Token[]
Array of comment tokens.

getComments()

Gets comments for a specific node.
node
ASTNode
required
The node to get comments for.
comments
object

getLoc()

Gets the location information for a node or token.
nodeOrToken
ASTNode | Token
required
The node or token.
loc
object

getRange()

Gets the range for a node or token.
nodeOrToken
ASTNode | Token
required
The node or token.
range
[number, number]
The [start, end] character indices (0-based).

getTokens()

Gets tokens for a node.
node
ASTNode
required
The node to get tokens for.
beforeCount
number
default:"0"
Number of tokens before the node to include.
afterCount
number
default:"0"
Number of tokens after the node to include.
tokens
Token[]
Array of tokens.

getFirstToken()

Gets the first token of a node.
node
ASTNode
required
The node.
options
number | function | object
Skip count, filter function, or options object.
token
Token | null
The first token, or null if not found.

getLastToken()

Gets the last token of a node.
node
ASTNode
required
The node.
options
number | function | object
Skip count, filter function, or options object.
token
Token | null
The last token, or null if not found.

getTokenBefore()

Gets the token before a node or token.
nodeOrToken
ASTNode | Token
required
The node or token.
options
number | function | object
Skip count, filter function, or options object.
token
Token | null
The previous token, or null if not found.

getTokenAfter()

Gets the token after a node or token.
nodeOrToken
ASTNode | Token
required
The node or token.
options
number | function | object
Skip count, filter function, or options object.
token
Token | null
The next token, or null if not found.

getFirstTokenBetween()

Gets the first token between two nodes or tokens.
left
ASTNode | Token
required
The left node or token.
right
ASTNode | Token
required
The right node or token.
options
number | function | object
Skip count, filter function, or options object.
token
Token | null
The first token between, or null if not found.

getTokensBetween()

Gets all tokens between two nodes or tokens.
left
ASTNode | Token
required
The left node or token.
right
ASTNode | Token
required
The right node or token.
padding
number
default:"0"
Number of extra tokens to include on each side.
tokens
Token[]
Array of tokens between the two nodes/tokens.

getTokenByRangeStart()

Gets a token by its range start position.
index
number
required
The range start index.
options
object
Options for the search.
token
Token | null
The token at that position, or null.

getNodeByRangeIndex()

Gets the deepest node containing a given index.
index
number
required
The character index.
node
ASTNode | null
The deepest node containing the index, or null.

isSpaceBetween()

Checks if there is whitespace between two nodes or tokens.
first
ASTNode | Token
required
The first node or token.
second
ASTNode | Token
required
The second node or token.
hasSpace
boolean
True if there is whitespace between them.

getScope()

Gets the scope for a node.
node
ASTNode
required
The node to get the scope for.
scope
Scope
The scope object.

markVariableAsUsed()

Marks a variable as used in a specific scope.
name
string
required
The variable name.
node
ASTNode
The node to start the search from.
found
boolean
True if the variable was found and marked.

getAncestors()

Gets the ancestor nodes of a given node.
node
ASTNode
required
The node.
ancestors
ASTNode[]
Array of ancestor nodes from closest to farthest.

getDeclaredVariables()

Gets variables declared by a node.
node
ASTNode
required
The node.
variables
Variable[]
Array of declared variables.

Examples

Usage in Rules

The SourceCode object is available in rules through the context object: