eslint 3.15.0 → 3.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +79 -0
- package/conf/{eslint.json → eslint-recommended.js} +87 -71
- package/lib/ast-utils.js +182 -80
- package/lib/code-path-analysis/code-path-state.js +2 -2
- package/lib/config/autoconfig.js +3 -3
- package/lib/config/config-file.js +14 -7
- package/lib/config/config-initializer.js +1 -1
- package/lib/config.js +3 -2
- package/lib/eslint.js +4 -4
- package/lib/internal-rules/internal-no-invalid-meta.js +2 -40
- package/lib/rules/array-callback-return.js +15 -5
- package/lib/rules/arrow-body-style.js +7 -4
- package/lib/rules/arrow-spacing.js +7 -6
- package/lib/rules/block-spacing.js +2 -2
- package/lib/rules/brace-style.js +2 -6
- package/lib/rules/capitalized-comments.js +8 -7
- package/lib/rules/comma-spacing.js +3 -3
- package/lib/rules/complexity.js +14 -8
- package/lib/rules/consistent-return.js +18 -11
- package/lib/rules/constructor-super.js +3 -3
- package/lib/rules/curly.js +11 -7
- package/lib/rules/default-case.js +3 -3
- package/lib/rules/eqeqeq.js +15 -6
- package/lib/rules/func-call-spacing.js +10 -13
- package/lib/rules/func-names.js +20 -5
- package/lib/rules/generator-star-spacing.js +18 -19
- package/lib/rules/id-blacklist.js +2 -2
- package/lib/rules/id-length.js +3 -3
- package/lib/rules/id-match.js +2 -2
- package/lib/rules/indent.js +7 -6
- package/lib/rules/key-spacing.js +12 -16
- package/lib/rules/keyword-spacing.js +21 -17
- package/lib/rules/line-comment-position.js +16 -6
- package/lib/rules/linebreak-style.js +7 -1
- package/lib/rules/lines-around-comment.js +23 -4
- package/lib/rules/lines-around-directive.js +3 -3
- package/lib/rules/max-lines.js +2 -2
- package/lib/rules/max-params.js +17 -4
- package/lib/rules/max-statements-per-line.js +7 -6
- package/lib/rules/max-statements.js +11 -10
- package/lib/rules/newline-after-var.js +7 -2
- package/lib/rules/newline-per-chained-call.js +3 -1
- package/lib/rules/no-compare-neg-zero.js +53 -0
- package/lib/rules/no-cond-assign.js +3 -3
- package/lib/rules/no-else-return.js +13 -1
- package/lib/rules/no-empty-function.js +9 -16
- package/lib/rules/no-extend-native.js +3 -3
- package/lib/rules/no-extra-bind.js +3 -4
- package/lib/rules/no-extra-boolean-cast.js +8 -0
- package/lib/rules/no-extra-parens.js +1 -2
- package/lib/rules/no-extra-semi.js +13 -1
- package/lib/rules/no-inner-declarations.js +4 -4
- package/lib/rules/no-invalid-regexp.js +2 -1
- package/lib/rules/no-irregular-whitespace.js +7 -1
- package/lib/rules/no-lone-blocks.js +10 -10
- package/lib/rules/no-mixed-operators.js +1 -7
- package/lib/rules/no-multi-spaces.js +4 -1
- package/lib/rules/no-multi-str.js +7 -3
- package/lib/rules/no-multiple-empty-lines.js +2 -4
- package/lib/rules/no-param-reassign.js +29 -6
- package/lib/rules/no-restricted-properties.js +2 -0
- package/lib/rules/no-return-assign.js +7 -14
- package/lib/rules/no-return-await.js +1 -1
- package/lib/rules/no-sequences.js +7 -6
- package/lib/rules/no-trailing-spaces.js +8 -2
- package/lib/rules/no-undefined.js +45 -6
- package/lib/rules/no-unexpected-multiline.js +9 -8
- package/lib/rules/no-unneeded-ternary.js +5 -1
- package/lib/rules/no-unused-labels.js +17 -2
- package/lib/rules/no-unused-vars.js +13 -27
- package/lib/rules/no-use-before-define.js +1 -1
- package/lib/rules/no-useless-computed-key.js +8 -3
- package/lib/rules/no-useless-concat.js +10 -7
- package/lib/rules/no-useless-escape.js +2 -2
- package/lib/rules/no-useless-return.js +14 -9
- package/lib/rules/no-var.js +1 -3
- package/lib/rules/no-whitespace-before-property.js +5 -16
- package/lib/rules/nonblock-statement-body-position.js +114 -0
- package/lib/rules/object-curly-newline.js +2 -2
- package/lib/rules/object-curly-spacing.js +7 -25
- package/lib/rules/object-property-newline.js +3 -3
- package/lib/rules/object-shorthand.js +4 -3
- package/lib/rules/operator-assignment.js +2 -2
- package/lib/rules/operator-linebreak.js +8 -10
- package/lib/rules/padded-blocks.js +39 -30
- package/lib/rules/prefer-destructuring.js +1 -1
- package/lib/rules/prefer-spread.js +1 -1
- package/lib/rules/prefer-template.js +1 -1
- package/lib/rules/quotes.js +10 -6
- package/lib/rules/semi-spacing.js +4 -0
- package/lib/rules/semi.js +13 -1
- package/lib/rules/space-before-function-paren.js +8 -5
- package/lib/rules/space-unary-ops.js +19 -1
- package/lib/rules/spaced-comment.js +2 -2
- package/lib/rules/strict.js +10 -4
- package/lib/rules/unicode-bom.js +1 -1
- package/lib/rules/wrap-iife.js +5 -5
- package/lib/rules/yoda.js +4 -9
- package/lib/testers/rule-tester.js +46 -9
- package/lib/token-store/backward-token-comment-cursor.js +57 -0
- package/lib/token-store/backward-token-cursor.js +56 -0
- package/lib/token-store/cursor.js +76 -0
- package/lib/token-store/cursors.js +92 -0
- package/lib/token-store/decorative-cursor.js +39 -0
- package/lib/token-store/filter-cursor.js +43 -0
- package/lib/token-store/forward-token-comment-cursor.js +57 -0
- package/lib/token-store/forward-token-cursor.js +61 -0
- package/lib/token-store/index.js +604 -0
- package/lib/token-store/limit-cursor.js +40 -0
- package/lib/token-store/padded-token-cursor.js +38 -0
- package/lib/token-store/skip-cursor.js +42 -0
- package/lib/token-store/utils.js +100 -0
- package/lib/util/fix-tracker.js +121 -0
- package/lib/util/source-code-fixer.js +35 -39
- package/lib/util/source-code.js +129 -16
- package/messages/extend-config-missing.txt +3 -0
- package/package.json +5 -6
- package/lib/token-store.js +0 -203
@@ -0,0 +1,57 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the cursor which iterates tokens and comments in reverse.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const Cursor = require("./cursor");
|
12
|
+
const utils = require("./utils");
|
13
|
+
|
14
|
+
//------------------------------------------------------------------------------
|
15
|
+
// Exports
|
16
|
+
//------------------------------------------------------------------------------
|
17
|
+
|
18
|
+
/**
|
19
|
+
* The cursor which iterates tokens and comments in reverse.
|
20
|
+
*/
|
21
|
+
module.exports = class BackwardTokenCommentCursor extends Cursor {
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Initializes this cursor.
|
25
|
+
* @param {Token[]} tokens - The array of tokens.
|
26
|
+
* @param {Comment[]} comments - The array of comments.
|
27
|
+
* @param {Object} indexMap - The map from locations to indices in `tokens`.
|
28
|
+
* @param {number} startLoc - The start location of the iteration range.
|
29
|
+
* @param {number} endLoc - The end location of the iteration range.
|
30
|
+
*/
|
31
|
+
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
32
|
+
super();
|
33
|
+
this.tokens = tokens;
|
34
|
+
this.comments = comments;
|
35
|
+
this.tokenIndex = utils.getLastIndex(tokens, indexMap, endLoc);
|
36
|
+
this.commentIndex = utils.search(comments, endLoc) - 1;
|
37
|
+
this.border = startLoc;
|
38
|
+
}
|
39
|
+
|
40
|
+
/** @inheritdoc */
|
41
|
+
moveNext() {
|
42
|
+
const token = (this.tokenIndex >= 0) ? this.tokens[this.tokenIndex] : null;
|
43
|
+
const comment = (this.commentIndex >= 0) ? this.comments[this.commentIndex] : null;
|
44
|
+
|
45
|
+
if (token && (!comment || token.range[1] > comment.range[1])) {
|
46
|
+
this.current = token;
|
47
|
+
this.tokenIndex -= 1;
|
48
|
+
} else if (comment) {
|
49
|
+
this.current = comment;
|
50
|
+
this.commentIndex -= 1;
|
51
|
+
} else {
|
52
|
+
this.current = null;
|
53
|
+
}
|
54
|
+
|
55
|
+
return Boolean(this.current) && (this.border === -1 || this.current.range[0] >= this.border);
|
56
|
+
}
|
57
|
+
};
|
@@ -0,0 +1,56 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the cursor which iterates tokens only in reverse.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const Cursor = require("./cursor");
|
12
|
+
const utils = require("./utils");
|
13
|
+
|
14
|
+
//------------------------------------------------------------------------------
|
15
|
+
// Exports
|
16
|
+
//------------------------------------------------------------------------------
|
17
|
+
|
18
|
+
/**
|
19
|
+
* The cursor which iterates tokens only in reverse.
|
20
|
+
*/
|
21
|
+
module.exports = class BackwardTokenCursor extends Cursor {
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Initializes this cursor.
|
25
|
+
* @param {Token[]} tokens - The array of tokens.
|
26
|
+
* @param {Comment[]} comments - The array of comments.
|
27
|
+
* @param {Object} indexMap - The map from locations to indices in `tokens`.
|
28
|
+
* @param {number} startLoc - The start location of the iteration range.
|
29
|
+
* @param {number} endLoc - The end location of the iteration range.
|
30
|
+
*/
|
31
|
+
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
32
|
+
super();
|
33
|
+
this.tokens = tokens;
|
34
|
+
this.index = utils.getLastIndex(tokens, indexMap, endLoc);
|
35
|
+
this.indexEnd = utils.getFirstIndex(tokens, indexMap, startLoc);
|
36
|
+
}
|
37
|
+
|
38
|
+
/** @inheritdoc */
|
39
|
+
moveNext() {
|
40
|
+
if (this.index >= this.indexEnd) {
|
41
|
+
this.current = this.tokens[this.index];
|
42
|
+
this.index -= 1;
|
43
|
+
return true;
|
44
|
+
}
|
45
|
+
return false;
|
46
|
+
}
|
47
|
+
|
48
|
+
//
|
49
|
+
// Shorthand for performance.
|
50
|
+
//
|
51
|
+
|
52
|
+
/** @inheritdoc */
|
53
|
+
getOneToken() {
|
54
|
+
return (this.index >= this.indexEnd) ? this.tokens[this.index] : null;
|
55
|
+
}
|
56
|
+
};
|
@@ -0,0 +1,76 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the abstract class about cursors which iterate tokens.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Exports
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
/**
|
12
|
+
* The abstract class about cursors which iterate tokens.
|
13
|
+
*
|
14
|
+
* This class has 2 abstract methods.
|
15
|
+
*
|
16
|
+
* - `current: Token | Comment | null` ... The current token.
|
17
|
+
* - `moveNext(): boolean` ... Moves this cursor to the next token. If the next token didn't exist, it returns `false`.
|
18
|
+
*
|
19
|
+
* This is similar to ES2015 Iterators.
|
20
|
+
* However, Iterators were slow (at 2017-01), so I created this class as similar to C# IEnumerable.
|
21
|
+
*
|
22
|
+
* There are the following known sub classes.
|
23
|
+
*
|
24
|
+
* - ForwardTokenCursor .......... The cursor which iterates tokens only.
|
25
|
+
* - BackwardTokenCursor ......... The cursor which iterates tokens only in reverse.
|
26
|
+
* - ForwardTokenCommentCursor ... The cursor which iterates tokens and comments.
|
27
|
+
* - BackwardTokenCommentCursor .. The cursor which iterates tokens and comments in reverse.
|
28
|
+
* - DecorativeCursor
|
29
|
+
* - FilterCursor ............ The cursor which ignores the specified tokens.
|
30
|
+
* - SkipCursor .............. The cursor which ignores the first few tokens.
|
31
|
+
* - LimitCursor ............. The cursor which limits the count of tokens.
|
32
|
+
*
|
33
|
+
*/
|
34
|
+
module.exports = class Cursor {
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Initializes this cursor.
|
38
|
+
*/
|
39
|
+
constructor() {
|
40
|
+
this.current = null;
|
41
|
+
}
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Gets the first token.
|
45
|
+
* This consumes this cursor.
|
46
|
+
* @returns {Token|Comment} The first token or null.
|
47
|
+
*/
|
48
|
+
getOneToken() {
|
49
|
+
return this.moveNext() ? this.current : null;
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Gets the first tokens.
|
54
|
+
* This consumes this cursor.
|
55
|
+
* @returns {(Token|Comment)[]} All tokens.
|
56
|
+
*/
|
57
|
+
getAllTokens() {
|
58
|
+
const tokens = [];
|
59
|
+
|
60
|
+
while (this.moveNext()) {
|
61
|
+
tokens.push(this.current);
|
62
|
+
}
|
63
|
+
|
64
|
+
return tokens;
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Moves this cursor to the next token.
|
69
|
+
* @returns {boolean} `true` if the next token exists.
|
70
|
+
* @abstract
|
71
|
+
*/
|
72
|
+
/* istanbul ignore next */
|
73
|
+
moveNext() { // eslint-disable-line class-methods-use-this
|
74
|
+
throw new Error("Not implemented.");
|
75
|
+
}
|
76
|
+
};
|
@@ -0,0 +1,92 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define 2 token factories; forward and backward.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const BackwardTokenCommentCursor = require("./backward-token-comment-cursor");
|
12
|
+
const BackwardTokenCursor = require("./backward-token-cursor");
|
13
|
+
const FilterCursor = require("./filter-cursor");
|
14
|
+
const ForwardTokenCommentCursor = require("./forward-token-comment-cursor");
|
15
|
+
const ForwardTokenCursor = require("./forward-token-cursor");
|
16
|
+
const LimitCursor = require("./limit-cursor");
|
17
|
+
const SkipCursor = require("./skip-cursor");
|
18
|
+
|
19
|
+
//------------------------------------------------------------------------------
|
20
|
+
// Helpers
|
21
|
+
//------------------------------------------------------------------------------
|
22
|
+
|
23
|
+
/**
|
24
|
+
* The cursor factory.
|
25
|
+
* @private
|
26
|
+
*/
|
27
|
+
class CursorFactory {
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Initializes this cursor.
|
31
|
+
* @param {Function} TokenCursor - The class of the cursor which iterates tokens only.
|
32
|
+
* @param {Function} TokenCommentCursor - The class of the cursor which iterates the mix of tokens and comments.
|
33
|
+
*/
|
34
|
+
constructor(TokenCursor, TokenCommentCursor) {
|
35
|
+
this.TokenCursor = TokenCursor;
|
36
|
+
this.TokenCommentCursor = TokenCommentCursor;
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Creates a base cursor instance that can be decorated by createCursor.
|
41
|
+
*
|
42
|
+
* @param {Token[]} tokens - The array of tokens.
|
43
|
+
* @param {Comment[]} comments - The array of comments.
|
44
|
+
* @param {Object} indexMap - The map from locations to indices in `tokens`.
|
45
|
+
* @param {number} startLoc - The start location of the iteration range.
|
46
|
+
* @param {number} endLoc - The end location of the iteration range.
|
47
|
+
* @param {boolean} includeComments - The flag to iterate comments as well.
|
48
|
+
* @returns {Cursor} The created base cursor.
|
49
|
+
*/
|
50
|
+
createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) {
|
51
|
+
const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor;
|
52
|
+
|
53
|
+
return new Cursor(tokens, comments, indexMap, startLoc, endLoc);
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Creates a cursor that iterates tokens with normalized options.
|
58
|
+
*
|
59
|
+
* @param {Token[]} tokens - The array of tokens.
|
60
|
+
* @param {Comment[]} comments - The array of comments.
|
61
|
+
* @param {Object} indexMap - The map from locations to indices in `tokens`.
|
62
|
+
* @param {number} startLoc - The start location of the iteration range.
|
63
|
+
* @param {number} endLoc - The end location of the iteration range.
|
64
|
+
* @param {boolean} includeComments - The flag to iterate comments as well.
|
65
|
+
* @param {Function|null} filter - The predicate function to choose tokens.
|
66
|
+
* @param {number} skip - The count of tokens the cursor skips.
|
67
|
+
* @param {number} count - The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility.
|
68
|
+
* @returns {Cursor} The created cursor.
|
69
|
+
*/
|
70
|
+
createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) {
|
71
|
+
let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments);
|
72
|
+
|
73
|
+
if (filter) {
|
74
|
+
cursor = new FilterCursor(cursor, filter);
|
75
|
+
}
|
76
|
+
if (skip >= 1) {
|
77
|
+
cursor = new SkipCursor(cursor, skip);
|
78
|
+
}
|
79
|
+
if (count >= 0) {
|
80
|
+
cursor = new LimitCursor(cursor, count);
|
81
|
+
}
|
82
|
+
|
83
|
+
return cursor;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
//------------------------------------------------------------------------------
|
88
|
+
// Exports
|
89
|
+
//------------------------------------------------------------------------------
|
90
|
+
|
91
|
+
exports.forward = new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor);
|
92
|
+
exports.backward = new CursorFactory(BackwardTokenCursor, BackwardTokenCommentCursor);
|
@@ -0,0 +1,39 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the abstract class about cursors which manipulate another cursor.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const Cursor = require("./cursor");
|
12
|
+
|
13
|
+
//------------------------------------------------------------------------------
|
14
|
+
// Exports
|
15
|
+
//------------------------------------------------------------------------------
|
16
|
+
|
17
|
+
/**
|
18
|
+
* The abstract class about cursors which manipulate another cursor.
|
19
|
+
*/
|
20
|
+
module.exports = class DecorativeCursor extends Cursor {
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Initializes this cursor.
|
24
|
+
* @param {Cursor} cursor - The cursor to be decorated.
|
25
|
+
*/
|
26
|
+
constructor(cursor) {
|
27
|
+
super();
|
28
|
+
this.cursor = cursor;
|
29
|
+
}
|
30
|
+
|
31
|
+
/** @inheritdoc */
|
32
|
+
moveNext() {
|
33
|
+
const retv = this.cursor.moveNext();
|
34
|
+
|
35
|
+
this.current = this.cursor.current;
|
36
|
+
|
37
|
+
return retv;
|
38
|
+
}
|
39
|
+
};
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the cursor which ignores specified tokens.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const DecorativeCursor = require("./decorative-cursor");
|
12
|
+
|
13
|
+
//------------------------------------------------------------------------------
|
14
|
+
// Exports
|
15
|
+
//------------------------------------------------------------------------------
|
16
|
+
|
17
|
+
/**
|
18
|
+
* The decorative cursor which ignores specified tokens.
|
19
|
+
*/
|
20
|
+
module.exports = class FilterCursor extends DecorativeCursor {
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Initializes this cursor.
|
24
|
+
* @param {Cursor} cursor - The cursor to be decorated.
|
25
|
+
* @param {Function} predicate - The predicate function to decide tokens this cursor iterates.
|
26
|
+
*/
|
27
|
+
constructor(cursor, predicate) {
|
28
|
+
super(cursor);
|
29
|
+
this.predicate = predicate;
|
30
|
+
}
|
31
|
+
|
32
|
+
/** @inheritdoc */
|
33
|
+
moveNext() {
|
34
|
+
const predicate = this.predicate;
|
35
|
+
|
36
|
+
while (super.moveNext()) {
|
37
|
+
if (predicate(this.current)) {
|
38
|
+
return true;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
return false;
|
42
|
+
}
|
43
|
+
};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the cursor which iterates tokens and comments.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const Cursor = require("./cursor");
|
12
|
+
const utils = require("./utils");
|
13
|
+
|
14
|
+
//------------------------------------------------------------------------------
|
15
|
+
// Exports
|
16
|
+
//------------------------------------------------------------------------------
|
17
|
+
|
18
|
+
/**
|
19
|
+
* The cursor which iterates tokens and comments.
|
20
|
+
*/
|
21
|
+
module.exports = class ForwardTokenCommentCursor extends Cursor {
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Initializes this cursor.
|
25
|
+
* @param {Token[]} tokens - The array of tokens.
|
26
|
+
* @param {Comment[]} comments - The array of comments.
|
27
|
+
* @param {Object} indexMap - The map from locations to indices in `tokens`.
|
28
|
+
* @param {number} startLoc - The start location of the iteration range.
|
29
|
+
* @param {number} endLoc - The end location of the iteration range.
|
30
|
+
*/
|
31
|
+
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
32
|
+
super();
|
33
|
+
this.tokens = tokens;
|
34
|
+
this.comments = comments;
|
35
|
+
this.tokenIndex = utils.getFirstIndex(tokens, indexMap, startLoc);
|
36
|
+
this.commentIndex = utils.search(comments, startLoc);
|
37
|
+
this.border = endLoc;
|
38
|
+
}
|
39
|
+
|
40
|
+
/** @inheritdoc */
|
41
|
+
moveNext() {
|
42
|
+
const token = (this.tokenIndex < this.tokens.length) ? this.tokens[this.tokenIndex] : null;
|
43
|
+
const comment = (this.commentIndex < this.comments.length) ? this.comments[this.commentIndex] : null;
|
44
|
+
|
45
|
+
if (token && (!comment || token.range[0] < comment.range[0])) {
|
46
|
+
this.current = token;
|
47
|
+
this.tokenIndex += 1;
|
48
|
+
} else if (comment) {
|
49
|
+
this.current = comment;
|
50
|
+
this.commentIndex += 1;
|
51
|
+
} else {
|
52
|
+
this.current = null;
|
53
|
+
}
|
54
|
+
|
55
|
+
return Boolean(this.current) && (this.border === -1 || this.current.range[1] <= this.border);
|
56
|
+
}
|
57
|
+
};
|
@@ -0,0 +1,61 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the cursor which iterates tokens only.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const Cursor = require("./cursor");
|
12
|
+
const utils = require("./utils");
|
13
|
+
|
14
|
+
//------------------------------------------------------------------------------
|
15
|
+
// Exports
|
16
|
+
//------------------------------------------------------------------------------
|
17
|
+
|
18
|
+
/**
|
19
|
+
* The cursor which iterates tokens only.
|
20
|
+
*/
|
21
|
+
module.exports = class ForwardTokenCursor extends Cursor {
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Initializes this cursor.
|
25
|
+
* @param {Token[]} tokens - The array of tokens.
|
26
|
+
* @param {Comment[]} comments - The array of comments.
|
27
|
+
* @param {Object} indexMap - The map from locations to indices in `tokens`.
|
28
|
+
* @param {number} startLoc - The start location of the iteration range.
|
29
|
+
* @param {number} endLoc - The end location of the iteration range.
|
30
|
+
*/
|
31
|
+
constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
32
|
+
super();
|
33
|
+
this.tokens = tokens;
|
34
|
+
this.index = utils.getFirstIndex(tokens, indexMap, startLoc);
|
35
|
+
this.indexEnd = utils.getLastIndex(tokens, indexMap, endLoc);
|
36
|
+
}
|
37
|
+
|
38
|
+
/** @inheritdoc */
|
39
|
+
moveNext() {
|
40
|
+
if (this.index <= this.indexEnd) {
|
41
|
+
this.current = this.tokens[this.index];
|
42
|
+
this.index += 1;
|
43
|
+
return true;
|
44
|
+
}
|
45
|
+
return false;
|
46
|
+
}
|
47
|
+
|
48
|
+
//
|
49
|
+
// Shorthand for performance.
|
50
|
+
//
|
51
|
+
|
52
|
+
/** @inheritdoc */
|
53
|
+
getOneToken() {
|
54
|
+
return (this.index <= this.indexEnd) ? this.tokens[this.index] : null;
|
55
|
+
}
|
56
|
+
|
57
|
+
/** @inheritdoc */
|
58
|
+
getAllTokens() {
|
59
|
+
return this.tokens.slice(this.index, this.indexEnd + 1);
|
60
|
+
}
|
61
|
+
};
|