eslint 3.15.0 → 3.16.0
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 +32 -0
- package/conf/{eslint.json → eslint-recommended.js} +85 -71
- package/lib/ast-utils.js +185 -19
- 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/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 +6 -6
- package/lib/rules/comma-spacing.js +3 -3
- package/lib/rules/consistent-return.js +1 -1
- 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/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 +2 -13
- package/lib/rules/line-comment-position.js +1 -1
- package/lib/rules/linebreak-style.js +7 -1
- package/lib/rules/lines-around-comment.js +4 -4
- package/lib/rules/lines-around-directive.js +3 -3
- package/lib/rules/max-lines.js +2 -2
- package/lib/rules/max-statements-per-line.js +7 -6
- package/lib/rules/newline-after-var.js +7 -2
- package/lib/rules/newline-per-chained-call.js +3 -1
- package/lib/rules/no-cond-assign.js +3 -3
- 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-inner-declarations.js +4 -4
- 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-return-assign.js +7 -14
- 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 +2 -16
- 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 +1 -1
- package/lib/rules/no-useless-return.js +1 -7
- package/lib/rules/no-var.js +1 -3
- package/lib/rules/no-whitespace-before-property.js +5 -16
- 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 +2 -2
- package/lib/rules/operator-assignment.js +1 -1
- package/lib/rules/operator-linebreak.js +8 -10
- package/lib/rules/padded-blocks.js +4 -4
- 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/space-before-function-paren.js +8 -5
- package/lib/rules/spaced-comment.js +2 -2
- package/lib/rules/strict.js +2 -2
- package/lib/rules/unicode-bom.js +1 -1
- package/lib/rules/wrap-iife.js +5 -5
- package/lib/rules/yoda.js +2 -7
- package/lib/testers/rule-tester.js +13 -6
- 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/source-code-fixer.js +35 -39
- package/lib/util/source-code.js +31 -15
- package/messages/extend-config-missing.txt +3 -0
- package/package.json +2 -2
- package/lib/token-store.js +0 -203
@@ -0,0 +1,42 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define the cursor which ignores the first few 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 the first few tokens.
|
19
|
+
*/
|
20
|
+
module.exports = class SkipCursor extends DecorativeCursor {
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Initializes this cursor.
|
24
|
+
* @param {Cursor} cursor - The cursor to be decorated.
|
25
|
+
* @param {number} count - The count of tokens this cursor skips.
|
26
|
+
*/
|
27
|
+
constructor(cursor, count) {
|
28
|
+
super(cursor);
|
29
|
+
this.count = count;
|
30
|
+
}
|
31
|
+
|
32
|
+
/** @inheritdoc */
|
33
|
+
moveNext() {
|
34
|
+
while (this.count > 0) {
|
35
|
+
this.count -= 1;
|
36
|
+
if (!super.moveNext()) {
|
37
|
+
return false;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
return super.moveNext();
|
41
|
+
}
|
42
|
+
};
|
@@ -0,0 +1,100 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Define utilify functions for token store.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
//------------------------------------------------------------------------------
|
8
|
+
// Requirements
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
|
11
|
+
const lodash = require("lodash");
|
12
|
+
|
13
|
+
//------------------------------------------------------------------------------
|
14
|
+
// Helpers
|
15
|
+
//------------------------------------------------------------------------------
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Gets `token.range[0]` from the given token.
|
19
|
+
*
|
20
|
+
* @param {Node|Token|Comment} token - The token to get.
|
21
|
+
* @returns {number} The start location.
|
22
|
+
* @private
|
23
|
+
*/
|
24
|
+
function getStartLocation(token) {
|
25
|
+
return token.range[0];
|
26
|
+
}
|
27
|
+
|
28
|
+
//------------------------------------------------------------------------------
|
29
|
+
// Exports
|
30
|
+
//------------------------------------------------------------------------------
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Binary-searches the index of the first token which is after the given location.
|
34
|
+
* If it was not found, this returns `tokens.length`.
|
35
|
+
*
|
36
|
+
* @param {(Token|Comment)[]} tokens - It searches the token in this list.
|
37
|
+
* @param {number} location - The location to search.
|
38
|
+
* @returns {number} The found index or `tokens.length`.
|
39
|
+
*/
|
40
|
+
exports.search = function search(tokens, location) {
|
41
|
+
return lodash.sortedIndexBy(
|
42
|
+
tokens,
|
43
|
+
{ range: [location] },
|
44
|
+
getStartLocation
|
45
|
+
);
|
46
|
+
};
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Gets the index of the `startLoc` in `tokens`.
|
50
|
+
* `startLoc` can be the value of `node.range[1]`, so this checks about `startLoc - 1` as well.
|
51
|
+
*
|
52
|
+
* @param {(Token|Comment)[]} tokens - The tokens to find an index.
|
53
|
+
* @param {Object} indexMap - The map from locations to indices.
|
54
|
+
* @param {number} startLoc - The location to get an index.
|
55
|
+
* @returns {number} The index.
|
56
|
+
*/
|
57
|
+
exports.getFirstIndex = function getFirstIndex(tokens, indexMap, startLoc) {
|
58
|
+
if (startLoc in indexMap) {
|
59
|
+
return indexMap[startLoc];
|
60
|
+
}
|
61
|
+
if ((startLoc - 1) in indexMap) {
|
62
|
+
const index = indexMap[startLoc - 1];
|
63
|
+
const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
|
64
|
+
|
65
|
+
// For the map of "comment's location -> token's index", it points the next token of a comment.
|
66
|
+
// In that case, +1 is unnecessary.
|
67
|
+
if (token && token.range[0] >= startLoc) {
|
68
|
+
return index;
|
69
|
+
}
|
70
|
+
return index + 1;
|
71
|
+
}
|
72
|
+
return 0;
|
73
|
+
};
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Gets the index of the `endLoc` in `tokens`.
|
77
|
+
* The information of end locations are recorded at `endLoc - 1` in `indexMap`, so this checks about `endLoc - 1` as well.
|
78
|
+
*
|
79
|
+
* @param {(Token|Comment)[]} tokens - The tokens to find an index.
|
80
|
+
* @param {Object} indexMap - The map from locations to indices.
|
81
|
+
* @param {number} endLoc - The location to get an index.
|
82
|
+
* @returns {number} The index.
|
83
|
+
*/
|
84
|
+
exports.getLastIndex = function getLastIndex(tokens, indexMap, endLoc) {
|
85
|
+
if (endLoc in indexMap) {
|
86
|
+
return indexMap[endLoc] - 1;
|
87
|
+
}
|
88
|
+
if ((endLoc - 1) in indexMap) {
|
89
|
+
const index = indexMap[endLoc - 1];
|
90
|
+
const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
|
91
|
+
|
92
|
+
// For the map of "comment's location -> token's index", it points the next token of a comment.
|
93
|
+
// In that case, -1 is necessary.
|
94
|
+
if (token && token.range[1] > endLoc) {
|
95
|
+
return index - 1;
|
96
|
+
}
|
97
|
+
return index;
|
98
|
+
}
|
99
|
+
return tokens.length - 1;
|
100
|
+
};
|
@@ -16,6 +16,17 @@ const debug = require("debug")("eslint:text-fixer");
|
|
16
16
|
|
17
17
|
const BOM = "\uFEFF";
|
18
18
|
|
19
|
+
/**
|
20
|
+
* Compares items in a messages array by range.
|
21
|
+
* @param {Message} a The first message.
|
22
|
+
* @param {Message} b The second message.
|
23
|
+
* @returns {int} -1 if a comes before b, 1 if a comes after b, 0 if equal.
|
24
|
+
* @private
|
25
|
+
*/
|
26
|
+
function compareMessagesByFixRange(a, b) {
|
27
|
+
return a.fix.range[0] - b.fix.range[0] || a.fix.range[1] - b.fix.range[1];
|
28
|
+
}
|
29
|
+
|
19
30
|
/**
|
20
31
|
* Compares items in a messages array by line and column.
|
21
32
|
* @param {Message} a The first message.
|
@@ -24,13 +35,7 @@ const BOM = "\uFEFF";
|
|
24
35
|
* @private
|
25
36
|
*/
|
26
37
|
function compareMessagesByLocation(a, b) {
|
27
|
-
|
28
|
-
|
29
|
-
if (lineDiff === 0) {
|
30
|
-
return a.column - b.column;
|
31
|
-
}
|
32
|
-
return lineDiff;
|
33
|
-
|
38
|
+
return a.line - b.line || a.column - b.column;
|
34
39
|
}
|
35
40
|
|
36
41
|
//------------------------------------------------------------------------------
|
@@ -68,9 +73,10 @@ SourceCodeFixer.applyFixes = function(sourceCode, messages) {
|
|
68
73
|
// clone the array
|
69
74
|
const remainingMessages = [],
|
70
75
|
fixes = [],
|
76
|
+
bom = (sourceCode.hasBOM ? BOM : ""),
|
71
77
|
text = sourceCode.text;
|
72
|
-
let
|
73
|
-
|
78
|
+
let lastPos = Number.NEGATIVE_INFINITY,
|
79
|
+
output = bom;
|
74
80
|
|
75
81
|
messages.forEach(problem => {
|
76
82
|
if (problem.hasOwnProperty("fix")) {
|
@@ -83,51 +89,41 @@ SourceCodeFixer.applyFixes = function(sourceCode, messages) {
|
|
83
89
|
if (fixes.length) {
|
84
90
|
debug("Found fixes to apply");
|
85
91
|
|
86
|
-
|
87
|
-
fixes.sort((a, b) => b.fix.range[1] - a.fix.range[1] || b.fix.range[0] - a.fix.range[0]);
|
88
|
-
|
89
|
-
// split into array of characters for easier manipulation
|
90
|
-
const chars = text.split("");
|
91
|
-
|
92
|
-
fixes.forEach(problem => {
|
92
|
+
for (const problem of fixes.sort(compareMessagesByFixRange)) {
|
93
93
|
const fix = problem.fix;
|
94
|
-
|
94
|
+
const start = fix.range[0];
|
95
95
|
const end = fix.range[1];
|
96
|
-
let insertionText = fix.text;
|
97
|
-
|
98
|
-
if (end < lastFixPos) {
|
99
|
-
if (start < 0) {
|
100
|
-
|
101
|
-
// Remove BOM.
|
102
|
-
prefix = "";
|
103
|
-
start = 0;
|
104
|
-
}
|
105
|
-
|
106
|
-
if (start === 0 && insertionText[0] === BOM) {
|
107
|
-
|
108
|
-
// Set BOM.
|
109
|
-
prefix = BOM;
|
110
|
-
insertionText = insertionText.slice(1);
|
111
|
-
}
|
112
96
|
|
113
|
-
|
114
|
-
|
115
|
-
} else {
|
97
|
+
// Remain it as a problem if it's overlapped.
|
98
|
+
if (lastPos >= start) {
|
116
99
|
remainingMessages.push(problem);
|
100
|
+
continue;
|
117
101
|
}
|
118
|
-
|
102
|
+
|
103
|
+
// Remove BOM.
|
104
|
+
if ((start < 0 && end >= 0) || (start === 0 && fix.text.startsWith(BOM))) {
|
105
|
+
output = "";
|
106
|
+
}
|
107
|
+
|
108
|
+
// Make output to this fix.
|
109
|
+
output += text.slice(Math.max(0, lastPos), Math.max(0, start));
|
110
|
+
output += fix.text;
|
111
|
+
lastPos = end;
|
112
|
+
}
|
113
|
+
output += text.slice(Math.max(0, lastPos));
|
119
114
|
|
120
115
|
return {
|
121
116
|
fixed: true,
|
122
117
|
messages: remainingMessages.sort(compareMessagesByLocation),
|
123
|
-
output
|
118
|
+
output
|
124
119
|
};
|
125
120
|
}
|
121
|
+
|
126
122
|
debug("No fixes to apply");
|
127
123
|
return {
|
128
124
|
fixed: false,
|
129
125
|
messages,
|
130
|
-
output:
|
126
|
+
output: bom + text
|
131
127
|
};
|
132
128
|
|
133
129
|
};
|
package/lib/util/source-code.js
CHANGED
@@ -8,8 +8,9 @@
|
|
8
8
|
// Requirements
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
-
const
|
12
|
-
Traverser = require("./traverser")
|
11
|
+
const TokenStore = require("../token-store"),
|
12
|
+
Traverser = require("./traverser"),
|
13
|
+
astUtils = require("../ast-utils");
|
13
14
|
|
14
15
|
//------------------------------------------------------------------------------
|
15
16
|
// Private
|
@@ -77,6 +78,28 @@ function looksLikeExport(astNode) {
|
|
77
78
|
astNode.type === "ExportAllDeclaration" || astNode.type === "ExportSpecifier";
|
78
79
|
}
|
79
80
|
|
81
|
+
/**
|
82
|
+
* Merges two sorted lists into a larger sorted list in O(n) time
|
83
|
+
* @param {Token[]} tokens The list of tokens
|
84
|
+
* @param {Token[]} comments The list of comments
|
85
|
+
* @returns {Token[]} A sorted list of tokens and comments
|
86
|
+
*/
|
87
|
+
function sortedMerge(tokens, comments) {
|
88
|
+
const result = [];
|
89
|
+
let tokenIndex = 0;
|
90
|
+
let commentIndex = 0;
|
91
|
+
|
92
|
+
while (tokenIndex < tokens.length || commentIndex < comments.length) {
|
93
|
+
if (commentIndex >= comments.length || tokenIndex < tokens.length && tokens[tokenIndex].range[0] < comments[commentIndex].range[0]) {
|
94
|
+
result.push(tokens[tokenIndex++]);
|
95
|
+
} else {
|
96
|
+
result.push(comments[commentIndex++]);
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
return result;
|
101
|
+
}
|
102
|
+
|
80
103
|
|
81
104
|
//------------------------------------------------------------------------------
|
82
105
|
// Public Interface
|
@@ -117,21 +140,14 @@ function SourceCode(text, ast) {
|
|
117
140
|
*/
|
118
141
|
this.lines = SourceCode.splitLines(this.text);
|
119
142
|
|
120
|
-
this.tokensAndComments = ast.tokens
|
121
|
-
.concat(ast.comments)
|
122
|
-
.sort((left, right) => left.range[0] - right.range[0]);
|
143
|
+
this.tokensAndComments = sortedMerge(ast.tokens, ast.comments);
|
123
144
|
|
124
145
|
// create token store methods
|
125
|
-
const tokenStore =
|
126
|
-
|
127
|
-
Object.keys(tokenStore).forEach(methodName => {
|
128
|
-
this[methodName] = tokenStore[methodName];
|
129
|
-
});
|
146
|
+
const tokenStore = new TokenStore(ast.tokens, ast.comments);
|
130
147
|
|
131
|
-
const
|
132
|
-
|
133
|
-
|
134
|
-
this.getTokenOrCommentAfter = tokensAndCommentsStore.getTokenAfter;
|
148
|
+
for (const methodName of TokenStore.PUBLIC_METHODS) {
|
149
|
+
this[methodName] = tokenStore[methodName].bind(tokenStore);
|
150
|
+
}
|
135
151
|
|
136
152
|
// don't allow modification of this object
|
137
153
|
Object.freeze(this);
|
@@ -145,7 +161,7 @@ function SourceCode(text, ast) {
|
|
145
161
|
* @public
|
146
162
|
*/
|
147
163
|
SourceCode.splitLines = function(text) {
|
148
|
-
return text.split(
|
164
|
+
return text.split(astUtils.createGlobalLinebreakMatcher());
|
149
165
|
};
|
150
166
|
|
151
167
|
SourceCode.prototype = {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "eslint",
|
3
|
-
"version": "3.
|
3
|
+
"version": "3.16.0",
|
4
4
|
"author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
|
5
5
|
"description": "An AST-based pattern checker for JavaScript.",
|
6
6
|
"bin": {
|
@@ -98,7 +98,7 @@
|
|
98
98
|
"load-perf": "^0.2.0",
|
99
99
|
"markdownlint": "^0.3.1",
|
100
100
|
"mocha": "^2.4.5",
|
101
|
-
"mock-fs": "^
|
101
|
+
"mock-fs": "^4.0.0",
|
102
102
|
"npm-license": "^0.3.2",
|
103
103
|
"phantomjs-prebuilt": "^2.1.7",
|
104
104
|
"proxyquire": "^1.7.10",
|
package/lib/token-store.js
DELETED
@@ -1,203 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @fileoverview Object to handle access and retrieval of tokens.
|
3
|
-
* @author Brandon Mills
|
4
|
-
*/
|
5
|
-
"use strict";
|
6
|
-
|
7
|
-
//------------------------------------------------------------------------------
|
8
|
-
// Implementation
|
9
|
-
//------------------------------------------------------------------------------
|
10
|
-
|
11
|
-
module.exports = function(tokens) {
|
12
|
-
const api = {},
|
13
|
-
starts = Object.create(null),
|
14
|
-
ends = Object.create(null),
|
15
|
-
length = tokens.length;
|
16
|
-
|
17
|
-
/**
|
18
|
-
* Gets tokens in a given interval.
|
19
|
-
* @param {int} start Inclusive index of the first token. 0 if negative.
|
20
|
-
* @param {int} end Exclusive index of the last token.
|
21
|
-
* @returns {Token[]} Tokens in the interval.
|
22
|
-
*/
|
23
|
-
function get(start, end) {
|
24
|
-
const result = [];
|
25
|
-
|
26
|
-
for (let i = Math.max(0, start); i < end && i < length; i++) {
|
27
|
-
result.push(tokens[i]);
|
28
|
-
}
|
29
|
-
|
30
|
-
return result;
|
31
|
-
}
|
32
|
-
|
33
|
-
/**
|
34
|
-
* Gets the index in the tokens array of the last token belonging to a node.
|
35
|
-
* Usually a node ends exactly at a token, but due to ASI, sometimes a
|
36
|
-
* node's range extends beyond its last token.
|
37
|
-
* @param {ASTNode} node The node for which to find the last token's index.
|
38
|
-
* @returns {int} Index in the tokens array of the node's last token.
|
39
|
-
*/
|
40
|
-
function lastTokenIndex(node) {
|
41
|
-
const end = node.range[1];
|
42
|
-
let cursor = ends[end];
|
43
|
-
|
44
|
-
// If the node extends beyond its last token, get the token before the
|
45
|
-
// next token
|
46
|
-
if (typeof cursor === "undefined") {
|
47
|
-
cursor = starts[end] - 1;
|
48
|
-
}
|
49
|
-
|
50
|
-
// If there isn't a next token, the desired token is the last one in the
|
51
|
-
// array
|
52
|
-
if (isNaN(cursor)) {
|
53
|
-
cursor = length - 1;
|
54
|
-
}
|
55
|
-
|
56
|
-
return cursor;
|
57
|
-
}
|
58
|
-
|
59
|
-
// Map tokens' start and end range to the index in the tokens array
|
60
|
-
for (let i = 0; i < length; i++) {
|
61
|
-
const range = tokens[i].range;
|
62
|
-
|
63
|
-
starts[range[0]] = i;
|
64
|
-
ends[range[1]] = i;
|
65
|
-
}
|
66
|
-
|
67
|
-
/**
|
68
|
-
* Gets a number of tokens that precede a given node or token in the token
|
69
|
-
* stream.
|
70
|
-
* @param {(ASTNode|Token)} node The AST node or token.
|
71
|
-
* @param {int} [beforeCount=0] The number of tokens before the node or
|
72
|
-
* token to retrieve.
|
73
|
-
* @returns {Token[]} Array of objects representing tokens.
|
74
|
-
*/
|
75
|
-
api.getTokensBefore = function(node, beforeCount) {
|
76
|
-
const first = starts[node.range[0]];
|
77
|
-
|
78
|
-
return get(first - (beforeCount || 0), first);
|
79
|
-
};
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Gets the token that precedes a given node or token in the token stream.
|
83
|
-
* @param {(ASTNode|Token)} node The AST node or token.
|
84
|
-
* @param {int} [skip=0] A number of tokens to skip before the given node or
|
85
|
-
* token.
|
86
|
-
* @returns {Token} An object representing the token.
|
87
|
-
*/
|
88
|
-
api.getTokenBefore = function(node, skip) {
|
89
|
-
return tokens[starts[node.range[0]] - (skip || 0) - 1];
|
90
|
-
};
|
91
|
-
|
92
|
-
/**
|
93
|
-
* Gets a number of tokens that follow a given node or token in the token
|
94
|
-
* stream.
|
95
|
-
* @param {(ASTNode|Token)} node The AST node or token.
|
96
|
-
* @param {int} [afterCount=0] The number of tokens after the node or token
|
97
|
-
* to retrieve.
|
98
|
-
* @returns {Token[]} Array of objects representing tokens.
|
99
|
-
*/
|
100
|
-
api.getTokensAfter = function(node, afterCount) {
|
101
|
-
const start = lastTokenIndex(node) + 1;
|
102
|
-
|
103
|
-
return get(start, start + (afterCount || 0));
|
104
|
-
};
|
105
|
-
|
106
|
-
/**
|
107
|
-
* Gets the token that follows a given node or token in the token stream.
|
108
|
-
* @param {(ASTNode|Token)} node The AST node or token.
|
109
|
-
* @param {int} [skip=0] A number of tokens to skip after the given node or
|
110
|
-
* token.
|
111
|
-
* @returns {Token} An object representing the token.
|
112
|
-
*/
|
113
|
-
api.getTokenAfter = function(node, skip) {
|
114
|
-
return tokens[lastTokenIndex(node) + (skip || 0) + 1];
|
115
|
-
};
|
116
|
-
|
117
|
-
/**
|
118
|
-
* Gets all tokens that are related to the given node.
|
119
|
-
* @param {ASTNode} node The AST node.
|
120
|
-
* @param {int} [beforeCount=0] The number of tokens before the node to retrieve.
|
121
|
-
* @param {int} [afterCount=0] The number of tokens after the node to retrieve.
|
122
|
-
* @returns {Token[]} Array of objects representing tokens.
|
123
|
-
*/
|
124
|
-
api.getTokens = function(node, beforeCount, afterCount) {
|
125
|
-
return get(
|
126
|
-
starts[node.range[0]] - (beforeCount || 0),
|
127
|
-
lastTokenIndex(node) + (afterCount || 0) + 1
|
128
|
-
);
|
129
|
-
};
|
130
|
-
|
131
|
-
/**
|
132
|
-
* Gets the first `count` tokens of the given node's token stream.
|
133
|
-
* @param {ASTNode} node The AST node.
|
134
|
-
* @param {int} [count=0] The number of tokens of the node to retrieve.
|
135
|
-
* @returns {Token[]} Array of objects representing tokens.
|
136
|
-
*/
|
137
|
-
api.getFirstTokens = function(node, count) {
|
138
|
-
const first = starts[node.range[0]];
|
139
|
-
|
140
|
-
return get(
|
141
|
-
first,
|
142
|
-
Math.min(lastTokenIndex(node) + 1, first + (count || 0))
|
143
|
-
);
|
144
|
-
};
|
145
|
-
|
146
|
-
/**
|
147
|
-
* Gets the first token of the given node's token stream.
|
148
|
-
* @param {ASTNode} node The AST node.
|
149
|
-
* @param {int} [skip=0] A number of tokens to skip.
|
150
|
-
* @returns {Token} An object representing the token.
|
151
|
-
*/
|
152
|
-
api.getFirstToken = function(node, skip) {
|
153
|
-
return tokens[starts[node.range[0]] + (skip || 0)];
|
154
|
-
};
|
155
|
-
|
156
|
-
/**
|
157
|
-
* Gets the last `count` tokens of the given node.
|
158
|
-
* @param {ASTNode} node The AST node.
|
159
|
-
* @param {int} [count=0] The number of tokens of the node to retrieve.
|
160
|
-
* @returns {Token[]} Array of objects representing tokens.
|
161
|
-
*/
|
162
|
-
api.getLastTokens = function(node, count) {
|
163
|
-
const last = lastTokenIndex(node) + 1;
|
164
|
-
|
165
|
-
return get(Math.max(starts[node.range[0]], last - (count || 0)), last);
|
166
|
-
};
|
167
|
-
|
168
|
-
/**
|
169
|
-
* Gets the last token of the given node's token stream.
|
170
|
-
* @param {ASTNode} node The AST node.
|
171
|
-
* @param {int} [skip=0] A number of tokens to skip.
|
172
|
-
* @returns {Token} An object representing the token.
|
173
|
-
*/
|
174
|
-
api.getLastToken = function(node, skip) {
|
175
|
-
return tokens[lastTokenIndex(node) - (skip || 0)];
|
176
|
-
};
|
177
|
-
|
178
|
-
/**
|
179
|
-
* Gets all of the tokens between two non-overlapping nodes.
|
180
|
-
* @param {ASTNode} left Node before the desired token range.
|
181
|
-
* @param {ASTNode} right Node after the desired token range.
|
182
|
-
* @param {int} [padding=0] Number of extra tokens on either side of center.
|
183
|
-
* @returns {Token[]} Tokens between left and right plus padding.
|
184
|
-
*/
|
185
|
-
api.getTokensBetween = function(left, right, padding) {
|
186
|
-
padding = padding || 0;
|
187
|
-
return get(
|
188
|
-
lastTokenIndex(left) + 1 - padding,
|
189
|
-
starts[right.range[0]] + padding
|
190
|
-
);
|
191
|
-
};
|
192
|
-
|
193
|
-
/**
|
194
|
-
* Gets the token starting at the specified index.
|
195
|
-
* @param {int} startIndex Index of the start of the token's range.
|
196
|
-
* @returns {Token} The token starting at index, or null if no such token.
|
197
|
-
*/
|
198
|
-
api.getTokenByRangeStart = function(startIndex) {
|
199
|
-
return (tokens[starts[startIndex]] || null);
|
200
|
-
};
|
201
|
-
|
202
|
-
return api;
|
203
|
-
};
|