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

Commit

Permalink
[Perf] Reduce location computations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Apr 14, 2016
1 parent 4bd3800 commit 90fae1a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
27 changes: 16 additions & 11 deletions lib/errors.js
Expand Up @@ -80,14 +80,11 @@ Errors.prototype = {
* @private
*/
_addError: function(errorInfo) {
var position = Errors.getPosition(errorInfo.element, errorInfo.offset);
this._errorList.push({
filename: this._file.getFilename(),
rule: this._currentRule,
message: this._prepareMessage(errorInfo),
element: errorInfo.element,
line: position.line,
column: position.column,
offset: errorInfo.offset,
additional: errorInfo.additional,
fixed: errorInfo.fixed
Expand Down Expand Up @@ -175,6 +172,17 @@ Errors.prototype = {
this._errorList = this._errorList.filter(filter);
},

/**
* @param {TokenIndex} tokenIndex
*/
calculateErrorLocations: function(tokenIndex) {
this._errorList.forEach(function(error) {
var pos = Errors.getPosition(error.element, error.offset, tokenIndex);
error.line = pos.line;
error.column = pos.column;
});
},

/**
* Formats error for further output.
*
Expand Down Expand Up @@ -280,9 +288,10 @@ function renderPointer(column, colorize) {
*
* @param {Object} [element]
* @param {Number} [offset]
* @param {TokenIndex} [tokenIndex]
* @return {Object}
*/
Errors.getPosition = function(element, offset) {
Errors.getPosition = function(element, offset, tokenIndex) {
if (!element) {
return EMPTY_POS;
}
Expand All @@ -295,13 +304,11 @@ Errors.getPosition = function(element, offset) {
}
}

var loc = element.getLoc();
if (!loc) {
var pos = tokenIndex ? tokenIndex.getElementLoc(element) : element.getLoc().start;
if (!pos) {
return EMPTY_POS;
}

var pos = loc.start;

if (offset === 0) {
return pos;
}
Expand Down Expand Up @@ -331,13 +338,11 @@ Errors.getPosition = function(element, offset) {
column: offset - previousOffset
};
} else {
pos = {
return {
line: pos.line,
column: pos.column + offset
};
}

return pos;
};

module.exports = Errors;
19 changes: 9 additions & 10 deletions lib/string-checker.js
Expand Up @@ -163,16 +163,15 @@ StringChecker.prototype = {
});

var program = file.getProgram();
if (program && program.getFirstToken()) {
var tokenIndex = new TokenIndex(program.getFirstToken());
errors.filter(function(error) {
if (error.element) {
return tokenIndex.isRuleEnabled(error.rule, error.element);
} else {
return true;
}
});
}
var tokenIndex = new TokenIndex(program.getFirstToken());
errors.calculateErrorLocations(tokenIndex);
errors.filter(function(error) {
if (error.element) {
return tokenIndex.isRuleEnabled(error.rule, error.element);
} else {
return true;
}
});

// sort errors list to show errors as they appear in source
errors.getErrorList().sort(function(a, b) {
Expand Down
40 changes: 36 additions & 4 deletions lib/token-index.js
Expand Up @@ -33,7 +33,7 @@ function parseRuleNames(text, enabled) {
* @param {Element} firstToken
* @constructor
*/
function PragmaIndex(firstToken) {
function TokenIndex(firstToken) {
this._buildIndex(firstToken);
}

Expand All @@ -43,18 +43,36 @@ function PragmaIndex(firstToken) {
* @param {Element} firstToken
* @private
*/
PragmaIndex.prototype._buildIndex = function(firstToken) {
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);
Expand Down Expand Up @@ -106,6 +124,7 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
}
this._tokens = tokens;
this._index = index;
this._positions = positions;
};

/**
Expand All @@ -115,7 +134,7 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
* @param {Element} element
* @returns {Boolean}
*/
PragmaIndex.prototype.isRuleEnabled = function(ruleName, element) {
TokenIndex.prototype.isRuleEnabled = function(ruleName, element) {
if (!this._hasPragmas) {
return true;
}
Expand All @@ -132,5 +151,18 @@ PragmaIndex.prototype.isRuleEnabled = function(ruleName, element) {
return true;
};

module.exports = PragmaIndex;
/**
* 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;

8 changes: 0 additions & 8 deletions test/specs/errors.js
Expand Up @@ -136,8 +136,6 @@ describe('errors', function() {
var error = errors.getErrorList()[0];

expect(error.rule).to.equal('anyRule');
expect(error.line).to.equal(1);
expect(error.column).to.equal(0);
});
});

Expand All @@ -157,8 +155,6 @@ describe('errors', function() {
var error = errors.getErrorList()[0];

expect(error.rule).to.equal('anyRule');
expect(error.line).to.equal(1);
expect(error.column).to.equal(0);
expect(error.additional).to.equal('test');
});
});
Expand Down Expand Up @@ -226,11 +222,7 @@ describe('errors', function() {
errors.stripErrorList(2);
expect(errors).to.have.error.count.equal(2);
expect(errors.getErrorList()[0].message).to.equal('disallowQuotedKeysInObjects: msg1');
expect(errors.getErrorList()[0].line).to.equal(1);
expect(errors.getErrorList()[0].column).to.equal(0);
expect(errors.getErrorList()[1].message).to.equal('disallowQuotedKeysInObjects: msg2');
expect(errors.getErrorList()[1].line).to.equal(1);
expect(errors.getErrorList()[1].column).to.equal(0);
});
});
});

0 comments on commit 90fae1a

Please sign in to comment.