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

const { SourceCode } = require("eslint");
const sourceCode = new SourceCode(textOrConfig, ast);
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

const text = sourceCode.text;
text
string
The full source code text.

ast

const ast = sourceCode.ast;
ast
ASTNode
The Program node of the Abstract Syntax Tree.

lines

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

hasBOM

const hasBOM = sourceCode.hasBOM;
hasBOM
boolean
Whether the source code has a Unicode Byte Order Mark.

parserServices

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

scopeManager

const scopeManager = sourceCode.scopeManager;
scopeManager
ScopeManager
The scope manager containing scope analysis results.

visitorKeys

const visitorKeys = sourceCode.visitorKeys;
visitorKeys
object
Visitor keys for traversing the AST.

Methods

getText()

Gets the source code text for a node or token.
const text = sourceCode.getText(nodeOrToken, beforeCount, afterCount);
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.
const lines = sourceCode.getLines();
lines
string[]
Array of source code lines.

getAllComments()

Gets all comments in the source code.
const comments = sourceCode.getAllComments();
comments
Token[]
Array of comment tokens.

getComments()

Gets comments for a specific node.
const comments = sourceCode.getComments(node);
node
ASTNode
required
The node to get comments for.
comments
object

getLoc()

Gets the location information for a node or token.
const loc = sourceCode.getLoc(nodeOrToken);
nodeOrToken
ASTNode | Token
required
The node or token.
loc
object

getRange()

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

getTokens()

Gets tokens for a node.
const tokens = sourceCode.getTokens(node, beforeCount, afterCount);
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.
const token = sourceCode.getFirstToken(node, options);
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.
const token = sourceCode.getLastToken(node, options);
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.
const token = sourceCode.getTokenBefore(nodeOrToken, options);
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.
const token = sourceCode.getTokenAfter(nodeOrToken, options);
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.
const token = sourceCode.getFirstTokenBetween(left, right, options);
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.
const tokens = sourceCode.getTokensBetween(left, right, padding);
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.
const token = sourceCode.getTokenByRangeStart(index, options);
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.
const node = sourceCode.getNodeByRangeIndex(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.
const hasSpace = sourceCode.isSpaceBetween(first, second);
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.
const scope = sourceCode.getScope(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.
const found = sourceCode.markVariableAsUsed(name, node);
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.
const ancestors = sourceCode.getAncestors(node);
node
ASTNode
required
The node.
ancestors
ASTNode[]
Array of ancestor nodes from closest to farthest.

getDeclaredVariables()

Gets variables declared by a node.
const variables = sourceCode.getDeclaredVariables(node);
node
ASTNode
required
The node.
variables
Variable[]
Array of declared variables.

Examples

const { SourceCode } = require("eslint");

// Assuming you have an AST node
const text = sourceCode.getText(node);
console.log(text);

// Get text with surrounding context
const textWithContext = sourceCode.getText(node, 2, 2);
console.log(textWithContext);

Usage in Rules

The SourceCode object is available in rules through the context object:
module.exports = {
  create(context) {
    const sourceCode = context.sourceCode;

    return {
      Identifier(node) {
        const text = sourceCode.getText(node);
        const token = sourceCode.getFirstToken(node);
        
        // Your rule logic here
      }
    };
  }
};