Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 40 additions & 9 deletions lib/source-code/source-code.js
Expand Up @@ -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
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
mysticatea marked this conversation as resolved.
Show resolved Hide resolved
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;
}

/**
Expand Down