Skip to content

Commit 43f0ac9

Browse files
authored
docs: clarify parameter description in getText (#21287)
* docs: clarify parameter description in `getText` * wip * Update getText method description in custom-rules.md * wip * wip
1 parent a39d9fb commit 43f0ac9

4 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎docs/src/extend/custom-rules.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ module.exports = {
568568
569569
Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code:
570570
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)).
571+
- `getText(node, beforeCount, afterCount)`: Returns the source code for the given node, token, or comment, and optionally, a specified number of characters before and after its range. If `node` is omitted or `undefined`, the entire source text is returned (see the [dedicated section](#accessing-the-source-text)).
572572
- `getAllComments()`: Returns an array of all comments in the source (see the [dedicated section](#accessing-comments)).
573573
- `getCommentsBefore(nodeOrToken)`: Returns an array of comment tokens that occur directly before the given node or token (see the [dedicated section](#accessing-comments)).
574574
- `getCommentsAfter(nodeOrToken)`: Returns an array of comment tokens that occur directly after the given node or token (see the [dedicated section](#accessing-comments)).

‎docs/src/extend/languages.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Additionally, the following members are common on `SourceCode` objects and are r
6060
- `lines` - the individual lines of the source code as an array of strings.
6161
- `getParent(node)` - returns the parent of the given node or `undefined` if the node is the root.
6262
- `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, token, or comment, and optionally, a specified number of characters before and after its 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. If `node` is omitted or `undefined`, the entire source text is returned.
6464

6565
See [`JSONSourceCode`](https://github.com/eslint/json/blob/main/src/languages/json-source-code.js) as an example of a basic `SourceCode` class.
6666

‎lib/languages/js/source-code/source-code.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ class SourceCode extends TokenStore {
470470
/**
471471
* Gets the source code for the given node, token, or comment.
472472
* @param {ASTNode|Token|Comment} [node] The node, token, or comment to get the text for.
473+
* If `node` is omitted or `undefined`, the entire source text is returned.
473474
* @param {number} [beforeCount] The number of characters to retrieve before the node, token, or comment.
474475
* @param {number} [afterCount] The number of characters to retrieve after the node, token, or comment.
475476
* @returns {string} The text representing the node, token, or comment.

‎tests/lib/languages/js/source-code/source-code.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ describe("SourceCode", () => {
395395
assert.strictEqual(text, TEST_CODE);
396396
});
397397

398+
it("should retrieve all text when node is `undefined`", () => {
399+
const text = sourceCode.getText(void 0);
400+
401+
assert.strictEqual(text, TEST_CODE);
402+
});
403+
398404
it("should retrieve all text for root node", () => {
399405
const text = sourceCode.getText(ast);
400406

0 commit comments

Comments
 (0)