This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
377 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
var texts = [ | ||
'Illegal space before "%s" keyword', | ||
'Should be zero spaces instead of %d, before "%s" keyword' | ||
]; | ||
|
||
var defaultKeywords = require('../utils').spacedKeywords; | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports.prototype = { | ||
|
||
configure: function(keywords) { | ||
assert( | ||
Array.isArray(keywords) || keywords === true, | ||
'disallowSpaceAfterKeywords option requires array or true value'); | ||
|
||
if (keywords === true) { | ||
keywords = defaultKeywords; | ||
} | ||
|
||
this._keywordIndex = {}; | ||
for (var i = 0, l = keywords.length; i < l; i++) { | ||
this._keywordIndex[keywords[i]] = true; | ||
} | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'disallowSpaceBeforeKeywords'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
var keywordIndex = this._keywordIndex; | ||
|
||
function getCommentIfExists(start, end) { | ||
return file.getComments().filter(function(comment) { | ||
return start <= comment.range[0] && end >= comment.range[1]; | ||
})[0]; | ||
} | ||
|
||
file.iterateTokensByType(['Keyword'], function(token, i, tokens) { | ||
if (keywordIndex[token.value]) { | ||
var prevToken = tokens[i - 1]; | ||
if (!prevToken) { | ||
return; | ||
} | ||
|
||
var comment = getCommentIfExists(prevToken.range[1], token.range[0]); | ||
prevToken = comment || prevToken; | ||
|
||
var diff = token.range[0] - prevToken.range[1]; | ||
|
||
if (prevToken.loc.end.line === token.loc.start.line && diff !== 0) { | ||
if (prevToken.type !== 'Punctuator' || prevToken.value !== ';') { | ||
errors.add( | ||
util.format.apply(null, | ||
diff === 1 ? | ||
[texts[0], token.value] : | ||
[texts[1], diff, token.value] | ||
), | ||
token.loc.start.line, | ||
token.loc.start.column | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
var texts = [ | ||
'Missing space before "%s" keyword', | ||
'Should be one space instead of %d, before "%s" keyword' | ||
]; | ||
|
||
var defaultKeywords = require('../utils').spacedKeywords; | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports.prototype = { | ||
|
||
configure: function(keywords) { | ||
assert( | ||
Array.isArray(keywords) || keywords === true, | ||
'requireSpaceAfterKeywords option requires array or true value'); | ||
|
||
if (keywords === true) { | ||
keywords = defaultKeywords; | ||
} | ||
|
||
this._keywordIndex = {}; | ||
for (var i = 0, l = keywords.length; i < l; i++) { | ||
this._keywordIndex[keywords[i]] = true; | ||
} | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'requireSpaceBeforeKeywords'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
var keywordIndex = this._keywordIndex; | ||
|
||
function getCommentIfExists(start, end) { | ||
return file.getComments().filter(function(comment) { | ||
return start <= comment.range[0] && end >= comment.range[1]; | ||
})[0]; | ||
} | ||
|
||
file.iterateTokensByType(['Keyword'], function(token, i, tokens) { | ||
if (keywordIndex[token.value]) { | ||
var prevToken = tokens[i - 1]; | ||
if (!prevToken) { | ||
return; | ||
} | ||
|
||
var comment = getCommentIfExists(prevToken.range[1], token.range[0]); | ||
prevToken = comment || prevToken; | ||
|
||
var diff = token.range[0] - prevToken.range[1]; | ||
|
||
if (prevToken.loc.end.line === token.loc.start.line && diff !== 1) { | ||
if (prevToken.type !== 'Punctuator' || prevToken.value !== ';') { | ||
errors.add( | ||
util.format.apply(null, | ||
diff === 0 ? | ||
[texts[0], token.value] : | ||
[texts[1], diff, token.value] | ||
), | ||
token.loc.start.line, | ||
token.loc.start.column | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var Checker = require('../../lib/checker'); | ||
var assert = require('assert'); | ||
|
||
describe('rules/dissalow-space-before-keywords', function() { | ||
var checker; | ||
|
||
beforeEach(function() { | ||
checker = new Checker(); | ||
checker.registerDefaultRules(); | ||
}); | ||
|
||
it('should report illegal space before keyword', function() { | ||
checker.configure({ disallowSpaceBeforeKeywords: ['else'] }); | ||
|
||
var errors = checker.checkString('if (true) {\n} else { x++; }'); | ||
var error = errors.getErrorList()[0]; | ||
|
||
assert(errors.getErrorCount() === 1); | ||
assert(errors.explainError(error).indexOf('Illegal space before "else" keyword') === 0); | ||
}); | ||
|
||
it('should not report no space before keyword', function() { | ||
checker.configure({ disallowSpaceBeforeKeywords: ['else'] }); | ||
|
||
assert(checker.checkString( | ||
'if (x) {\n' + | ||
'x++;\n' + | ||
'}else {\n' + | ||
'x--;\n' + | ||
'}' | ||
).isEmpty()); | ||
}); | ||
|
||
it('should show different error if there is more than one space', function() { | ||
checker.configure({ disallowSpaceBeforeKeywords: ['else'] }); | ||
|
||
var errors = checker.checkString('if (true) {\n} else { x++; }'); | ||
var error = errors.getErrorList()[0]; | ||
|
||
assert(errors.explainError(error).indexOf('Should be zero spaces instead of 2, before "else" keyword') === 0); | ||
}); | ||
|
||
it('should not trigger error for comments', function() { | ||
checker.configure({ disallowSpaceBeforeKeywords: ['else'] }); | ||
assert(checker.checkString('if (true) {\n} /**/else { x++; }').isEmpty()); | ||
}); | ||
|
||
it('should report on all possible ES3 keywords if a value of true is supplied', function() { | ||
checker.configure({ disallowSpaceBeforeKeywords: true }); | ||
|
||
var errors = checker.checkString('if (true) {\n} else { x++; }'); | ||
var error = errors.getErrorList()[0]; | ||
assert(errors.getErrorCount() === 1); | ||
assert(errors.explainError(error).indexOf('Illegal space before "else" keyword') === 0); | ||
|
||
errors = checker.checkString('/**/ if (true) {\n} else { x++; }'); | ||
error = errors.getErrorList()[0]; | ||
assert(errors.getErrorCount() === 1); | ||
assert(errors.explainError(error).indexOf('Illegal space before "else" keyword') === 0); | ||
|
||
errors = checker.checkString('do {\nx++;\n} while (x < 5)'); | ||
error = errors.getErrorList()[0]; | ||
assert(errors.getErrorCount() === 1); | ||
assert(errors.explainError(error).indexOf('Illegal space before "while" keyword') === 0); | ||
|
||
errors = checker.checkString('try {\nx++;\n} catch (e) {}'); | ||
error = errors.getErrorList()[0]; | ||
assert(errors.getErrorCount() === 1); | ||
assert(errors.explainError(error).indexOf('Illegal space before "catch" keyword') === 0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.