@@ -78,6 +78,18 @@ function sortedMerge(tokens, comments) {
7878 return result ;
7979}
8080
81+ /**
82+ * Determines if two nodes or tokens overlap.
83+ * @param {ASTNode|Token } first The first node or token to check.
84+ * @param {ASTNode|Token } second The second node or token to check.
85+ * @returns {boolean } True if the two nodes or tokens overlap.
86+ * @private
87+ */
88+ function nodesOrTokensOverlap ( first , second ) {
89+ return ( first . range [ 0 ] <= second . range [ 0 ] && first . range [ 1 ] >= second . range [ 0 ] ) ||
90+ ( second . range [ 0 ] <= first . range [ 0 ] && second . range [ 1 ] >= first . range [ 0 ] ) ;
91+ }
92+
8193//------------------------------------------------------------------------------
8294// Public Interface
8395//------------------------------------------------------------------------------
@@ -411,19 +423,38 @@ class SourceCode extends TokenStore {
411423 }
412424
413425 /**
414- * Determines if two tokens have at least one whitespace character
415- * between them. This completely disregards comments in making the
416- * determination, so comments count as zero-length substrings .
417- * @param {Token } first The token to check after .
418- * @param {Token } second The token to check before .
419- * @returns {boolean } True if there is only space between tokens, false
420- * if there is anything other than whitespace between tokens.
426+ * Determines if two nodes or tokens have at least one whitespace character
427+ * between them. Order does not matter. Returns false if the given nodes or
428+ * tokens overlap .
429+ * @param {ASTNode| Token } first The first node or token to check between .
430+ * @param {ASTNode| Token } second The second node or token to check between .
431+ * @returns {boolean } True if there is a whitespace character between
432+ * any of the tokens found between the two given nodes or tokens.
421433 * @public
422434 */
423435 isSpaceBetweenTokens ( first , second ) {
424- const text = this . text . slice ( first . range [ 1 ] , second . range [ 0 ] ) ;
436+ if ( nodesOrTokensOverlap ( first , second ) ) {
437+ return false ;
438+ }
439+
440+ const [ startingNodeOrToken , endingNodeOrToken ] = first . range [ 1 ] <= second . range [ 0 ]
441+ ? [ first , second ]
442+ : [ second , first ] ;
443+ const firstToken = this . getLastToken ( startingNodeOrToken ) || startingNodeOrToken ;
444+ const finalToken = this . getFirstToken ( endingNodeOrToken ) || endingNodeOrToken ;
445+ let currentToken = firstToken ;
446+
447+ while ( currentToken !== finalToken ) {
448+ const nextToken = this . getTokenAfter ( currentToken , { includeComments : true } ) ;
449+
450+ if ( currentToken . range [ 1 ] !== nextToken . range [ 0 ] ) {
451+ return true ;
452+ }
453+
454+ currentToken = nextToken ;
455+ }
425456
426- return / \s / u . test ( text . replace ( / \/ \* . * ? \* \/ / gus , "" ) ) ;
457+ return false ;
427458 }
428459
429460 /**
0 commit comments