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

Unified Diff: lib/matcher.js

Issue 29869571: Issue 6741 - Use ES2015 classes in lib/matcher.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Aug. 30, 2018, 3:53 p.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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/matcher.js
===================================================================
--- a/lib/matcher.js
+++ b/lib/matcher.js
@@ -19,47 +19,45 @@
/**
* @fileOverview Matcher class implementing matching addresses against
* a list of filters.
*/
const {Filter, WhitelistFilter} = require("./filterClasses");
-/**
- * Blacklist/whitelist filter matching
- * @constructor
- */
-function Matcher()
+class Matcher
{
- this.clear();
-}
-exports.Matcher = Matcher;
+ /**
Manish Jethani 2018/08/31 13:01:34 I think this rather sounds like the description of
Jon Sonesen 2018/09/02 16:43:20 Done.
+ * Blacklist/whitelist filter matching
+ */
+ constructor()
+ {
+ /**
+ * Lookup table for filters by their associated keyword
+ * @type {Map.<string,(Filter|Filter[])>}
+ */
+ this.filterByKeyword = null;
-Matcher.prototype = {
- /**
- * Lookup table for filters by their associated keyword
- * @type {Map.<string,(Filter|Filter[])>}
- */
- filterByKeyword: null,
-
- /**
- * Lookup table for keywords by the filter
- * @type {Map.<Filter,string>}
- */
- keywordByFilter: null,
+ /**
+ * Lookup table for keywords by the filter
+ * @type {Map.<Filter,string>}
+ */
+ this.keywordByFilter = null;
+ this.clear();
Manish Jethani 2018/08/31 13:01:35 Let's leave a line before this.clear(). Actually
Jon Sonesen 2018/09/02 16:43:20 Done.
+ }
/**
* Removes all known filters
*/
clear()
{
this.filterByKeyword = new Map();
Manish Jethani 2018/08/31 13:01:35 Since these are Map objects now, we can just call
Jon Sonesen 2018/09/02 16:43:19 Done.
this.keywordByFilter = new Map();
- },
+ }
/**
* Adds a filter to the matcher
* @param {RegExpFilter} filter
*/
add(filter)
{
if (this.keywordByFilter.has(filter))
@@ -70,17 +68,17 @@
let oldEntry = this.filterByKeyword.get(keyword);
if (typeof oldEntry == "undefined")
this.filterByKeyword.set(keyword, filter);
else if (oldEntry.length == 1)
this.filterByKeyword.set(keyword, [oldEntry, filter]);
else
oldEntry.push(filter);
this.keywordByFilter.set(filter, keyword);
- },
+ }
/**
* Removes a filter from the matcher
* @param {RegExpFilter} filter
*/
remove(filter)
{
let keyword = this.keywordByFilter.get(filter);
@@ -97,17 +95,17 @@
{
list.splice(index, 1);
if (list.length == 1)
this.filterByKeyword.set(keyword, list[0]);
}
}
this.keywordByFilter.delete(filter);
- },
+ }
/**
* Chooses a keyword to be associated with the filter
* @param {Filter} filter
* @return {string} keyword or an empty string if no keyword could be found
Manish Jethani 2018/08/31 13:01:34 Let's s/@return/@returns/ here and throughout the
Jon Sonesen 2018/09/02 16:43:20 Done.
*/
findKeyword(filter)
{
@@ -143,38 +141,38 @@
(count == resultCount && candidate.length > resultLength))
{
result = candidate;
resultCount = count;
resultLength = candidate.length;
}
}
return result;
- },
+ }
/**
* Checks whether a particular filter is being matched against.
* @param {RegExpFilter} filter
* @return {boolean}
*/
hasFilter(filter)
{
return this.keywordByFilter.has(filter);
- },
+ }
/**
* Returns the keyword used for a filter, null for unknown filters.
* @param {RegExpFilter} filter
* @return {?string}
*/
getKeywordForFilter(filter)
{
let keyword = this.keywordByFilter.get(filter);
return typeof keyword != "undefined" ? keyword : null;
- },
+ }
/**
* Checks whether the entries for a particular keyword match a URL
* @param {string} keyword
* @param {string} location
* @param {number} typeMask
* @param {string} docDomain
Manish Jethani 2018/08/31 13:01:34 docDomain, thirdParty, sitekey, and specificOnly a
Jon Sonesen 2018/09/02 16:43:20 Done.
* @param {boolean} thirdParty
@@ -195,17 +193,17 @@
if (specificOnly && filter.isGeneric() &&
!(filter instanceof WhitelistFilter))
continue;
if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
return filter;
}
return null;
- },
+ }
/**
* Tests whether the URL matches any of the known filters
* @param {string} location
* URL to be tested
* @param {number} typeMask
* bitmask of content / request types to match
* @param {string} docDomain
Manish Jethani 2018/08/31 13:01:35 See comment above about optional parameters.
Jon Sonesen 2018/09/02 16:43:20 Done.
@@ -231,146 +229,143 @@
docDomain, thirdParty, sitekey,
specificOnly);
if (result)
return result;
}
return null;
}
-};
+}
Manish Jethani 2018/08/31 13:01:34 Let's leave a blank line after the class definitio
Jon Sonesen 2018/09/02 16:43:20 Done.
+exports.Matcher = Matcher;
-/**
- * Combines a matcher for blocking and exception rules, automatically sorts
- * rules into two Matcher instances.
- * @constructor
- * @augments Matcher
- */
-function CombinedMatcher()
-{
- this.blacklist = new Matcher();
- this.whitelist = new Matcher();
- this.resultCache = new Map();
-}
-exports.CombinedMatcher = CombinedMatcher;
+
-/**
- * Maximal number of matching cache entries to be kept
- * @type {number}
- */
-CombinedMatcher.maxCacheEntries = 1000;
-
-CombinedMatcher.prototype =
+class CombinedMatcher
{
/**
- * Matcher for blocking rules.
- * @type {Matcher}
+ * Combines a matcher for blocking and exception rules, automatically sorts
Manish Jethani 2018/08/31 13:01:34 Let's move this to above the class definition.
+ * rules into two Matcher instances.
Manish Jethani 2018/08/31 13:01:35 Let's change `Matcher` here to `{@link Matcher}` s
+ * @augments Matcher
Manish Jethani 2018/08/31 13:01:34 Let's remove @arguments here, it's outdated.
*/
- blacklist: null,
+ constructor()
+ {
+ /**
+ * Maximal number of matching cache entries to be kept
+ * @type {number}
+ */
+ this.maxCacheEntries = 1000;
- /**
- * Matcher for exception rules.
- * @type {Matcher}
- */
- whitelist: null,
+ /**
+ * Matcher for blocking rules.
+ * @type {Matcher}
+ */
+ this.blacklist = new Matcher();
- /**
- * Lookup table of previous matchesAny results
- * @type {Map.<string,Filter>}
- */
- resultCache: null,
+ /**
+ * Matcher for exception rules.
+ * @type {Matcher}
+ */
+ this.whitelist = new Matcher();
+
+ /**
+ * Lookup table of previous matchesAny results
Manish Jethani 2018/08/31 13:01:35 Let's make this `{@link Matcher#matchesAny}`
Jon Sonesen 2018/09/02 16:43:20 Done.
+ * @type {Map.<string,Filter>}
+ */
+ this.resultCache = new Map();
+ }
/**
* @see Matcher#clear
*/
clear()
{
this.blacklist.clear();
this.whitelist.clear();
this.resultCache.clear();
- },
+ }
/**
* @see Matcher#add
* @param {Filter} filter
*/
add(filter)
{
if (filter instanceof WhitelistFilter)
this.whitelist.add(filter);
else
this.blacklist.add(filter);
this.resultCache.clear();
- },
+ }
/**
* @see Matcher#remove
* @param {Filter} filter
*/
remove(filter)
{
if (filter instanceof WhitelistFilter)
this.whitelist.remove(filter);
else
this.blacklist.remove(filter);
this.resultCache.clear();
- },
+ }
/**
* @see Matcher#findKeyword
* @param {Filter} filter
* @return {string} keyword
*/
findKeyword(filter)
{
if (filter instanceof WhitelistFilter)
return this.whitelist.findKeyword(filter);
return this.blacklist.findKeyword(filter);
- },
+ }
/**
* @see Matcher#hasFilter
* @param {Filter} filter
* @return {boolean}
*/
hasFilter(filter)
{
if (filter instanceof WhitelistFilter)
return this.whitelist.hasFilter(filter);
return this.blacklist.hasFilter(filter);
- },
+ }
/**
* @see Matcher#getKeywordForFilter
* @param {Filter} filter
* @return {string} keyword
*/
getKeywordForFilter(filter)
{
if (filter instanceof WhitelistFilter)
return this.whitelist.getKeywordForFilter(filter);
return this.blacklist.getKeywordForFilter(filter);
- },
+ }
/**
* Checks whether a particular filter is slow
* @param {RegExpFilter} filter
* @return {boolean}
*/
isSlowFilter(filter)
{
let matcher = (
filter instanceof WhitelistFilter ? this.whitelist : this.blacklist
);
if (matcher.hasFilter(filter))
return !matcher.getKeywordForFilter(filter);
return !matcher.findKeyword(filter);
- },
+ }
/**
* Optimized filter matching testing both whitelist and blacklist matchers
* simultaneously. For parameters see
{@link Matcher#matchesAny Matcher.matchesAny()}.
* @see Matcher#matchesAny
* @inheritdoc
*/
@@ -395,17 +390,17 @@
{
blacklistHit = this.blacklist._checkEntryMatch(
substr, location, typeMask, docDomain, thirdParty, sitekey,
specificOnly
);
}
}
return blacklistHit;
- },
+ }
/**
* @see Matcher#matchesAny
* @inheritdoc
*/
matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
{
let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
@@ -420,15 +415,16 @@
if (this.resultCache.size >= CombinedMatcher.maxCacheEntries)
this.resultCache.clear();
this.resultCache.set(key, result);
return result;
}
-};
+}
Manish Jethani 2018/08/31 13:01:34 Let's leave a blank line here.
Jon Sonesen 2018/09/02 16:43:20 Done.
+exports.CombinedMatcher = CombinedMatcher;
/**
* Shared CombinedMatcher instance that should usually be used.
Manish Jethani 2018/08/31 13:01:35 Let's make this `{@link CombinedMatcher}`
Jon Sonesen 2018/09/02 16:43:20 Done.
* @type {CombinedMatcher}
*/
exports.defaultMatcher = new CombinedMatcher();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld