|
8 | 8 | // Requirements |
9 | 9 | //------------------------------------------------------------------------------ |
10 | 10 |
|
11 | | -const { isCommentToken } = require("@eslint-community/eslint-utils"), |
12 | | - TokenStore = require("./token-store"), |
| 11 | +const TokenStore = require("./token-store"), |
13 | 12 | astUtils = require("../../../shared/ast-utils"), |
14 | 13 | Traverser = require("../../../shared/traverser"), |
15 | 14 | globals = require("../../../../conf/globals"), |
@@ -90,21 +89,6 @@ function getGlobalsForEcmaVersion(ecmaVersion) { |
90 | 89 | } |
91 | 90 | } |
92 | 91 |
|
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 | | - |
108 | 92 | /** |
109 | 93 | * Merges two sorted lists into a larger sorted list in O(n) time. |
110 | 94 | * @param {Token[]} tokens The list of tokens. |
@@ -179,56 +163,6 @@ function nodesOrTokensOverlap(first, second) { |
179 | 163 | ); |
180 | 164 | } |
181 | 165 |
|
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 | | - |
232 | 166 | /** |
233 | 167 | * Performs binary search to find the line number containing a given character index. |
234 | 168 | * Returns the lower bound - the index of the first element greater than the target. |
@@ -529,87 +463,6 @@ class SourceCode extends TokenStore { |
529 | 463 | return this.ast.comments; |
530 | 464 | } |
531 | 465 |
|
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 | | - |
613 | 466 | /** |
614 | 467 | * Gets the deepest node containing a range index. |
615 | 468 | * @param {number} index Range index of the desired node. |
@@ -649,24 +502,33 @@ class SourceCode extends TokenStore { |
649 | 502 | * @public |
650 | 503 | */ |
651 | 504 | isSpaceBetween(first, second) { |
652 | | - return isSpaceBetween(this, first, second, false); |
653 | | - } |
| 505 | + if (nodesOrTokensOverlap(first, second)) { |
| 506 | + return false; |
| 507 | + } |
654 | 508 |
|
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; |
670 | 532 | } |
671 | 533 |
|
672 | 534 | /** |
|
0 commit comments