Ajout de promotion et de commande
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const SourceCode = require("./source-code");
|
||||
|
||||
module.exports = {
|
||||
SourceCode,
|
||||
};
|
||||
+1364
File diff suppressed because it is too large
Load Diff
Generated
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which iterates tokens and comments in reverse.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const Cursor = require("./cursor");
|
||||
const utils = require("./utils");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The cursor which iterates tokens and comments in reverse.
|
||||
*/
|
||||
module.exports = class BackwardTokenCommentCursor extends Cursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @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.
|
||||
*/
|
||||
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
||||
super();
|
||||
this.tokens = tokens;
|
||||
this.comments = comments;
|
||||
this.tokenIndex = utils.getLastIndex(tokens, indexMap, endLoc);
|
||||
this.commentIndex = utils.search(comments, endLoc) - 1;
|
||||
this.border = startLoc;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
const token =
|
||||
this.tokenIndex >= 0 ? this.tokens[this.tokenIndex] : null;
|
||||
const comment =
|
||||
this.commentIndex >= 0 ? this.comments[this.commentIndex] : null;
|
||||
|
||||
if (token && (!comment || token.range[1] > comment.range[1])) {
|
||||
this.current = token;
|
||||
this.tokenIndex -= 1;
|
||||
} else if (comment) {
|
||||
this.current = comment;
|
||||
this.commentIndex -= 1;
|
||||
} else {
|
||||
this.current = null;
|
||||
}
|
||||
|
||||
return (
|
||||
Boolean(this.current) &&
|
||||
(this.border === -1 || this.current.range[0] >= this.border)
|
||||
);
|
||||
}
|
||||
};
|
||||
Generated
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which iterates tokens only in reverse.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const Cursor = require("./cursor");
|
||||
const { getLastIndex, getFirstIndex } = require("./utils");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The cursor which iterates tokens only in reverse.
|
||||
*/
|
||||
module.exports = class BackwardTokenCursor extends Cursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @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.
|
||||
*/
|
||||
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
||||
super();
|
||||
this.tokens = tokens;
|
||||
this.index = getLastIndex(tokens, indexMap, endLoc);
|
||||
this.indexEnd = getFirstIndex(tokens, indexMap, startLoc);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
if (this.index >= this.indexEnd) {
|
||||
this.current = this.tokens[this.index];
|
||||
this.index -= 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Shorthand for performance.
|
||||
*
|
||||
*/
|
||||
|
||||
/** @inheritdoc */
|
||||
getOneToken() {
|
||||
return this.index >= this.indexEnd ? this.tokens[this.index] : null;
|
||||
}
|
||||
};
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @fileoverview Define the abstract class about cursors which iterate tokens.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The abstract class about cursors which iterate tokens.
|
||||
*
|
||||
* This class has 2 abstract methods.
|
||||
*
|
||||
* - `current: Token | Comment | null` ... The current token.
|
||||
* - `moveNext(): boolean` ... Moves this cursor to the next token. If the next token didn't exist, it returns `false`.
|
||||
*
|
||||
* This is similar to ES2015 Iterators.
|
||||
* However, Iterators were slow (at 2017-01), so I created this class as similar to C# IEnumerable.
|
||||
*
|
||||
* There are the following known sub classes.
|
||||
*
|
||||
* - ForwardTokenCursor .......... The cursor which iterates tokens only.
|
||||
* - BackwardTokenCursor ......... The cursor which iterates tokens only in reverse.
|
||||
* - ForwardTokenCommentCursor ... The cursor which iterates tokens and comments.
|
||||
* - BackwardTokenCommentCursor .. The cursor which iterates tokens and comments in reverse.
|
||||
* - DecorativeCursor
|
||||
* - FilterCursor ............ The cursor which ignores the specified tokens.
|
||||
* - SkipCursor .............. The cursor which ignores the first few tokens.
|
||||
* - LimitCursor ............. The cursor which limits the count of tokens.
|
||||
*
|
||||
*/
|
||||
module.exports = class Cursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
*/
|
||||
constructor() {
|
||||
this.current = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first token.
|
||||
* This consumes this cursor.
|
||||
* @returns {Token|Comment} The first token or null.
|
||||
*/
|
||||
getOneToken() {
|
||||
return this.moveNext() ? this.current : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first tokens.
|
||||
* This consumes this cursor.
|
||||
* @returns {(Token|Comment)[]} All tokens.
|
||||
*/
|
||||
getAllTokens() {
|
||||
const tokens = [];
|
||||
|
||||
while (this.moveNext()) {
|
||||
tokens.push(this.current);
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves this cursor to the next token.
|
||||
* @returns {boolean} `true` if the next token exists.
|
||||
* @abstract
|
||||
*/
|
||||
/* c8 ignore next */
|
||||
// eslint-disable-next-line class-methods-use-this -- Unused
|
||||
moveNext() {
|
||||
throw new Error("Not implemented.");
|
||||
}
|
||||
};
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* @fileoverview Define 2 token factories; forward and backward.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const BackwardTokenCommentCursor = require("./backward-token-comment-cursor");
|
||||
const BackwardTokenCursor = require("./backward-token-cursor");
|
||||
const FilterCursor = require("./filter-cursor");
|
||||
const ForwardTokenCommentCursor = require("./forward-token-comment-cursor");
|
||||
const ForwardTokenCursor = require("./forward-token-cursor");
|
||||
const LimitCursor = require("./limit-cursor");
|
||||
const SkipCursor = require("./skip-cursor");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Helpers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The cursor factory.
|
||||
* @private
|
||||
*/
|
||||
class CursorFactory {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @param {Function} TokenCursor The class of the cursor which iterates tokens only.
|
||||
* @param {Function} TokenCommentCursor The class of the cursor which iterates the mix of tokens and comments.
|
||||
*/
|
||||
constructor(TokenCursor, TokenCommentCursor) {
|
||||
this.TokenCursor = TokenCursor;
|
||||
this.TokenCommentCursor = TokenCommentCursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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.
|
||||
* @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.
|
||||
* @param {Function|null} filter The predicate function to choose tokens.
|
||||
* @param {number} skip The count of tokens the cursor skips.
|
||||
* @param {number} count The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility.
|
||||
* @returns {Cursor} The created cursor.
|
||||
*/
|
||||
createCursor(
|
||||
tokens,
|
||||
comments,
|
||||
indexMap,
|
||||
startLoc,
|
||||
endLoc,
|
||||
includeComments,
|
||||
filter,
|
||||
skip,
|
||||
count,
|
||||
) {
|
||||
let cursor = this.createBaseCursor(
|
||||
tokens,
|
||||
comments,
|
||||
indexMap,
|
||||
startLoc,
|
||||
endLoc,
|
||||
includeComments,
|
||||
);
|
||||
|
||||
if (filter) {
|
||||
cursor = new FilterCursor(cursor, filter);
|
||||
}
|
||||
if (skip >= 1) {
|
||||
cursor = new SkipCursor(cursor, skip);
|
||||
}
|
||||
if (count >= 0) {
|
||||
cursor = new LimitCursor(cursor, count);
|
||||
}
|
||||
|
||||
return cursor;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
module.exports = {
|
||||
forward: new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor),
|
||||
backward: new CursorFactory(
|
||||
BackwardTokenCursor,
|
||||
BackwardTokenCommentCursor,
|
||||
),
|
||||
};
|
||||
Generated
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @fileoverview Define the abstract class about cursors which manipulate another cursor.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const Cursor = require("./cursor");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The abstract class about cursors which manipulate another cursor.
|
||||
*/
|
||||
module.exports = class DecorativeCursor extends Cursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @param {Cursor} cursor The cursor to be decorated.
|
||||
*/
|
||||
constructor(cursor) {
|
||||
super();
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
const retv = this.cursor.moveNext();
|
||||
|
||||
this.current = this.cursor.current;
|
||||
|
||||
return retv;
|
||||
}
|
||||
};
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which ignores specified tokens.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const DecorativeCursor = require("./decorative-cursor");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The decorative cursor which ignores specified tokens.
|
||||
*/
|
||||
module.exports = class FilterCursor extends DecorativeCursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @param {Cursor} cursor The cursor to be decorated.
|
||||
* @param {Function} predicate The predicate function to decide tokens this cursor iterates.
|
||||
*/
|
||||
constructor(cursor, predicate) {
|
||||
super(cursor);
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
const predicate = this.predicate;
|
||||
|
||||
while (super.moveNext()) {
|
||||
if (predicate(this.current)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Generated
Vendored
+65
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which iterates tokens and comments.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const Cursor = require("./cursor");
|
||||
const { getFirstIndex, search } = require("./utils");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The cursor which iterates tokens and comments.
|
||||
*/
|
||||
module.exports = class ForwardTokenCommentCursor extends Cursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @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.
|
||||
*/
|
||||
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
||||
super();
|
||||
this.tokens = tokens;
|
||||
this.comments = comments;
|
||||
this.tokenIndex = getFirstIndex(tokens, indexMap, startLoc);
|
||||
this.commentIndex = search(comments, startLoc);
|
||||
this.border = endLoc;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
const token =
|
||||
this.tokenIndex < this.tokens.length
|
||||
? this.tokens[this.tokenIndex]
|
||||
: null;
|
||||
const comment =
|
||||
this.commentIndex < this.comments.length
|
||||
? this.comments[this.commentIndex]
|
||||
: null;
|
||||
|
||||
if (token && (!comment || token.range[0] < comment.range[0])) {
|
||||
this.current = token;
|
||||
this.tokenIndex += 1;
|
||||
} else if (comment) {
|
||||
this.current = comment;
|
||||
this.commentIndex += 1;
|
||||
} else {
|
||||
this.current = null;
|
||||
}
|
||||
|
||||
return (
|
||||
Boolean(this.current) &&
|
||||
(this.border === -1 || this.current.range[1] <= this.border)
|
||||
);
|
||||
}
|
||||
};
|
||||
Generated
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which iterates tokens only.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const Cursor = require("./cursor");
|
||||
const { getFirstIndex, getLastIndex } = require("./utils");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The cursor which iterates tokens only.
|
||||
*/
|
||||
module.exports = class ForwardTokenCursor extends Cursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @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.
|
||||
*/
|
||||
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
||||
super();
|
||||
this.tokens = tokens;
|
||||
this.index = getFirstIndex(tokens, indexMap, startLoc);
|
||||
this.indexEnd = getLastIndex(tokens, indexMap, endLoc);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
if (this.index <= this.indexEnd) {
|
||||
this.current = this.tokens[this.index];
|
||||
this.index += 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Shorthand for performance.
|
||||
*
|
||||
*/
|
||||
|
||||
/** @inheritdoc */
|
||||
getOneToken() {
|
||||
return this.index <= this.indexEnd ? this.tokens[this.index] : null;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
getAllTokens() {
|
||||
return this.tokens.slice(this.index, this.indexEnd + 1);
|
||||
}
|
||||
};
|
||||
+721
File diff suppressed because it is too large
Load Diff
+39
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which limits the number of tokens.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const DecorativeCursor = require("./decorative-cursor");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The decorative cursor which limits the number of tokens.
|
||||
*/
|
||||
module.exports = class LimitCursor extends DecorativeCursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @param {Cursor} cursor The cursor to be decorated.
|
||||
* @param {number} count The count of tokens this cursor iterates.
|
||||
*/
|
||||
constructor(cursor, count) {
|
||||
super(cursor);
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
if (this.count > 0) {
|
||||
this.count -= 1;
|
||||
return super.moveNext();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Generated
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which iterates tokens only, with inflated range.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const ForwardTokenCursor = require("./forward-token-cursor");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The cursor which iterates tokens only, with inflated range.
|
||||
* This is for the backward compatibility of padding options.
|
||||
*/
|
||||
module.exports = class PaddedTokenCursor extends ForwardTokenCursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @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 {number} beforeCount The number of tokens this cursor iterates before start.
|
||||
* @param {number} afterCount The number of tokens this cursor iterates after end.
|
||||
*/
|
||||
constructor(
|
||||
tokens,
|
||||
comments,
|
||||
indexMap,
|
||||
startLoc,
|
||||
endLoc,
|
||||
beforeCount,
|
||||
afterCount,
|
||||
) {
|
||||
super(tokens, comments, indexMap, startLoc, endLoc);
|
||||
this.index = Math.max(0, this.index - beforeCount);
|
||||
this.indexEnd = Math.min(tokens.length - 1, this.indexEnd + afterCount);
|
||||
}
|
||||
};
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @fileoverview Define the cursor which ignores the first few tokens.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const DecorativeCursor = require("./decorative-cursor");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The decorative cursor which ignores the first few tokens.
|
||||
*/
|
||||
module.exports = class SkipCursor extends DecorativeCursor {
|
||||
/**
|
||||
* Initializes this cursor.
|
||||
* @param {Cursor} cursor The cursor to be decorated.
|
||||
* @param {number} count The count of tokens this cursor skips.
|
||||
*/
|
||||
constructor(cursor, count) {
|
||||
super(cursor);
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
moveNext() {
|
||||
while (this.count > 0) {
|
||||
this.count -= 1;
|
||||
if (!super.moveNext()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return super.moveNext();
|
||||
}
|
||||
};
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @fileoverview Define utility functions for token store.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exports
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Finds the index of the first token which is after the given location.
|
||||
* If it was not found, this returns `tokens.length`.
|
||||
* @param {(Token|Comment)[]} tokens It searches the token in this list.
|
||||
* @param {number} location The location to search.
|
||||
* @returns {number} The found index or `tokens.length`.
|
||||
*/
|
||||
exports.search = function search(tokens, location) {
|
||||
for (
|
||||
let minIndex = 0, maxIndex = tokens.length - 1;
|
||||
minIndex <= maxIndex;
|
||||
|
||||
) {
|
||||
/*
|
||||
* Calculate the index in the middle between minIndex and maxIndex.
|
||||
* `| 0` is used to round a fractional value down to the nearest integer: this is similar to
|
||||
* using `Math.trunc()` or `Math.floor()`, but performance tests have shown this method to
|
||||
* be faster.
|
||||
*/
|
||||
const index = ((minIndex + maxIndex) / 2) | 0;
|
||||
const token = tokens[index];
|
||||
const tokenStartLocation = token.range[0];
|
||||
|
||||
if (location <= tokenStartLocation) {
|
||||
if (index === minIndex) {
|
||||
return index;
|
||||
}
|
||||
maxIndex = index;
|
||||
} else {
|
||||
minIndex = index + 1;
|
||||
}
|
||||
}
|
||||
return tokens.length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the index of the `startLoc` in `tokens`.
|
||||
* `startLoc` can be the value of `node.range[1]`, so this checks about `startLoc - 1` as well.
|
||||
* @param {(Token|Comment)[]} tokens The tokens to find an index.
|
||||
* @param {Object} indexMap The map from locations to indices.
|
||||
* @param {number} startLoc The location to get an index.
|
||||
* @returns {number} The index.
|
||||
*/
|
||||
exports.getFirstIndex = function getFirstIndex(tokens, indexMap, startLoc) {
|
||||
if (startLoc in indexMap) {
|
||||
return indexMap[startLoc];
|
||||
}
|
||||
if (startLoc - 1 in indexMap) {
|
||||
const index = indexMap[startLoc - 1];
|
||||
const token = tokens[index];
|
||||
|
||||
// If the mapped index is out of bounds, the returned cursor index will point after the end of the tokens array.
|
||||
if (!token) {
|
||||
return tokens.length;
|
||||
}
|
||||
|
||||
/*
|
||||
* For the map of "comment's location -> token's index", it points the next token of a comment.
|
||||
* In that case, +1 is unnecessary.
|
||||
*/
|
||||
if (token.range[0] >= startLoc) {
|
||||
return index;
|
||||
}
|
||||
return index + 1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the index of the `endLoc` in `tokens`.
|
||||
* The information of end locations are recorded at `endLoc - 1` in `indexMap`, so this checks about `endLoc - 1` as well.
|
||||
* @param {(Token|Comment)[]} tokens The tokens to find an index.
|
||||
* @param {Object} indexMap The map from locations to indices.
|
||||
* @param {number} endLoc The location to get an index.
|
||||
* @returns {number} The index.
|
||||
*/
|
||||
exports.getLastIndex = function getLastIndex(tokens, indexMap, endLoc) {
|
||||
if (endLoc in indexMap) {
|
||||
return indexMap[endLoc] - 1;
|
||||
}
|
||||
if (endLoc - 1 in indexMap) {
|
||||
const index = indexMap[endLoc - 1];
|
||||
const token = tokens[index];
|
||||
|
||||
// If the mapped index is out of bounds, the returned cursor index will point before the end of the tokens array.
|
||||
if (!token) {
|
||||
return tokens.length - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* For the map of "comment's location -> token's index", it points the next token of a comment.
|
||||
* In that case, -1 is necessary.
|
||||
*/
|
||||
if (token.range[1] > endLoc) {
|
||||
return index - 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
return tokens.length - 1;
|
||||
};
|
||||
Reference in New Issue
Block a user