Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 4517263

Browse files
committed
[Fix] Keywords in identifiers
1 parent c82294f commit 4517263

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

lib/js-file.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var cst = require('cst');
33
var Parser = cst.Parser;
44
var Token = cst.Token;
5+
var Program = cst.types.Program;
56
var Fragment = cst.Fragment;
67
var ScopesApi = cst.api.ScopesApi;
78

@@ -49,7 +50,9 @@ var JsFile = function(params) {
4950
this._program = parser.parse(this._source);
5051
} catch (e) {
5152
this._parseErrors.push(e);
52-
// this._program = new Program([]);
53+
this._program = new Program([
54+
new Token('EOF', '')
55+
]);
5356
}
5457

5558
// Lazy initialization

lib/string-checker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Errors = require('./errors');
22
var JsFile = require('./js-file');
3-
var PragmaIndex = require('./pragma-index');
3+
var TokenIndex = require('./token-index');
44
var Configuration = require('./config/configuration');
55

66
var MAX_FIX_ATTEMPTS = 5;
@@ -164,10 +164,10 @@ StringChecker.prototype = {
164164

165165
var program = file.getProgram();
166166
if (program && program.getFirstToken()) {
167-
var pragmaIndex = new PragmaIndex(program.getFirstToken());
167+
var tokenIndex = new TokenIndex(program.getFirstToken());
168168
errors.filter(function(error) {
169169
if (error.element) {
170-
return pragmaIndex.isRuleEnabled(error.rule, error.element);
170+
return tokenIndex.isRuleEnabled(error.rule, error.element);
171171
} else {
172172
return true;
173173
}

lib/pragma-index.js renamed to lib/token-index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function PragmaIndex(firstToken) {
4444
* @private
4545
*/
4646
PragmaIndex.prototype._buildIndex = function(firstToken) {
47+
this._hasPragmas = false;
48+
4749
var tokens = [];
4850
var index = [];
4951
var currentPosition = 0;
@@ -57,11 +59,13 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
5759
var value = currentToken.value;
5860
var blockMatch = BLOCK_REGEXP.exec(value);
5961
if (blockMatch) {
62+
this._hasPragmas = true;
6063
lastBlockState = assign({}, lastBlockState, parseRuleNames(blockMatch[2], blockMatch[1] === 'en'));
6164
tokenState = lastBlockState;
6265
} else {
6366
var lineMatch = LINE_REGEXP.exec(value);
6467
if (lineMatch) {
68+
this._hasPragmas = true;
6569
var ignoreState = parseRuleNames(lineMatch[1], false);
6670
index.push(null);
6771
var ignoreToken = currentToken.getPreviousToken();
@@ -112,6 +116,9 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
112116
* @returns {Boolean}
113117
*/
114118
PragmaIndex.prototype.isRuleEnabled = function(ruleName, element) {
119+
if (!this._hasPragmas) {
120+
return true;
121+
}
115122
var pos = this._tokens.indexOf(element.getFirstToken());
116123
if (pos !== -1) {
117124
var state = this._index[pos];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"chalk": "~1.1.0",
6464
"cli-table": "~0.3.1",
6565
"commander": "~2.9.0",
66-
"cst": "0.1.4",
66+
"cst": "0.1.6",
6767
"estraverse": "^4.1.0",
6868
"exit": "~0.1.2",
6969
"glob": "^5.0.1",

test/specs/pragma-index.js renamed to test/specs/token-index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var JsFile = require('../../lib/js-file');
2-
var PragmaIndex = require('../../lib/pragma-index');
2+
var TokenIndex = require('../../lib/token-index');
33
var assign = require('lodash').assign;
44
var expect = require('chai').expect;
55

@@ -13,10 +13,10 @@ function createJsFile(sources, options) {
1313
}
1414

1515
function createPragmaIndex(file) {
16-
return new PragmaIndex(file.getProgram().getFirstToken());
16+
return new TokenIndex(file.getProgram().getFirstToken());
1717
}
1818

19-
describe('PragmaIndex', function() {
19+
describe('TokenIndex', function() {
2020
describe('isRuleEnabled', function() {
2121
it('should always return true when no control comments are used', function() {
2222
var file = createJsFile(['var x = "1";', 'x++;', 'x--;'].join('\n'));

0 commit comments

Comments
 (0)