Skip to content
Permalink
Browse files

Fix: sourceCode#isSpaceBetweenTokens() checks non-adjacent tokens (#1…

…2491)

* Fix: sourceCode#isSpaceBetweenTokens() checks non-adjacent tokens

* Incorporate feedback :)

* Handle args in reverse order and when args overlap

* Extract helper function
  • Loading branch information
kaicataldo committed Nov 7, 2019
1 parent 5868550 commit 9e29e189752f06362fd1956659e07834efb746a5
Showing with 366 additions and 33 deletions.
  1. +40 −9 lib/source-code/source-code.js
  2. +326 −24 tests/lib/source-code/source-code.js
@@ -78,6 +78,18 @@ function sortedMerge(tokens, comments) {
return result;
}

/**
* Determines if two nodes or tokens overlap.
* @param {ASTNode|Token} first The first node or token to check.
* @param {ASTNode|Token} second The second node or token to check.
* @returns {boolean} True if the two nodes or tokens overlap.
* @private
*/
function nodesOrTokensOverlap(first, second) {
return (first.range[0] <= second.range[0] && first.range[1] >= second.range[0]) ||
(second.range[0] <= first.range[0] && second.range[1] >= first.range[0]);
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
@@ -411,19 +423,38 @@ class SourceCode extends TokenStore {
}

/**
* Determines if two tokens have at least one whitespace character
* between them. This completely disregards comments in making the
* determination, so comments count as zero-length substrings.
* @param {Token} first The token to check after.
* @param {Token} second The token to check before.
* @returns {boolean} True if there is only space between tokens, false
* if there is anything other than whitespace between tokens.
* 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 {ASTNode|Token} first The first node or token to check between.
* @param {ASTNode|Token} 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.
* @public
*/
isSpaceBetweenTokens(first, second) {
const text = this.text.slice(first.range[1], second.range[0]);
if (nodesOrTokensOverlap(first, second)) {
return false;
}

const [startingNodeOrToken, endingNodeOrToken] = first.range[1] <= second.range[0]
? [first, second]
: [second, first];
const firstToken = this.getLastToken(startingNodeOrToken) || startingNodeOrToken;
const finalToken = this.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
let currentToken = firstToken;

while (currentToken !== finalToken) {
const nextToken = this.getTokenAfter(currentToken, { includeComments: true });

if (currentToken.range[1] !== nextToken.range[0]) {
return true;
}

currentToken = nextToken;
}

return /\s/u.test(text.replace(/\/\*.*?\*\//gus, ""));
return false;
}

/**

0 comments on commit 9e29e18

Please sign in to comment.
You can’t perform that action at this time.