-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Update: Add includeComments to getTokenByRangeStart (fixes #8068) #8069
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,25 @@ class CursorFactory { | |
} | ||
|
||
/** | ||
* Creates the cursor iterates tokens with normalized options. | ||
* Creates a base cursor instance that can be decorated by createCursor. | ||
* | ||
* @param {Token[]} tokens - The array of tokens. | ||
* @param {Comment[]} comments - The array of comments. | ||
* @param {Object} indexMap - The map from locations to indices in `tokens`. | ||
* @param {number} startLoc - The start location of the iteration range. | ||
* @param {number} endLoc - The end location of the iteration range. | ||
* @param {boolean} includeComments - The flag to iterate comments as well. | ||
* @returns {Cursor} The created base cursor. | ||
* @private | ||
*/ | ||
createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) { | ||
const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor; | ||
|
||
return new Cursor(tokens, comments, indexMap, startLoc, endLoc); | ||
} | ||
|
||
/** | ||
* Creates a cursor that iterates tokens with normalized options. | ||
* | ||
* @param {Token[]} tokens - The array of tokens. | ||
* @param {Comment[]} comments - The array of comments. | ||
|
@@ -52,8 +70,7 @@ class CursorFactory { | |
* @private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. |
||
*/ | ||
createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) { | ||
const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor; | ||
let cursor = new Cursor(tokens, comments, indexMap, startLoc, endLoc); | ||
let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments); | ||
|
||
if (filter) { | ||
cursor = new FilterCursor(cursor, filter); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,10 +236,20 @@ module.exports = class TokenStore { | |
/** | ||
* Gets the token starting at the specified index. | ||
* @param {number} offset - Index of the start of the token's range. | ||
* @param {Object} [options=0] - The option object. | ||
* @param {boolean} [options.includeComments=false] - The flag to iterate comments as well. | ||
* @returns {Token|null} The token starting at index, or null if no such token. | ||
*/ | ||
getTokenByRangeStart(offset) { | ||
const token = this.tokens[this.indexMap[offset]] || null; | ||
getTokenByRangeStart(offset, options) { | ||
const includeComments = options && options.includeComments; | ||
const token = cursors.forward.createBaseCursor( | ||
this.tokens, | ||
this.comments, | ||
this.indexMap, | ||
offset, | ||
-1, | ||
includeComments | ||
).getOneToken(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
const token = cursors.forward.createCursor(
this.tokens,
this.comments,
this.indexMap,
offset, // startLoc
-1, // endLoc
includeComments,
null, // filter
0, // skip
-1 // count
).getOneToken() Or, it's nice if we define const token = cursors.forward.createBaseCursor(
this.tokens,
this.comments,
this.indexMap,
offset, // startLoc
-1, // endLoc
includeComments
).getOneToken() |
||
|
||
if (token && token.range[0] === offset) { | ||
return token; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was curious if this is actually private since we call these methods in
token-store/index
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it's my mistake of copy/paste!