Skip to content

Commit 3383e7e

Browse files
authored
fix!: remove deprecated SourceCode methods (#20137)
* fix!: remove deprecated `SourceCode` methods * remove deprecated types * remove deprecated types * update migration guide * apply suggestions
1 parent 3b0289c commit 3383e7e

27 files changed

+355
-1895
lines changed

docs/src/extend/custom-rules.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -938,15 +938,6 @@ Here, the `myCustomVar` variable is marked as used relative to a `ReturnStatemen
938938

939939
ESLint analyzes code paths while traversing AST. You can access code path objects with seven events related to code paths. For more information, refer to [Code Path Analysis](code-path-analysis).
940940

941-
### Deprecated `SourceCode` Methods
942-
943-
Please note that the following `SourceCode` methods have been deprecated and will be removed in a future version of ESLint:
944-
945-
- `getTokenOrCommentBefore()`: Replaced by `SourceCode#getTokenBefore()` with the `{ includeComments: true }` option.
946-
- `getTokenOrCommentAfter()`: Replaced by `SourceCode#getTokenAfter()` with the `{ includeComments: true }` option.
947-
- `isSpaceBetweenTokens()`: Replaced by `SourceCode#isSpaceBetween()`
948-
- `getJSDocComment()`
949-
950941
## Rule Unit Tests
951942

952943
ESLint provides the [`RuleTester`](../integrate/nodejs-api#ruletester) utility to make it easy to write tests for rules.

docs/src/use/migrate-to-10.0.0.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The lists below are ordered roughly by the number of users each change is expect
3636
- [Fixer methods now require string `text` arguments](#fixer-text-must-be-string)
3737
- [New requirements for `ScopeManager` implementations](#scope-manager)
3838
- [Removal of deprecated `context` members](#rule-context)
39+
- [Removal of deprecated `SourceCode` methods](#sourcecode-methods-removed)
3940
- [Prohibiting `errors` or `output` of valid RuleTester test cases](#stricter-rule-tester)
4041
- [POSIX character classes in glob patterns](#posix-character-classes)
4142

@@ -305,6 +306,30 @@ The removed `context` properties must be done manually as there may not be a dir
305306

306307
**Related issue(s):** [eslint/eslint#16999](https://github.com/eslint/eslint/issues/16999)
307308

309+
## <a name="sourcecode-methods-removed"></a> Removal of deprecated `SourceCode` methods
310+
311+
The following deprecated `SourceCode` methods have been removed in ESLint v10.0.0:
312+
313+
- `getTokenOrCommentBefore()`
314+
- `getTokenOrCommentAfter()`
315+
- `isSpaceBetweenTokens()`
316+
- `getJSDocComment()`
317+
318+
These methods have been deprecated for multiple major versions and were primarily used by deprecated formatting rules and internal ESLint utilities. Custom rules using these methods must be updated to use their modern replacements.
319+
320+
**To address:** In your custom rules, make the following changes:
321+
322+
| **Removed on `SourceCode`** | **Replacement** |
323+
| -------------------------------------------- | -------------------------------------------------------------- |
324+
| `getTokenOrCommentBefore(nodeOrToken, skip)` | `getTokenBefore(nodeOrToken, { includeComments: true, skip })` |
325+
| `getTokenOrCommentAfter(nodeOrToken, skip)` | `getTokenAfter(nodeOrToken, { includeComments: true, skip })` |
326+
| `isSpaceBetweenTokens(first, second)` | `isSpaceBetween(first, second)` |
327+
| `getJSDocComment(node)` | No replacement |
328+
329+
Compatibility patches are available in the [`@eslint/compat`](https://www.npmjs.com/package/@eslint/compat) package to help with the transition.
330+
331+
**Related issue(s):** [#20113](https://github.com/eslint/eslint/issues/20113)
332+
308333
## <a name="stricter-rule-tester"></a> Prohibiting `errors` or `output` of valid RuleTester test cases
309334

310335
In ESLint v10.0.0, the RuleTester has become more strict about test case structure. Valid test cases (those that should not produce any linting errors) are no longer allowed to have `errors` or `output` properties.

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

Lines changed: 27 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// Requirements
99
//------------------------------------------------------------------------------
1010

11-
const { isCommentToken } = require("@eslint-community/eslint-utils"),
12-
TokenStore = require("./token-store"),
11+
const TokenStore = require("./token-store"),
1312
astUtils = require("../../../shared/ast-utils"),
1413
Traverser = require("../../../shared/traverser"),
1514
globals = require("../../../../conf/globals"),
@@ -90,21 +89,6 @@ function getGlobalsForEcmaVersion(ecmaVersion) {
9089
}
9190
}
9291

93-
/**
94-
* Check to see if its a ES6 export declaration.
95-
* @param {ASTNode} astNode An AST node.
96-
* @returns {boolean} whether the given node represents an export declaration.
97-
* @private
98-
*/
99-
function looksLikeExport(astNode) {
100-
return (
101-
astNode.type === "ExportDefaultDeclaration" ||
102-
astNode.type === "ExportNamedDeclaration" ||
103-
astNode.type === "ExportAllDeclaration" ||
104-
astNode.type === "ExportSpecifier"
105-
);
106-
}
107-
10892
/**
10993
* Merges two sorted lists into a larger sorted list in O(n) time.
11094
* @param {Token[]} tokens The list of tokens.
@@ -179,56 +163,6 @@ function nodesOrTokensOverlap(first, second) {
179163
);
180164
}
181165

182-
/**
183-
* Determines if two nodes or tokens have at least one whitespace character
184-
* between them. Order does not matter. Returns false if the given nodes or
185-
* tokens overlap.
186-
* @param {SourceCode} sourceCode The source code object.
187-
* @param {ASTNode|Token} first The first node or token to check between.
188-
* @param {ASTNode|Token} second The second node or token to check between.
189-
* @param {boolean} checkInsideOfJSXText If `true` is present, check inside of JSXText tokens for backward compatibility.
190-
* @returns {boolean} True if there is a whitespace character between
191-
* any of the tokens found between the two given nodes or tokens.
192-
* @public
193-
*/
194-
function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
195-
if (nodesOrTokensOverlap(first, second)) {
196-
return false;
197-
}
198-
199-
const [startingNodeOrToken, endingNodeOrToken] =
200-
first.range[1] <= second.range[0] ? [first, second] : [second, first];
201-
const firstToken =
202-
sourceCode.getLastToken(startingNodeOrToken) || startingNodeOrToken;
203-
const finalToken =
204-
sourceCode.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
205-
let currentToken = firstToken;
206-
207-
while (currentToken !== finalToken) {
208-
const nextToken = sourceCode.getTokenAfter(currentToken, {
209-
includeComments: true,
210-
});
211-
212-
if (
213-
currentToken.range[1] !== nextToken.range[0] ||
214-
/*
215-
* For backward compatibility, check spaces in JSXText.
216-
* https://github.com/eslint/eslint/issues/12614
217-
*/
218-
(checkInsideOfJSXText &&
219-
nextToken !== finalToken &&
220-
nextToken.type === "JSXText" &&
221-
/\s/u.test(nextToken.value))
222-
) {
223-
return true;
224-
}
225-
226-
currentToken = nextToken;
227-
}
228-
229-
return false;
230-
}
231-
232166
/**
233167
* Performs binary search to find the line number containing a given character index.
234168
* Returns the lower bound - the index of the first element greater than the target.
@@ -529,87 +463,6 @@ class SourceCode extends TokenStore {
529463
return this.ast.comments;
530464
}
531465

532-
/**
533-
* Retrieves the JSDoc comment for a given node.
534-
* @param {ASTNode} node The AST node to get the comment for.
535-
* @returns {Token|null} The Block comment token containing the JSDoc comment
536-
* for the given node or null if not found.
537-
* @public
538-
* @deprecated
539-
*/
540-
getJSDocComment(node) {
541-
/**
542-
* Checks for the presence of a JSDoc comment for the given node and returns it.
543-
* @param {ASTNode} astNode The AST node to get the comment for.
544-
* @returns {Token|null} The Block comment token containing the JSDoc comment
545-
* for the given node or null if not found.
546-
* @private
547-
*/
548-
const findJSDocComment = astNode => {
549-
const tokenBefore = this.getTokenBefore(astNode, {
550-
includeComments: true,
551-
});
552-
553-
if (
554-
tokenBefore &&
555-
isCommentToken(tokenBefore) &&
556-
tokenBefore.type === "Block" &&
557-
tokenBefore.value.charAt(0) === "*" &&
558-
astNode.loc.start.line - tokenBefore.loc.end.line <= 1
559-
) {
560-
return tokenBefore;
561-
}
562-
563-
return null;
564-
};
565-
let parent = node.parent;
566-
567-
switch (node.type) {
568-
case "ClassDeclaration":
569-
case "FunctionDeclaration":
570-
return findJSDocComment(
571-
looksLikeExport(parent) ? parent : node,
572-
);
573-
574-
case "ClassExpression":
575-
return findJSDocComment(parent.parent);
576-
577-
case "ArrowFunctionExpression":
578-
case "FunctionExpression":
579-
if (
580-
parent.type !== "CallExpression" &&
581-
parent.type !== "NewExpression"
582-
) {
583-
while (
584-
!this.getCommentsBefore(parent).length &&
585-
!/Function/u.test(parent.type) &&
586-
parent.type !== "MethodDefinition" &&
587-
parent.type !== "Property"
588-
) {
589-
parent = parent.parent;
590-
591-
if (!parent) {
592-
break;
593-
}
594-
}
595-
596-
if (
597-
parent &&
598-
parent.type !== "FunctionDeclaration" &&
599-
parent.type !== "Program"
600-
) {
601-
return findJSDocComment(parent);
602-
}
603-
}
604-
605-
return findJSDocComment(node);
606-
607-
// falls through
608-
default:
609-
return null;
610-
}
611-
}
612-
613466
/**
614467
* Gets the deepest node containing a range index.
615468
* @param {number} index Range index of the desired node.
@@ -649,24 +502,33 @@ class SourceCode extends TokenStore {
649502
* @public
650503
*/
651504
isSpaceBetween(first, second) {
652-
return isSpaceBetween(this, first, second, false);
653-
}
505+
if (nodesOrTokensOverlap(first, second)) {
506+
return false;
507+
}
654508

655-
/**
656-
* Determines if two nodes or tokens have at least one whitespace character
657-
* between them. Order does not matter. Returns false if the given nodes or
658-
* tokens overlap.
659-
* For backward compatibility, this method returns true if there are
660-
* `JSXText` tokens that contain whitespaces between the two.
661-
* @param {ASTNode|Token} first The first node or token to check between.
662-
* @param {ASTNode|Token} second The second node or token to check between.
663-
* @returns {boolean} True if there is a whitespace character between
664-
* any of the tokens found between the two given nodes or tokens.
665-
* @deprecated in favor of isSpaceBetween().
666-
* @public
667-
*/
668-
isSpaceBetweenTokens(first, second) {
669-
return isSpaceBetween(this, first, second, true);
509+
const [startingNodeOrToken, endingNodeOrToken] =
510+
first.range[1] <= second.range[0]
511+
? [first, second]
512+
: [second, first];
513+
const firstToken =
514+
this.getLastToken(startingNodeOrToken) || startingNodeOrToken;
515+
const finalToken =
516+
this.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
517+
let currentToken = firstToken;
518+
519+
while (currentToken !== finalToken) {
520+
const nextToken = this.getTokenAfter(currentToken, {
521+
includeComments: true,
522+
});
523+
524+
if (currentToken.range[1] !== nextToken.range[0]) {
525+
return true;
526+
}
527+
528+
currentToken = nextToken;
529+
}
530+
531+
return false;
670532
}
671533

672534
/**

lib/languages/js/source-code/token-store/index.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -444,32 +444,6 @@ module.exports = class TokenStore {
444444
).getOneToken();
445445
}
446446

447-
/**
448-
* Gets the token that precedes a given node or token in the token stream.
449-
* This is defined for backward compatibility. Use `includeComments` option instead.
450-
* TODO: We have a plan to remove this in a future major version.
451-
* @param {ASTNode|Token|Comment} node The AST node or token.
452-
* @param {number} [skip=0] A number of tokens to skip.
453-
* @returns {Token|null} An object representing the token.
454-
* @deprecated
455-
*/
456-
getTokenOrCommentBefore(node, skip) {
457-
return this.getTokenBefore(node, { includeComments: true, skip });
458-
}
459-
460-
/**
461-
* Gets the token that follows a given node or token in the token stream.
462-
* This is defined for backward compatibility. Use `includeComments` option instead.
463-
* TODO: We have a plan to remove this in a future major version.
464-
* @param {ASTNode|Token|Comment} node The AST node or token.
465-
* @param {number} [skip=0] A number of tokens to skip.
466-
* @returns {Token|null} An object representing the token.
467-
* @deprecated
468-
*/
469-
getTokenOrCommentAfter(node, skip) {
470-
return this.getTokenAfter(node, { includeComments: true, skip });
471-
}
472-
473447
//--------------------------------------------------------------------------
474448
// Gets multiple tokens.
475449
//--------------------------------------------------------------------------

lib/rules/array-bracket-spacing.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ module.exports = {
258258
if (astUtils.isTokenOnSameLine(first, second)) {
259259
if (
260260
openingBracketMustBeSpaced &&
261-
!sourceCode.isSpaceBetweenTokens(first, second)
261+
!sourceCode.isSpaceBetween(first, second)
262262
) {
263263
reportRequiredBeginningSpace(node, first);
264264
}
265265
if (
266266
!openingBracketMustBeSpaced &&
267-
sourceCode.isSpaceBetweenTokens(first, second)
267+
sourceCode.isSpaceBetween(first, second)
268268
) {
269269
reportNoBeginningSpace(node, first);
270270
}
@@ -276,13 +276,13 @@ module.exports = {
276276
) {
277277
if (
278278
closingBracketMustBeSpaced &&
279-
!sourceCode.isSpaceBetweenTokens(penultimate, last)
279+
!sourceCode.isSpaceBetween(penultimate, last)
280280
) {
281281
reportRequiredEndingSpace(node, last);
282282
}
283283
if (
284284
!closingBracketMustBeSpaced &&
285-
sourceCode.isSpaceBetweenTokens(penultimate, last)
285+
sourceCode.isSpaceBetween(penultimate, last)
286286
) {
287287
reportNoEndingSpace(node, last);
288288
}

lib/rules/block-spacing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module.exports = {
9595
function isValid(left, right) {
9696
return (
9797
!util.isTokenOnSameLine(left, right) ||
98-
sourceCode.isSpaceBetweenTokens(left, right) === always
98+
sourceCode.isSpaceBetween(left, right) === always
9999
);
100100
}
101101

lib/rules/comma-spacing.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ module.exports = {
178178
!commaTokensToIgnore.includes(token) &&
179179
astUtils.isTokenOnSameLine(previousToken, token) &&
180180
options.before !==
181-
sourceCode.isSpaceBetweenTokens(
182-
previousToken,
183-
token,
184-
)
181+
sourceCode.isSpaceBetween(previousToken, token)
185182
) {
186183
report(token, "before", previousToken);
187184
}
@@ -195,7 +192,7 @@ module.exports = {
195192
!(!options.after && nextToken.type === "Line") && // special case, allow space before line comment
196193
astUtils.isTokenOnSameLine(token, nextToken) &&
197194
options.after !==
198-
sourceCode.isSpaceBetweenTokens(token, nextToken)
195+
sourceCode.isSpaceBetween(token, nextToken)
199196
) {
200197
report(token, "after", nextToken);
201198
}

0 commit comments

Comments
 (0)