This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathtoken-index.js
168 lines (149 loc) · 4.63 KB
/
token-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var assign = require('lodash').assign;
var BLOCK_REGEXP = /^\s*(?:jscs\s*:\s*(en|dis)able)(.*)/;
var LINE_REGEXP = /^\s*(?:jscs\s*:\s*ignore)(.*)/;
/**
* Parses rule names in enable/disable/ignore statements.
*
* @param {String} text
* @param {Boolean} enabled
* @returns {Object}
*/
function parseRuleNames(text, enabled) {
text = text.trim();
if (!text) {
return {'*': enabled};
}
return text.split(',').reduce(function(result, ruleName) {
ruleName = ruleName.trim();
if (ruleName) {
result[ruleName] = enabled;
}
return result;
}, {});
}
/**
* Pragma index implementation.
* Checks if rule is enabled or disabled for the specified element.
*
* @param {Element} firstToken
* @constructor
*/
function TokenIndex(firstToken) {
this._buildIndex(firstToken);
}
/**
* Builds pragma index.
*
* @param {Element} firstToken
* @private
*/
TokenIndex.prototype._buildIndex = function(firstToken) {
this._hasPragmas = false;
var tokens = [];
var index = [];
var positions = [];
var currentPosition = 0;
var currentToken = firstToken;
var lastBlockState = {'*': true};
var tokenState;
var previousLoc = {line: 1, column: 0};
while (currentToken) {
tokens.push(currentToken);
currentToken.__loc = previousLoc;
var newlineCount = currentToken.getNewlineCount();
if (newlineCount > 0) {
var lines = currentToken.getSourceCodeLines();
previousLoc = {
line: previousLoc.line + newlineCount,
column: lines[lines.length - 1].length
};
} else {
previousLoc = {
line: previousLoc.line,
column: previousLoc.column + currentToken.getSourceCodeLength()
};
}
if (currentToken.isComment) {
var value = currentToken.value;
var blockMatch = BLOCK_REGEXP.exec(value);
if (blockMatch) {
this._hasPragmas = true;
lastBlockState = assign({}, lastBlockState, parseRuleNames(blockMatch[2], blockMatch[1] === 'en'));
tokenState = lastBlockState;
} else {
var lineMatch = LINE_REGEXP.exec(value);
if (lineMatch) {
this._hasPragmas = true;
var ignoreState = parseRuleNames(lineMatch[1], false);
index.push(null);
var ignoreToken = currentToken.getPreviousToken();
var i = index.length - 1;
while (ignoreToken) {
i--;
index[i] = assign({}, index[i], ignoreState);
if (ignoreToken.getNewlineCount() > 0) {
break;
}
ignoreToken = ignoreToken.getPreviousToken();
}
ignoreToken = currentToken.getNextToken();
while (ignoreToken) {
index.push(ignoreState);
if (ignoreToken.getNewlineCount() > 0) {
break;
}
ignoreToken = ignoreToken.getNextToken();
}
tokenState = assign({}, lastBlockState, ignoreState);
} else {
tokenState = lastBlockState;
}
}
} else {
tokenState = lastBlockState;
}
if (index[currentPosition]) {
tokenState = assign({}, tokenState, index[currentPosition]);
}
index[currentPosition] = tokenState;
currentPosition++;
currentToken = currentToken.getNextToken();
}
this._tokens = tokens;
this._index = index;
this._positions = positions;
};
/**
* Checks if rule whether rule is enabled for the specified element.
*
* @param {String} ruleName
* @param {Element} element
* @returns {Boolean}
*/
TokenIndex.prototype.isRuleEnabled = function(ruleName, element) {
if (!this._hasPragmas) {
return true;
}
var pos = this._tokens.indexOf(element.getFirstToken());
if (pos !== -1) {
var state = this._index[pos];
if (ruleName in state) {
return state[ruleName];
}
return state['*'];
}
return true;
};
/**
* Return calculated element location.
*
* @param {Element} element
* @returns {Object}
*/
TokenIndex.prototype.getElementLoc = function(element) {
return element.getFirstToken().__loc || {
line: 1,
column: 0
};
};
module.exports = TokenIndex;