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

Commit

Permalink
[Fix] Keywords in identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Apr 14, 2016
1 parent c82294f commit 4517263
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/js-file.js
Expand Up @@ -2,6 +2,7 @@
var cst = require('cst');
var Parser = cst.Parser;
var Token = cst.Token;
var Program = cst.types.Program;
var Fragment = cst.Fragment;
var ScopesApi = cst.api.ScopesApi;

Expand Down Expand Up @@ -49,7 +50,9 @@ var JsFile = function(params) {
this._program = parser.parse(this._source);
} catch (e) {
this._parseErrors.push(e);
// this._program = new Program([]);
this._program = new Program([
new Token('EOF', '')
]);
}

// Lazy initialization
Expand Down
6 changes: 3 additions & 3 deletions lib/string-checker.js
@@ -1,6 +1,6 @@
var Errors = require('./errors');
var JsFile = require('./js-file');
var PragmaIndex = require('./pragma-index');
var TokenIndex = require('./token-index');
var Configuration = require('./config/configuration');

var MAX_FIX_ATTEMPTS = 5;
Expand Down Expand Up @@ -164,10 +164,10 @@ StringChecker.prototype = {

var program = file.getProgram();
if (program && program.getFirstToken()) {
var pragmaIndex = new PragmaIndex(program.getFirstToken());
var tokenIndex = new TokenIndex(program.getFirstToken());
errors.filter(function(error) {
if (error.element) {
return pragmaIndex.isRuleEnabled(error.rule, error.element);
return tokenIndex.isRuleEnabled(error.rule, error.element);
} else {
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/pragma-index.js → lib/token-index.js
Expand Up @@ -44,6 +44,8 @@ function PragmaIndex(firstToken) {
* @private
*/
PragmaIndex.prototype._buildIndex = function(firstToken) {
this._hasPragmas = false;

var tokens = [];
var index = [];
var currentPosition = 0;
Expand All @@ -57,11 +59,13 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
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();
Expand Down Expand Up @@ -112,6 +116,9 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
* @returns {Boolean}
*/
PragmaIndex.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];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -63,7 +63,7 @@
"chalk": "~1.1.0",
"cli-table": "~0.3.1",
"commander": "~2.9.0",
"cst": "0.1.4",
"cst": "0.1.6",
"estraverse": "^4.1.0",
"exit": "~0.1.2",
"glob": "^5.0.1",
Expand Down
6 changes: 3 additions & 3 deletions test/specs/pragma-index.js → test/specs/token-index.js
@@ -1,5 +1,5 @@
var JsFile = require('../../lib/js-file');
var PragmaIndex = require('../../lib/pragma-index');
var TokenIndex = require('../../lib/token-index');
var assign = require('lodash').assign;
var expect = require('chai').expect;

Expand All @@ -13,10 +13,10 @@ function createJsFile(sources, options) {
}

function createPragmaIndex(file) {
return new PragmaIndex(file.getProgram().getFirstToken());
return new TokenIndex(file.getProgram().getFirstToken());
}

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

0 comments on commit 4517263

Please sign in to comment.