Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/matcher.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Patch Set: Addressed Sebastian's initial feedback Created Feb. 21, 2017, 6:12 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/matcher.js
diff --git a/lib/matcher.js b/lib/matcher.js
index 69a8e2543e115a982eefcd9883b728c2589d2845..14dfe566154aab7b0b1221124ea2faf9513a7ac9 100644
--- a/lib/matcher.js
+++ b/lib/matcher.js
@@ -15,8 +15,11 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+"use strict";
+
/**
- * @fileOverview Matcher class implementing matching addresses against a list of filters.
+ * @fileOverview Matcher class implementing matching addresses against
+ * a list of filters.
*/
let {Filter, RegExpFilter, WhitelistFilter} = require("filterClasses");
@@ -34,20 +37,20 @@ exports.Matcher = Matcher;
Matcher.prototype = {
/**
* Lookup table for filters by their associated keyword
- * @type Object
+ * @type {Object}
*/
filterByKeyword: null,
/**
* Lookup table for keywords by the filter text
- * @type Object
+ * @type {Object}
*/
keywordByFilter: null,
/**
* Removes all known filters
*/
- clear: function()
+ clear()
{
this.filterByKeyword = Object.create(null);
this.keywordByFilter = Object.create(null);
@@ -57,7 +60,7 @@ Matcher.prototype = {
* Adds a filter to the matcher
* @param {RegExpFilter} filter
*/
- add: function(filter)
+ add(filter)
{
if (filter.text in this.keywordByFilter)
return;
@@ -78,7 +81,7 @@ Matcher.prototype = {
* Removes a filter from the matcher
* @param {RegExpFilter} filter
*/
- remove: function(filter)
+ remove(filter)
{
if (!(filter.text in this.keywordByFilter))
return;
@@ -103,13 +106,13 @@ Matcher.prototype = {
/**
* Chooses a keyword to be associated with the filter
- * @param {String} text text representation of the filter
- * @return {String} keyword (might be empty string)
+ * @param {Filter} filter
+ * @return {string} keyword
*/
- findKeyword: function(filter)
+ findKeyword(filter)
{
let result = "";
- let text = filter.text;
+ let {text} = filter;
if (Filter.regexpRegExp.test(text))
return result;
@@ -122,7 +125,9 @@ Matcher.prototype = {
if (text.substr(0, 2) == "@@")
text = text.substr(2);
- let candidates = text.toLowerCase().match(/[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g);
+ let candidates = text.toLowerCase().match(
+ /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g
+ );
if (!candidates)
return result;
@@ -133,7 +138,8 @@ Matcher.prototype = {
{
let candidate = candidates[i].substr(1);
let count = (candidate in hash ? hash[candidate].length : 0);
- if (count < resultCount || (count == resultCount && candidate.length > resultLength))
+ if (count < resultCount ||
+ (count == resultCount && candidate.length > resultLength))
{
result = candidate;
resultCount = count;
@@ -145,27 +151,39 @@ Matcher.prototype = {
/**
* Checks whether a particular filter is being matched against.
+ * @param {RegExpFilter} filter
+ * @return {boolean}
*/
- hasFilter: function(/**RegExpFilter*/ filter) /**Boolean*/
+ hasFilter(filter)
{
return (filter.text in this.keywordByFilter);
},
/**
* Returns the keyword used for a filter, null for unknown filters.
+ * @param {RegExpFilter} filter
+ * @return {string}
*/
- getKeywordForFilter: function(/**RegExpFilter*/ filter) /**String*/
+ getKeywordForFilter(filter)
{
if (filter.text in this.keywordByFilter)
return this.keywordByFilter[filter.text];
- else
- return null;
+ return null;
},
/**
* Checks whether the entries for a particular keyword match a URL
+ * @param {string} keyword
+ * @param {string} location
+ * @param {Number} typeMask
+ * @param {string} docDomain
+ * @param {boolean} thirdParty
+ * @param {string} sitekey
+ * @param {boolean} specificOnly
+ * @return {Filter|null}
*/
- _checkEntryMatch: function(keyword, location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
+ _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
+ specificOnly)
{
let list = this.filterByKeyword[keyword];
for (let i = 0; i < list.length; i++)
@@ -184,15 +202,17 @@ Matcher.prototype = {
/**
* Tests whether the URL matches any of the known filters
- * @param {String} location URL to be tested
+ * @param {string} location URL to be tested
* @param {number} typeMask bitmask of content / request types to match
- * @param {String} docDomain domain name of the document that loads the URL
- * @param {Boolean} thirdParty should be true if the URL is a third-party request
- * @param {String} sitekey public key provided by the document
- * @param {Boolean} specificOnly should be true if generic matches should be ignored
- * @return {RegExpFilter} matching filter or null
+ * @param {string} docDomain domain name of the document that loads the URL
+ * @param {boolean} thirdParty should be true if the URL is a third-party
+ * request
+ * @param {string} sitekey public key provided by the document
+ * @param {boolean} specificOnly should be true if generic matches should be
+ * ignored
+ * @return {RegExpFilter|null} matching filter or null
*/
- matchesAny: function(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
+ matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
{
let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
if (candidates === null)
@@ -203,7 +223,9 @@ Matcher.prototype = {
let substr = candidates[i];
if (substr in this.filterByKeyword)
{
- let result = this._checkEntryMatch(substr, location, typeMask, docDomain, thirdParty, sitekey, specificOnly);
+ let result = this._checkEntryMatch(substr, location, typeMask,
+ docDomain, thirdParty, sitekey,
+ specificOnly);
if (result)
return result;
}
@@ -228,7 +250,7 @@ exports.CombinedMatcher = CombinedMatcher;
/**
* Maximal number of matching cache entries to be kept
- * @type Number
+ * @type {Number}
*/
CombinedMatcher.maxCacheEntries = 1000;
@@ -236,32 +258,32 @@ CombinedMatcher.prototype =
{
/**
* Matcher for blocking rules.
- * @type Matcher
+ * @type {Matcher}
*/
blacklist: null,
/**
* Matcher for exception rules.
- * @type Matcher
+ * @type {Matcher}
*/
whitelist: null,
/**
* Lookup table of previous matchesAny results
- * @type Object
+ * @type {Object}
*/
resultCache: null,
/**
* Number of entries in resultCache
- * @type Number
+ * @type {Number}
*/
cacheEntries: 0,
/**
* @see Matcher#clear
*/
- clear: function()
+ clear()
{
this.blacklist.clear();
this.whitelist.clear();
@@ -271,8 +293,9 @@ CombinedMatcher.prototype =
/**
* @see Matcher#add
+ * @param {Filter} filter
*/
- add: function(filter)
+ add(filter)
{
if (filter instanceof WhitelistFilter)
this.whitelist.add(filter);
@@ -288,8 +311,9 @@ CombinedMatcher.prototype =
/**
* @see Matcher#remove
+ * @param {Filter} filter
*/
- remove: function(filter)
+ remove(filter)
{
if (filter instanceof WhitelistFilter)
this.whitelist.remove(filter);
@@ -305,55 +329,71 @@ CombinedMatcher.prototype =
/**
* @see Matcher#findKeyword
+ * @param {Filter} filter
+ * @return {string} keyword
*/
- findKeyword: function(filter)
+ findKeyword(filter)
{
if (filter instanceof WhitelistFilter)
return this.whitelist.findKeyword(filter);
- else
- return this.blacklist.findKeyword(filter);
+ return this.blacklist.findKeyword(filter);
},
/**
* @see Matcher#hasFilter
+ * @param {Filter} filter
+ * @return {boolean}
*/
- hasFilter: function(filter)
+ hasFilter(filter)
{
if (filter instanceof WhitelistFilter)
return this.whitelist.hasFilter(filter);
- else
- return this.blacklist.hasFilter(filter);
+ return this.blacklist.hasFilter(filter);
},
/**
* @see Matcher#getKeywordForFilter
+ * @param {Filter} filter
+ * @return {string} keyword
*/
- getKeywordForFilter: function(filter)
+ getKeywordForFilter(filter)
{
if (filter instanceof WhitelistFilter)
return this.whitelist.getKeywordForFilter(filter);
- else
- return this.blacklist.getKeywordForFilter(filter);
+ return this.blacklist.getKeywordForFilter(filter);
},
/**
* Checks whether a particular filter is slow
+ * @param {RegExpFilter} filter
+ * @return {boolean}
*/
- isSlowFilter: function(/**RegExpFilter*/ filter) /**Boolean*/
+ isSlowFilter(filter)
{
- let matcher = (filter instanceof WhitelistFilter ? this.whitelist : this.blacklist);
+ let matcher = (
+ filter instanceof WhitelistFilter ? this.whitelist : this.blacklist
+ );
if (matcher.hasFilter(filter))
return !matcher.getKeywordForFilter(filter);
- else
- return !matcher.findKeyword(filter);
+ return !matcher.findKeyword(filter);
},
/**
* Optimized filter matching testing both whitelist and blacklist matchers
* simultaneously. For parameters see Matcher.matchesAny().
* @see Matcher#matchesAny
+ * @param {string} location URL to be tested
+ * @param {number} typeMask bitmask of content / request types to match
+ * @param {string} docDomain domain name of the document that loads the URL
+ * @param {boolean} thirdParty should be true if the URL is a third-party
+ * request
+ * @param {string} sitekey public key provided by the document
+ * @param {boolean} specificOnly should be true if generic matches should be
+ * ignored
+ * @return {RegExpFilter|null} matching filter or null
*/
- matchesAnyInternal: function(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
+ matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey,
+ specificOnly)
{
let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
if (candidates === null)
@@ -366,26 +406,44 @@ CombinedMatcher.prototype =
let substr = candidates[i];
if (substr in this.whitelist.filterByKeyword)
{
- let result = this.whitelist._checkEntryMatch(substr, location, typeMask, docDomain, thirdParty, sitekey);
+ let result = this.whitelist._checkEntryMatch(
+ substr, location, typeMask, docDomain, thirdParty, sitekey
+ );
if (result)
return result;
}
if (substr in this.blacklist.filterByKeyword && blacklistHit === null)
- blacklistHit = this.blacklist._checkEntryMatch(substr, location, typeMask, docDomain, thirdParty, sitekey, specificOnly);
+ {
+ blacklistHit = this.blacklist._checkEntryMatch(
+ substr, location, typeMask, docDomain, thirdParty, sitekey,
+ specificOnly
+ );
+ }
}
return blacklistHit;
},
/**
* @see Matcher#matchesAny
+ * @param {string} location URL to be tested
+ * @param {number} typeMask bitmask of content / request types to match
+ * @param {string} docDomain domain name of the document that loads the URL
+ * @param {boolean} thirdParty should be true if the URL is a third-party
+ * request
+ * @param {string} sitekey public key provided by the document
+ * @param {boolean} specificOnly should be true if generic matches should be
+ * ignored
+ * @return {RegExpFilter} matching filter or null
*/
- matchesAny: function(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
+ matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
{
- let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + " " + sitekey + " " + specificOnly;
+ let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
+ " " + sitekey + " " + specificOnly;
if (key in this.resultCache)
return this.resultCache[key];
- let result = this.matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, specificOnly);
+ let result = this.matchesAnyInternal(location, typeMask, docDomain,
+ thirdParty, sitekey, specificOnly);
if (this.cacheEntries >= CombinedMatcher.maxCacheEntries)
{
@@ -398,10 +456,10 @@ CombinedMatcher.prototype =
return result;
}
-}
+};
/**
* Shared CombinedMatcher instance that should usually be used.
- * @type CombinedMatcher
+ * @type {CombinedMatcher}
*/
let defaultMatcher = exports.defaultMatcher = new CombinedMatcher();

Powered by Google App Engine
This is Rietveld