📄 SourceCode¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 35 |
| 🧱 Classes | 3 |
| 📦 Imports | 5 |
| 📐 Interfaces | 2 |
| 📑 Type Aliases | 7 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/utils/src/ts-eslint/SourceCode.ts
📦 Imports¶
| Name | Source |
|---|---|
ESLintSourceCode |
eslint |
ParserServices |
../ts-estree |
TSESTree |
../ts-estree |
Parser |
./Parser |
Scope |
./Scope |
Functions¶
TokenStore.commentsExistBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token): boolean¶
Checks whether any comments exist or not between the given 2 nodes.
Parameters:
leftany: The node to check.rightany: The node to check.
Returns: undefined
true if one or more comments exist.
Raw JSDoc
Code
TokenStore.getCommentsAfter(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[]¶
Gets all comment tokens directly after the given node or token.
Parameters:
nodeOrTokenany: The AST node or token to check for adjacent comment tokens.
Returns: undefined
An array of comments in occurrence order.
Raw JSDoc
TokenStore.getCommentsBefore(nodeOrToken: TSESTree.Node | TSESTree.Token): TSESTree.Comment[]¶
Gets all comment tokens directly before the given node or token.
Parameters:
nodeOrTokenany: The AST node or token to check for adjacent comment tokens.
Returns: undefined
An array of comments in occurrence order.
Raw JSDoc
TokenStore.getCommentsInside(node: TSESTree.Node): TSESTree.Comment[]¶
Gets all comment tokens inside the given node.
Parameters:
nodeany: The AST node to get the comments for.
Returns: undefined
An array of comments in occurrence order.
Raw JSDoc
TokenStore.getFirstToken(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the first token of the given node.
Parameters:
nodeany: The AST node.optionsany: The option object. If this is a number then it'soptions.skip. If this is a function then it'soptions.filter.
Returns: undefined
An object representing the token.
Raw JSDoc
Code
TokenStore.getFirstTokenBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the first token between two non-overlapping nodes.
Parameters:
leftany: Node before the desired token range.rightany: Node after the desired token range.optionsany: The option object. If this is a number then it'soptions.skip. If this is a function then it'soptions.filter.
Returns: undefined
An object representing the token.
Raw JSDoc
/**
* Gets the first token between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param options The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
* @returns An object representing the token.
*/
Code
TokenStore.getFirstTokens(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets the first count tokens of the given node.
Parameters:
nodeany: The AST node.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Raw JSDoc
Code
TokenStore.getFirstTokensBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets the first count tokens between two non-overlapping nodes.
Parameters:
leftany: Node before the desired token range.rightany: Node after the desired token range.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Returns: undefined
Tokens between left and right.
Raw JSDoc
/**
* Gets the first `count` tokens between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
* @returns Tokens between left and right.
*/
Code
TokenStore.getLastToken(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the last token of the given node.
Parameters:
nodeany: The AST node.optionsany: The option object. If this is a number then it'soptions.skip. If this is a function then it'soptions.filter.
Returns: undefined
An object representing the token.
Raw JSDoc
Code
TokenStore.getLastTokenBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the last token between two non-overlapping nodes.
Parameters:
leftany: Node before the desired token range.rightany: Node after the desired token range.optionsany: The option object. If this is a number then it'soptions.skip. If this is a function then it'soptions.filter.
Returns: undefined
An object representing the token.
Raw JSDoc
/**
* Gets the last token between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param options The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
* @returns An object representing the token.
*/
Code
TokenStore.getLastTokens(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets the last count tokens of the given node.
Parameters:
nodeany: The AST node.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Raw JSDoc
Code
TokenStore.getLastTokensBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets the last count tokens between two non-overlapping nodes.
Parameters:
leftany: Node before the desired token range.rightany: Node after the desired token range.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Returns: undefined
Tokens between left and right.
Raw JSDoc
/**
* Gets the last `count` tokens between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
* @returns Tokens between left and right.
*/
Code
TokenStore.getTokenAfter(node: TSESTree.Node | TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the token that follows a given node or token.
Parameters:
nodeany: The AST node or token.optionsany: The option object. If this is a number then it'soptions.skip. If this is a function then it'soptions.filter.
Returns: undefined
An object representing the token.
Raw JSDoc
Code
TokenStore.getTokenBefore(node: TSESTree.Node | TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the token that precedes a given node or token.
Parameters:
nodeany: The AST node or token.optionsany: The option object
Returns: undefined
An object representing the token.
Raw JSDoc
Code
TokenStore.getTokenByRangeStart(offset: number, options: T): SourceCode.ReturnTypeFromOptions<T> | null¶
Gets the token starting at the specified index.
Parameters:
offsetany: Index of the start of the token's range.optionsany: The option object. If this is a number then it'soptions.skip. If this is a function then it'soptions.filter.
Returns: undefined
The token starting at index, or null if no such token.
Raw JSDoc
/**
* Gets the token starting at the specified index.
* @param offset Index of the start of the token's range.
* @param options The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`.
* @returns The token starting at index, or null if no such token.
*/
Code
TokenStore.getTokens(node: TSESTree.Node, beforeCount: number, afterCount: number): TSESTree.Token[]¶
Gets all tokens that are related to the given node.
Parameters:
nodeany: The AST node.beforeCountany: The number of tokens before the node to retrieve.afterCountany: The number of tokens after the node to retrieve.
Returns: undefined
Array of objects representing tokens.
Raw JSDoc
Code
TokenStore.getTokensAfter(node: TSESTree.Node | TSESTree.Token, options: number | T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets the count tokens that follows a given node or token.
Parameters:
nodeany: The AST node.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Raw JSDoc
Code
TokenStore.getTokensBefore(node: TSESTree.Node | TSESTree.Token, options: number | T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets the count tokens that precedes a given node or token.
Parameters:
nodeany: The AST node.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Raw JSDoc
Code
TokenStore.getTokensBetween(left: TSESTree.Node | TSESTree.Token, right: TSESTree.Node | TSESTree.Token, options: number | T): SourceCode.ReturnTypeFromOptions<T>[]¶
Gets all of the tokens between two non-overlapping nodes.
Parameters:
leftany: Node before the desired token range.rightany: Node after the desired token range.optionsany: The option object. If this is a number then it'soptions.count. If this is a function then it'soptions.filter.
Returns: undefined
Tokens between left and right.
Raw JSDoc
/**
* Gets all of the tokens between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
* @returns Tokens between left and right.
*/
Code
SourceCodeBase.applyInlineConfig(): void¶
Returns: void
SourceCodeBase.applyLanguageOptions(): void¶
Returns: void
SourceCodeBase.finalize(): void¶
Returns: void
SourceCodeBase.getAllComments(): TSESTree.Comment[]¶
Retrieves an array containing all comments in the source code.
Returns: undefined
An array of comment nodes.
Raw JSDoc
SourceCodeBase.getIndexFromLoc(location: TSESTree.Position): number¶
Converts a (line, column) pair into a range index.
Parameters:
locationany: A line/column location
Returns: undefined
The range index of the location in the file.
Raw JSDoc
SourceCodeBase.getLines(): string[]¶
Gets the entire source text split into an array of lines.
Returns: undefined
The source text as an array of lines.
Raw JSDoc
SourceCodeBase.getLocFromIndex(index: number): TSESTree.Position¶
Converts a source text index into a (line, column) pair.
Parameters:
indexany: The index of a character in a file
Returns: undefined
A {line, column} location object with a 0-indexed column
Raw JSDoc
SourceCodeBase.getNodeByRangeIndex(index: number): TSESTree.Node | null¶
Gets the deepest node containing a range index.
Parameters:
indexany: Range index of the desired node.
Returns: undefined
The node if found or null if not found.
Raw JSDoc
SourceCodeBase.getText(node: TSESTree.Node | TSESTree.Token, beforeCount: number, afterCount: number): string¶
Gets the source code for the given node.
Parameters:
nodeany: The AST node to get the text for.beforeCountany: The number of characters before the node to retrieve.afterCountany: The number of characters after the node to retrieve.
Returns: undefined
The text representing the AST node.
Raw JSDoc
Code
SourceCodeBase.isSpaceBetween(first: TSESTree.Node | TSESTree.Token, second: TSESTree.Node | TSESTree.Token): boolean¶
Determines if two nodes or tokens have at least one whitespace character between them. Order does not matter. Returns false if the given nodes or tokens overlap.
Parameters:
firstany: The first node or token to check between.secondany: The second node or token to check between.
Returns: undefined
True if there is a whitespace character between any of the tokens found between the two given nodes or tokens.
Raw JSDoc
/**
* Determines if two nodes or tokens have at least one whitespace character
* between them. Order does not matter. Returns false if the given nodes or
* tokens overlap.
* @param first The first node or token to check between.
* @param second The second node or token to check between.
* @returns True if there is a whitespace character between any of the tokens found between the two given nodes or tokens.
*/
Code
SourceCodeBase.isSpaceBetweenTokens(first: TSESTree.Token, second: TSESTree.Token): boolean¶
Determines if two nodes or tokens have at least one whitespace character
between them. Order does not matter. Returns false if the given nodes or
tokens overlap.
For backward compatibility, this method returns true if there are
JSXText tokens that contain whitespace between the two.
⚠️ DEPRECATED: in favor of isSpaceBetween
Parameters:
firstany: The first node or token to check between.secondany: The second node or token to check between.
Returns: boolean
True if there is a whitespace character between
any of the tokens found between the two given nodes or tokens.
Raw JSDoc
/**
* Determines if two nodes or tokens have at least one whitespace character
* between them. Order does not matter. Returns false if the given nodes or
* tokens overlap.
* For backward compatibility, this method returns true if there are
* `JSXText` tokens that contain whitespace between the two.
* @param first The first node or token to check between.
* @param second The second node or token to check between.
* @returns {boolean} True if there is a whitespace character between
* any of the tokens found between the two given nodes or tokens.
* @deprecated in favor of isSpaceBetween
*/
SourceCodeBase.getScope(node: TSESTree.Node): Scope.Scope¶
Returns the scope of the given node. This information can be used track references to variables.
Raw JSDoc
SourceCodeBase.getAncestors(node: TSESTree.Node): TSESTree.Node[]¶
Returns an array of the ancestors of the given node, starting at the root of the AST and continuing through the direct parent of the current node. This array does not include the currently-traversed node itself.
Raw JSDoc
SourceCodeBase.getDeclaredVariables(node: TSESTree.Node): readonly Scope.Variable[]¶
Returns a list of variables declared by the given node. This information can be used to track references to variables.
Raw JSDoc
SourceCodeBase.markVariableAsUsed(name: string, node: TSESTree.Node): boolean¶
Marks a variable with the given name in the current scope as used. This affects the no-unused-vars rule.
Raw JSDoc
SourceCodeBase.splitLines(text: string): string[]¶
Split the source code into multiple lines based on the line delimiters.
Parameters:
textany: Source code as a string.
Returns: undefined
Array of source code lines.
Raw JSDoc
Classes¶
TokenStore¶
Methods (20) — full entries under Functions
| Method | Signature |
|---|---|
commentsExistBetween |
(left: TSESTree.Node \| TSESTree.Token, right: TSESTree.Node \| TSESTree.Token): boolean |
getCommentsAfter |
(nodeOrToken: TSESTree.Node \| TSESTree.Token): TSESTree.Comment[] |
getCommentsBefore |
(nodeOrToken: TSESTree.Node \| TSESTree.Token): TSESTree.Comment[] |
getCommentsInside |
(node: TSESTree.Node): TSESTree.Comment[] |
getFirstToken |
(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T> \| null |
getFirstTokenBetween |
(left: TSESTree.Node \| TSESTree.Token, right: TSESTree.Node \| TSESTree.Token, options: T): Sour... |
getFirstTokens |
(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[] |
getFirstTokensBetween |
(left: TSESTree.Node \| TSESTree.Token, right: TSESTree.Node \| TSESTree.Token, options: T): Sour... |
getLastToken |
(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T> \| null |
getLastTokenBetween |
(left: TSESTree.Node \| TSESTree.Token, right: TSESTree.Node \| TSESTree.Token, options: T): Sour... |
getLastTokens |
(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[] |
getLastTokensBetween |
(left: TSESTree.Node \| TSESTree.Token, right: TSESTree.Node \| TSESTree.Token, options: T): Sour... |
getTokenAfter |
(node: TSESTree.Node \| TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T> \| null |
getTokenBefore |
(node: TSESTree.Node \| TSESTree.Token, options: T): SourceCode.ReturnTypeFromOptions<T> \| null |
getTokenByRangeStart |
(offset: number, options: T): SourceCode.ReturnTypeFromOptions<T> \| null |
getTokens |
(node: TSESTree.Node, beforeCount: number, afterCount: number): TSESTree.Token[] |
getTokens |
(node: TSESTree.Node, options: T): SourceCode.ReturnTypeFromOptions<T>[] |
getTokensAfter |
(node: TSESTree.Node \| TSESTree.Token, options: number \| T): SourceCode.ReturnTypeFromOptions<T>[] |
getTokensBefore |
(node: TSESTree.Node \| TSESTree.Token, options: number \| T): SourceCode.ReturnTypeFromOptions<T>[] |
getTokensBetween |
(left: TSESTree.Node \| TSESTree.Token, right: TSESTree.Node \| TSESTree.Token, options: number \... |
SourceCodeBase¶
Extends: TokenStore
Methods (16) — full entries under Functions
| Method | Signature |
|---|---|
applyInlineConfig |
(): void |
applyLanguageOptions |
(): void |
finalize |
(): void |
getAllComments |
(): TSESTree.Comment[] |
getIndexFromLoc |
(location: TSESTree.Position): number |
getLines |
(): string[] |
getLocFromIndex |
(index: number): TSESTree.Position |
getNodeByRangeIndex |
(index: number): TSESTree.Node \| null |
getText |
(node: TSESTree.Node \| TSESTree.Token, beforeCount: number, afterCount: number): string |
isSpaceBetween |
(first: TSESTree.Node \| TSESTree.Token, second: TSESTree.Node \| TSESTree.Token): boolean |
isSpaceBetweenTokens |
(first: TSESTree.Token, second: TSESTree.Token): boolean |
getScope |
(node: TSESTree.Node): Scope.Scope |
getAncestors |
(node: TSESTree.Node): TSESTree.Node[] |
getDeclaredVariables |
(node: TSESTree.Node): readonly Scope.Variable[] |
markVariableAsUsed |
(name: string, node: TSESTree.Node): boolean |
splitLines |
(text: string): string[] |
SourceCode¶
Extends: (ESLintSourceCode as typeof SourceCodeBase)
Interfaces¶
Program¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
comments |
TSESTree.Comment[] |
✗ | not shown |
tokens |
TSESTree.Token[] |
✗ | not shown |
SourceCodeConfig¶
Interface Code
export interface SourceCodeConfig {
/**
* The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
*/
ast: Program;
/**
* The parser services.
*/
parserServices: ParserServices | null;
/**
* The scope of this source code.
*/
scopeManager: Scope.ScopeManager | null;
/**
* The source code text.
*/
text: string;
/**
* The visitor keys to traverse AST.
*/
visitorKeys: VisitorKeys | null;
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
ast |
Program |
✗ | not shown |
parserServices |
ParserServices \| null |
✗ | not shown |
scopeManager |
Scope.ScopeManager \| null |
✗ | not shown |
text |
string |
✗ | not shown |
visitorKeys |
VisitorKeys \| null |
✗ | not shown |
Type Aliases¶
VisitorKeys¶
FilterPredicate¶
GetFilterPredicate<Filter, Default>¶
type GetFilterPredicate<Filter, Default> = Filter extends ((
token: TSESTree.Token,
) => token is infer U extends TSESTree.Token)
? U
: Default;
GetFilterPredicateFromOptions<Options, Default>¶
type GetFilterPredicateFromOptions<Options, Default> = Options extends { filter?: FilterPredicate }
? GetFilterPredicate<Options['filter'], Default>
: GetFilterPredicate<Options, Default>;
ReturnTypeFromOptions<T>¶
type ReturnTypeFromOptions<T> = T extends { includeComments: true }
? GetFilterPredicateFromOptions<T, TSESTree.Token>
: GetFilterPredicateFromOptions<
T,
Exclude<TSESTree.Token, TSESTree.Comment>
>;
CursorWithSkipOptions¶
type CursorWithSkipOptions = | number
| FilterPredicate
| {
/**
* The predicate function to choose tokens.
*/
filter?: FilterPredicate;
/**
* The flag to iterate comments as well.
*/
includeComments?: boolean;
/**
* The count of tokens the cursor skips.
*/
skip?: number;
};
CursorWithCountOptions¶
type CursorWithCountOptions = | number
| FilterPredicate
| {
/**
* The maximum count of tokens the cursor iterates.
*/
count?: number;
/**
* The predicate function to choose tokens.
*/
filter?: FilterPredicate;
/**
* The flag to iterate comments as well.
*/
includeComments?: boolean;
};
Generated by Syntax Scribe