You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4ec5168
Browse filesBrowse the repository at this point in the historyBrowse files
feat: allow SourceCode#getText() to accept tokens and comments (#21340)
* feat: allow SourceCode#getText() to accept tokens and comments
Fixes#21329
* refactor: remove redundant type declarations
* docs: update descriptions of getLoc() and getRange()
* docs: update JSDoc of getLoc() and getRange()
* fix: align SourceCode declaration with runtime class
Copy file name to clipboardExpand all lines: docs/src/extend/custom-rules.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -568,7 +568,7 @@ module.exports = {
568
568
569
569
Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code:
570
570
571
-
- `getText(node)`: Returns the source code for the given node. Omit `node` to get the whole source (see the [dedicated section](#accessing-the-source-text)).
571
+
- `getText(node)`: Returns the source code for the given node, token, or comment. Omit the argument to get the whole source (see the [dedicated section](#accessing-the-source-text)).
572
572
- `getAllComments()`: Returns an array of all comments in the source (see the [dedicated section](#accessing-comments)).
573
573
- `getCommentsBefore(nodeOrToken)`: Returns an array of comment tokens that occur directly before the given node or token (see the [dedicated section](#accessing-comments)).
574
574
- `getCommentsAfter(nodeOrToken)`: Returns an array of comment tokens that occur directly after the given node or token (see the [dedicated section](#accessing-comments)).
@@ -647,6 +647,12 @@ var source = sourceCode.getText();
647
647
// get source for just this AST node
648
648
var nodeSource =sourceCode.getText(node);
649
649
650
+
// get source for just this token
651
+
var tokenSource =sourceCode.getText(token);
652
+
653
+
// get source for just this comment
654
+
var commentSource =sourceCode.getText(comment);
655
+
650
656
// get source for AST node plus previous two characters
651
657
var nodeSourceWithPrev =sourceCode.getText(node, 2);
Copy file name to clipboardExpand all lines: docs/src/extend/languages.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,8 +42,8 @@ A basic `SourceCode` object must implement the following:
42
42
43
43
-`ast` - a property containing the AST or CST for the source code.
44
44
-`text` - the text of the source code.
45
-
-`getLoc(nodeOrToken)` - a method that returns the location of a given nodeor token. This must match the `loc` structure that ESTree uses.
46
-
-`getRange(nodeOrToken)` - a method that returns the range of a given nodeor token. This must return an array where the first item is the start index and the second is the end index.
45
+
-`getLoc(nodeOrToken)` - a method that returns the location of a given node, token, or comment. This must match the `loc` structure that ESTree uses.
46
+
-`getRange(nodeOrToken)` - a method that returns the range of a given node, token, or comment. This must return an array where the first item is the start index and the second is the end index.
47
47
-`traverse()` - a method that returns an iterable for traversing the AST or CST. The iterator must return objects that implement either `VisitTraversalStep` or `CallTraversalStep` from `@eslint/core`.
48
48
49
49
The following optional members allow you to customize how ESLint interacts with the object:
@@ -60,7 +60,7 @@ Additionally, the following members are common on `SourceCode` objects and are r
60
60
-`lines` - the individual lines of the source code as an array of strings.
61
61
-`getParent(node)` - returns the parent of the given node or `undefined` if the node is the root.
62
62
-`getAncestors(node)` - returns an array of the ancestry of the node with the first item as the root of the tree and each subsequent item as the descendants of the root that lead to `node`.
63
-
-`getText(node, beforeCount, afterCount)` - returns the string that represents the given node, and optionally, a specified number of characters before and after the node's range.
63
+
-`getText(node, beforeCount, afterCount)` - returns the string that represents the given node, token, or comment, and optionally, a specified number of characters before and after its range.
64
64
65
65
See [`JSONSourceCode`](https://github.com/eslint/json/blob/main/src/languages/json-source-code.js) as an example of a basic `SourceCode` class.
0 commit comments