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

Side by Side Diff: lib/matcher.js

Issue 29804575: Noissue - Remove unnecessary typeof checks in lib/matcher.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created June 12, 2018, 4:47 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 * @param {RegExpFilter} filter 61 * @param {RegExpFilter} filter
62 */ 62 */
63 add(filter) 63 add(filter)
64 { 64 {
65 if (this.keywordByFilter.has(filter)) 65 if (this.keywordByFilter.has(filter))
66 return; 66 return;
67 67
68 // Look for a suitable keyword 68 // Look for a suitable keyword
69 let keyword = this.findKeyword(filter); 69 let keyword = this.findKeyword(filter);
70 let oldEntry = this.filterByKeyword.get(keyword); 70 let oldEntry = this.filterByKeyword.get(keyword);
71 if (typeof oldEntry == "undefined") 71 if (!oldEntry)
72 this.filterByKeyword.set(keyword, filter); 72 this.filterByKeyword.set(keyword, filter);
73 else if (oldEntry.length == 1) 73 else if (oldEntry.length == 1)
74 this.filterByKeyword.set(keyword, [oldEntry, filter]); 74 this.filterByKeyword.set(keyword, [oldEntry, filter]);
75 else 75 else
76 oldEntry.push(filter); 76 oldEntry.push(filter);
77 this.keywordByFilter.set(filter, keyword); 77 this.keywordByFilter.set(filter, keyword);
78 }, 78 },
79 79
80 /** 80 /**
81 * Removes a filter from the matcher 81 * Removes a filter from the matcher
82 * @param {RegExpFilter} filter 82 * @param {RegExpFilter} filter
83 */ 83 */
84 remove(filter) 84 remove(filter)
85 { 85 {
86 let keyword = this.keywordByFilter.get(filter); 86 let keyword = this.keywordByFilter.get(filter);
87 if (typeof keyword == "undefined") 87 if (keyword == null)
Manish Jethani 2018/06/12 04:50:27 keyword can be an empty string, so we must check a
88 return; 88 return;
89 89
90 let list = this.filterByKeyword.get(keyword); 90 let list = this.filterByKeyword.get(keyword);
91 if (list.length <= 1) 91 if (list.length <= 1)
92 this.filterByKeyword.delete(keyword); 92 this.filterByKeyword.delete(keyword);
93 else 93 else
94 { 94 {
95 let index = list.indexOf(filter); 95 let index = list.indexOf(filter);
96 if (index >= 0) 96 if (index >= 0)
97 { 97 {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 if (!candidates) 131 if (!candidates)
132 return result; 132 return result;
133 133
134 let hash = this.filterByKeyword; 134 let hash = this.filterByKeyword;
135 let resultCount = 0xFFFFFF; 135 let resultCount = 0xFFFFFF;
136 let resultLength = 0; 136 let resultLength = 0;
137 for (let i = 0, l = candidates.length; i < l; i++) 137 for (let i = 0, l = candidates.length; i < l; i++)
138 { 138 {
139 let candidate = candidates[i].substr(1); 139 let candidate = candidates[i].substr(1);
140 let filters = hash.get(candidate); 140 let filters = hash.get(candidate);
141 let count = typeof filters != "undefined" ? filters.length : 0; 141 let count = filters ? filters.length : 0;
142 if (count < resultCount || 142 if (count < resultCount ||
143 (count == resultCount && candidate.length > resultLength)) 143 (count == resultCount && candidate.length > resultLength))
144 { 144 {
145 result = candidate; 145 result = candidate;
146 resultCount = count; 146 resultCount = count;
147 resultLength = candidate.length; 147 resultLength = candidate.length;
148 } 148 }
149 } 149 }
150 return result; 150 return result;
151 }, 151 },
152 152
153 /** 153 /**
154 * Checks whether a particular filter is being matched against. 154 * Checks whether a particular filter is being matched against.
155 * @param {RegExpFilter} filter 155 * @param {RegExpFilter} filter
156 * @return {boolean} 156 * @return {boolean}
157 */ 157 */
158 hasFilter(filter) 158 hasFilter(filter)
159 { 159 {
160 return this.keywordByFilter.has(filter); 160 return this.keywordByFilter.has(filter);
161 }, 161 },
162 162
163 /** 163 /**
164 * Returns the keyword used for a filter, null for unknown filters. 164 * Returns the keyword used for a filter, null for unknown filters.
165 * @param {RegExpFilter} filter 165 * @param {RegExpFilter} filter
166 * @return {?string} 166 * @return {?string}
167 */ 167 */
168 getKeywordForFilter(filter) 168 getKeywordForFilter(filter)
169 { 169 {
170 let keyword = this.keywordByFilter.get(filter); 170 let keyword = this.keywordByFilter.get(filter);
171 return typeof keyword != "undefined" ? keyword : null; 171 return keyword != null ? keyword : null;
172 }, 172 },
173 173
174 /** 174 /**
175 * Checks whether the entries for a particular keyword match a URL 175 * Checks whether the entries for a particular keyword match a URL
176 * @param {string} keyword 176 * @param {string} keyword
177 * @param {string} location 177 * @param {string} location
178 * @param {number} typeMask 178 * @param {number} typeMask
179 * @param {string} docDomain 179 * @param {string} docDomain
180 * @param {boolean} thirdParty 180 * @param {boolean} thirdParty
181 * @param {string} sitekey 181 * @param {string} sitekey
182 * @param {boolean} specificOnly 182 * @param {boolean} specificOnly
183 * @return {?Filter} 183 * @return {?Filter}
184 */ 184 */
185 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, 185 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
186 specificOnly) 186 specificOnly)
187 { 187 {
188 let list = this.filterByKeyword.get(keyword); 188 let list = this.filterByKeyword.get(keyword);
189 if (typeof list == "undefined") 189 if (!list)
190 return null; 190 return null;
191 for (let i = 0; i < list.length; i++) 191 for (let i = 0; i < list.length; i++)
192 { 192 {
193 let filter = list[i]; 193 let filter = list[i];
194 194
195 if (specificOnly && filter.isGeneric() && 195 if (specificOnly && filter.isGeneric() &&
196 !(filter instanceof WhitelistFilter)) 196 !(filter instanceof WhitelistFilter))
197 continue; 197 continue;
198 198
199 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) 199 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 424
425 return result; 425 return result;
426 } 426 }
427 }; 427 };
428 428
429 /** 429 /**
430 * Shared CombinedMatcher instance that should usually be used. 430 * Shared CombinedMatcher instance that should usually be used.
431 * @type {CombinedMatcher} 431 * @type {CombinedMatcher}
432 */ 432 */
433 exports.defaultMatcher = new CombinedMatcher(); 433 exports.defaultMatcher = new CombinedMatcher();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld