Skip to content

Commit

Permalink
requireDictionaryWords: Refactor isAllowed
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonrayhamilton committed Apr 4, 2015
1 parent 7bab2ea commit 301c3c8
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions lib/rules/require-dictionary-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ module.exports.prototype = {

buildAllowedIndex();

function isAllowed(context, as, name, line) {
function isAllowed(context, name, line) {
var allowed = false;
allowedIndex.some(function(region) {
// once the comment we're inspecting occurs after the location of the error,
Expand All @@ -356,42 +356,32 @@ module.exports.prototype = {
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;
}
var inverseContextRe = new RegExp(
(context === 'identifier' ? 'Properties' : 'Identifiers') + '$'
);

if (inverseContextRe.test(region.rule)) {
return;
}

if (/^allowNames/.test(region.rule)) {
if (region.name === name) {
allowed = region.allowed;
}
} else if (/^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)
isAllowed(context, name, start.line) ||
dictionariesHaveWord(nameDictionaries, name)
) {
return;
}
Expand Down

0 comments on commit 301c3c8

Please sign in to comment.