Skip to content

Commit

Permalink
requireDictionaryWords: Add inline configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonrayhamilton committed Apr 4, 2015
1 parent 1cbba75 commit 7bab2ea
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 9 deletions.
116 changes: 107 additions & 9 deletions lib/rules/require-dictionary-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,117 @@ module.exports.prototype = {
check: function(file, errors) {
var _this = this;

function check(wordDictionaries, nameDictionaries, name, start) {
if (!dictionariesHaveWord(nameDictionaries, name)) {
checkWords(
wordDictionaries,
_this._excludedWords,
errors,
name,
start
);
var allowedIndex = [];

function addToAllowedIndex(allowed, rule, namesStr, line) {
namesStr.split(',').forEach(function(name) {
name = name.trim();

if (!name) {
return;
}

allowedIndex.push({
rule: rule,
name: name,
allowed: allowed,
line: line
});
});
}

function buildAllowedIndex() {
var comments = file.getComments();
var commentRe = new RegExp([
'(jscs\\s*:\\s*(allow|disallow)(',
[
'Words',
'WordsInIdentifiers',
'WordsInProperties',
'Names',
'NamesAsIdentifiers',
'NamesAsProperties'
].join('|'),
'))\\s+(.*)',
].join(''));

comments.forEach(function(comment) {
var allowed;
var parsed = commentRe.exec(comment.value.trim());

if (!parsed || parsed.index !== 0) {
return;
}

allowed = parsed[2] === 'allow';
var rule = 'allow' + parsed[3];
var namesStr = parsed[4];
addToAllowedIndex(allowed, rule, namesStr, comment.loc.start.line);
}, this);
}

buildAllowedIndex();

function isAllowed(context, as, name, line) {
var allowed = false;
allowedIndex.some(function(region) {
// once the comment we're inspecting occurs after the location of the error,
// no longer check for whether the state is allowed or disallow
if (region.line > line) {
return true;
}

var contextRe = new RegExp([
'^allow',
as === 'name' ? 'Names(|As' : 'Words(|In',
context === 'identifier' ? 'Identifiers)' : 'Properties)',
'$'
].join(''));

if (contextRe.test(region.rule)) {
if (as === 'name' && /^allowNames/.test(region.rule)) {
if (region.name === name) {
allowed = region.allowed;
}
} else if (as === 'word' && /^allowWords/.test(region.rule)) {
if (name.match(reWords).indexOf(region.name) > -1) {
allowed = region.allowed;
}
}
}
});

return allowed;
}

function isAllowedName(context, name, line) {
return isAllowed(context, 'name', name, line);
}

function isAllowedWord(context, name, line) {
return isAllowed(context, 'word', name, line);
}

function check(context, wordDictionaries, nameDictionaries, name, start) {
if (
isAllowedName(context, name, start.line) ||
dictionariesHaveWord(nameDictionaries, name) ||
isAllowedWord(context, name, start.line)
) {
return;
}
checkWords(
wordDictionaries,
_this._excludedWords,
errors,
name,
start
);
}

function checkIdentifier(name, start) {
check(
'identifier',
_this._identifierWordDictionaries,
_this._identifierNameDictionaries,
name,
Expand All @@ -319,6 +416,7 @@ module.exports.prototype = {

function checkProperty(name, start) {
check(
'property',
_this._propertyWordDictionaries,
_this._propertyNameDictionaries,
name,
Expand Down
102 changes: 102 additions & 0 deletions test/specs/rules/require-dictionary-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,108 @@ describe('rules/require-dictionary-words', function() {
checker.configure({ plugins: ['./lib/index.js'] });
});

describe('inline configuration', function() {
beforeEach(function() {
checker.configure({ requireDictionaryWords: true });
});

it('should later allow and later disallow inline', function() {
assert(checker.checkString([
'asdf = 1',
'// jscs:allowWords asdf, jkl',
'asdf = 1',
'// jscs:disallowWords asdf, jkl',
'asdf = 1',
].join('\n')).getErrorCount() === 2);

assert(checker.checkString([
'asdf = 1',
'jkl = 1',
'// jscs:allowWords asdf, jkl',
'asdf = 1',
'jkl = 1',
'// jscs:disallowWords asdf, jkl',
'asdf = 1',
'jkl = 1'
].join('\n')).getErrorCount() === 4);
});

it('should allow words inline', function() {
assert(checker.checkString([
'// jscs:allowWords asdf, jkl',
'asdf = 1',
'asdfAsdf = 1',
'object.jkl = 1',
'object.jklJkl = 1'
].join('\n')).isEmpty());
});

it('should allow words in identifiers inline', function() {
assert(checker.checkString([
'// jscs:allowWordsInIdentifiers asdf, jkl',
'asdf = 1',
'asdfAsdf = 1'
].join('\n')).isEmpty());
assert(checker.checkString([
'// jscs:allowWordsInIdentifiers asdf, jkl',
'object.jkl = 1',
'object.jklJkl = 1'
].join('\n')).getErrorCount() === 3);
});

it('should allow words in properties inline', function() {
assert(checker.checkString([
'// jscs:allowWordsInProperties asdf, jkl',
'object.jkl = 1',
'object.jklJkl = 1'
].join('\n')).isEmpty());
assert(checker.checkString([
'// jscs:allowWordsInProperties asdf, jkl',
'asdf = 1',
'asdfAsdf = 1'
].join('\n')).getErrorCount() === 3);
});

it('should allow names inline', function() {
assert(checker.checkString([
'// jscs:allowNames asdf, jkl',
'asdf = 1',
'object.jkl = 1'
].join('\n')).isEmpty());
assert(checker.checkString([
'// jscs:allowNames asdf, jkl',
'asdfAsdf = 1',
'object.jklJkl = 1'
].join('\n')).getErrorCount() === 4);
});

it('should allow names as identifiers inline', function() {
assert(checker.checkString([
'// jscs:allowNamesAsIdentifiers asdf, jkl',
'asdf = 1'
].join('\n')).isEmpty());
assert(checker.checkString([
'// jscs:allowNamesAsIdentifiers asdf, jkl',
'asdfAsdf = 1',
'object.jkl = 1',
'object.jklJkl = 1'
].join('\n')).getErrorCount() === 5);
});

it('should allow names as properties inline', function() {
assert(checker.checkString([
'// jscs:allowNamesAsProperties asdf, jkl',
'object.jkl = 1'
].join('\n')).isEmpty());
assert(checker.checkString([
'// jscs:allowNamesAsProperties asdf, jkl',
'asdf = 1',
'asdfAsdf = 1',
'object.jklJkl = 1'
].join('\n')).getErrorCount() === 5);
});
});

describe('true', function() {
beforeEach(function() {
checker.configure({ requireDictionaryWords: true });
Expand Down

0 comments on commit 7bab2ea

Please sign in to comment.