Skip to content

Commit 301c3c8

Browse files
requireDictionaryWords: Refactor isAllowed
1 parent 7bab2ea commit 301c3c8

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

lib/rules/require-dictionary-words.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ module.exports.prototype = {
347347

348348
buildAllowedIndex();
349349

350-
function isAllowed(context, as, name, line) {
350+
function isAllowed(context, name, line) {
351351
var allowed = false;
352352
allowedIndex.some(function(region) {
353353
// once the comment we're inspecting occurs after the location of the error,
@@ -356,42 +356,32 @@ module.exports.prototype = {
356356
return true;
357357
}
358358

359-
var contextRe = new RegExp([
360-
'^allow',
361-
as === 'name' ? 'Names(|As' : 'Words(|In',
362-
context === 'identifier' ? 'Identifiers)' : 'Properties)',
363-
'$'
364-
].join(''));
365-
366-
if (contextRe.test(region.rule)) {
367-
if (as === 'name' && /^allowNames/.test(region.rule)) {
368-
if (region.name === name) {
369-
allowed = region.allowed;
370-
}
371-
} else if (as === 'word' && /^allowWords/.test(region.rule)) {
372-
if (name.match(reWords).indexOf(region.name) > -1) {
373-
allowed = region.allowed;
374-
}
359+
var inverseContextRe = new RegExp(
360+
(context === 'identifier' ? 'Properties' : 'Identifiers') + '$'
361+
);
362+
363+
if (inverseContextRe.test(region.rule)) {
364+
return;
365+
}
366+
367+
if (/^allowNames/.test(region.rule)) {
368+
if (region.name === name) {
369+
allowed = region.allowed;
370+
}
371+
} else if (/^allowWords/.test(region.rule)) {
372+
if (name.match(reWords).indexOf(region.name) > -1) {
373+
allowed = region.allowed;
375374
}
376375
}
377376
});
378377

379378
return allowed;
380379
}
381380

382-
function isAllowedName(context, name, line) {
383-
return isAllowed(context, 'name', name, line);
384-
}
385-
386-
function isAllowedWord(context, name, line) {
387-
return isAllowed(context, 'word', name, line);
388-
}
389-
390381
function check(context, wordDictionaries, nameDictionaries, name, start) {
391382
if (
392-
isAllowedName(context, name, start.line) ||
393-
dictionariesHaveWord(nameDictionaries, name) ||
394-
isAllowedWord(context, name, start.line)
383+
isAllowed(context, name, start.line) ||
384+
dictionariesHaveWord(nameDictionaries, name)
395385
) {
396386
return;
397387
}

0 commit comments

Comments
 (0)