Skip to content

Commit ed9edec

Browse files
pipboy96Hainish
authored andcommitted
Minor style fixes to cschanaj script (EFForg#18063)
1 parent 10c9257 commit ed9edec

1 file changed

Lines changed: 63 additions & 68 deletions

File tree

utils/trivialize-rules/trivialize-targets.js

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
'use strict';
22

3-
let util = require('util');
4-
let path = require('path');
5-
let xml2js = require('xml2js');
3+
const util = require('util');
4+
const path = require('path');
5+
const xml2js = require('xml2js');
66

7-
let fs = require('graceful-fs');
8-
let readdir = util.promisify(fs.readdir);
9-
let readFile = util.promisify(fs.readFile);
10-
let parseString = util.promisify(xml2js.parseString);
7+
const fs = require('graceful-fs');
8+
const readdir = util.promisify(fs.readdir);
9+
const readFile = util.promisify(fs.readFile);
10+
const parseString = util.promisify(xml2js.parseString);
1111

12-
let chalk = require('chalk');
13-
let validUrl = require('valid-url');
14-
let escapeStringRegexp = require('escape-string-regexp');
15-
let { explodeRegExp, UnsupportedRegExp } = require('./explode-regexp');
12+
const chalk = require('chalk');
13+
const validUrl = require('valid-url');
14+
const escapeStringRegexp = require('escape-string-regexp');
15+
const { explodeRegExp, UnsupportedRegExp } = require('./explode-regexp');
1616

1717
const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
1818

1919
(async () => {
20-
let filenames = (await readdir(rulesDir)).filter(fn => fn.endsWith('.xml'));
21-
let filePromises = filenames.map(async filename => {
22-
function createTag(tagName, colour, print) {
20+
const filenames = (await readdir(rulesDir)).filter(fn => fn.endsWith('.xml'));
21+
const filePromises = filenames.map(async filename => {
22+
const createTag = (tagName, colour, print) => {
2323
return (strings, ...values) => {
2424
let result = `[${tagName}] ${chalk.bold(filename)}: ${strings[0]}`;
2525
for (let i = 1; i < strings.length; i++) {
@@ -32,17 +32,17 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
3232
}
3333
print(colour(result));
3434
};
35-
}
35+
};
3636

37-
let warn = createTag('WARN', chalk.yellow, console.warn);
38-
let info = createTag('INFO', chalk.green, console.info);
39-
let fail = createTag('FAIL', chalk.red, console.error);
37+
const warn = createTag('WARN', chalk.yellow, console.warn);
38+
const info = createTag('INFO', chalk.green, console.info);
39+
const fail = createTag('FAIL', chalk.red, console.error);
4040

4141
let content = await readFile(path.join(rulesDir, filename), 'utf8');
42-
let { ruleset } = await parseString(content);
42+
const { ruleset } = await parseString(content);
4343

44-
let rules = ruleset.rule.map(rule => rule.$);
45-
let targets = ruleset.target.map(target => target.$.host);
44+
const rules = ruleset.rule.map(rule => rule.$);
45+
const targets = ruleset.target.map(target => target.$.host);
4646

4747
// make sure ruleset contains at least one left wildcard targets
4848
if (!targets.some(target => target.startsWith('*.'))) {
@@ -59,20 +59,20 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
5959
return;
6060
}
6161

62-
let ruleToIsSimpleMap = new Map();
63-
let ruleToIsSnappingMap = new Map();
62+
const ruleToIsSimpleMap = new Map();
63+
const ruleToIsSnappingMap = new Map();
6464

65-
let targetToSupportedExplodedDomainsMap = new Map();
65+
const targetToSupportedExplodedDomainsMap = new Map();
6666

67-
let explodedDomains = new Set();
68-
let unsupportedExplodedDomains = new Set();
69-
let unusedTargets = new Set();
67+
const explodedDomains = new Set();
68+
const unsupportedExplodedDomains = new Set();
69+
const unusedTargets = new Set();
7070

7171
// (1) We check if all rules can be exploded to valid urls
7272
// (a) if true, continue to (2)
7373
// (b) if we cannot trivialize targets for this ruleset, skip
74-
function isExplosiveRewrite(rule) {
75-
let explodedUrls = new Array();
74+
const isExplosiveRewrite = rule => {
75+
const explodedUrls = new Array();
7676

7777
try {
7878
explodeRegExp(rule.from, url => explodedUrls.push(url));
@@ -93,8 +93,8 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
9393
let isSimpleToAllExplodedUrls = true;
9494
let isSnappingToSomeExplodedUrls = false;
9595

96-
for (let url of explodedUrls) {
97-
let { protocol, hostname, pathname } = new URL(url);
96+
for (const url of explodedUrls) {
97+
const { protocol, hostname, pathname } = new URL(url);
9898

9999
// if a rule do not rewrite all path for any URL, it is not a simple rule
100100
// i.e. a rule is simple only if it rewrite all path for all URLs
@@ -119,7 +119,7 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
119119
ruleToIsSimpleMap.set(rule, isSimpleToAllExplodedUrls);
120120
ruleToIsSnappingMap.set(rule, isSnappingToSomeExplodedUrls);
121121
return true;
122-
}
122+
};
123123

124124
if (!rules.every(isExplosiveRewrite)) {
125125
return;
@@ -130,7 +130,7 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
130130
// (b) some exploded domains are not covered by the targets,
131131
// it is not safe to rewrite ruleset to include the
132132
// exploded domains. skipping
133-
function isSupported(domain) {
133+
const isSupported = domain => {
134134
if (targets.includes(domain)) {
135135
// do not map non-wildcard targets to exploded domains
136136
// otherwise, it will introduce unnecessary rewrites
@@ -139,22 +139,23 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
139139
}
140140

141141
// this part follows the implementation in rules.js
142-
let segments = domain.split('.');
142+
const segments = domain.split('.');
143143
for (let i = 1; i <= segments.length - 2; ++i) {
144-
let tmp = '*.' + segments.slice(i, segments.length).join('.');
144+
const tmp = '*.' + segments.slice(i, segments.length).join('.');
145145
if (targets.includes(tmp)) {
146146
targetToSupportedExplodedDomainsMap.get(tmp).push(domain);
147147
return true;
148148
}
149149
}
150150
unsupportedExplodedDomains.add(domain);
151151
return false;
152-
}
152+
};
153153

154154
// initially, assume each target doesn't support any exploded domain
155-
targets.forEach(target =>
156-
targetToSupportedExplodedDomainsMap.set(target, [])
157-
);
155+
for (const target of targets) {
156+
targetToSupportedExplodedDomainsMap.set(target, []);
157+
}
158+
158159
if (![...explodedDomains].every(domain => isSupported(domain))) {
159160
warn`ruleset rewrites domains ${[
160161
...unsupportedExplodedDomains
@@ -167,17 +168,15 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
167168
// (b) if some targets are not covered by any rewrites, this
168169
// doesn't affect our works trivializing the targets, but
169170
// we should give a warning and then proceed to (4)
170-
targets.forEach(target => {
171-
let supportedExplodedDomains = targetToSupportedExplodedDomainsMap.get(
172-
target
173-
);
174-
if (supportedExplodedDomains && supportedExplodedDomains.length == 0) {
171+
for (const target of targets) {
172+
const supportedExplodedDomains = targetToSupportedExplodedDomainsMap.get(target);
173+
if (supportedExplodedDomains && supportedExplodedDomains.length === 0) {
175174
// prepare the warning message here
176175
unusedTargets.add(target);
177176
// make sure we don't remove these targets when performing rewrites
178177
targetToSupportedExplodedDomainsMap.delete(target);
179178
}
180-
});
179+
}
181180

182181
if (unusedTargets.size > 0) {
183182
warn`ruleset contains targets ${[
@@ -188,23 +187,23 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
188187
// (4) Replace non-trivial targets with exploded domains
189188
let indent = null;
190189

191-
targetToSupportedExplodedDomainsMap.forEach((value, key, map) => {
192-
let escapedKey = escapeStringRegexp(key);
193-
let regexSource = `\n([\t ]*)<target\\s*host=\\s*"${escapedKey}"\\s*?/>[\t ]*\n`;
194-
let regex = new RegExp(regexSource);
190+
for (const [key, value] of targetToSupportedExplodedDomainsMap) {
191+
const escapedKey = escapeStringRegexp(key);
192+
const regexSource = `\n([\t ]*)<target\\s*host=\\s*"${escapedKey}"\\s*?/>[\t ]*\n`;
193+
const regex = new RegExp(regexSource);
195194

196-
let matches = content.match(regex);
195+
const matches = content.match(regex);
197196
if (!matches) {
198197
// should be unreachable.
199198
warn`unexpected regular expression error`;
200199
process.exit(1);
201200
}
202201

203202
[, indent] = matches;
204-
let sub =
203+
const sub =
205204
value.map(v => `\n${indent}<target host=\"${v}\" />`).join('') + '\n';
206205
content = content.replace(regex, sub);
207-
});
206+
}
208207

209208
// (5) Check if we can trivialize the rules. Need to satisfy all below conditions:
210209
// i) there is no unused target, i.e. unusedTargets.size == 0
@@ -214,17 +213,13 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
214213
// (a) if all of the conditions are met, append a trivial rule after all
215214
// existing rules; and remove the non-snapping rules.
216215
// (b) otherwise, do not trivialize the rules
217-
let condition1 = unusedTargets.size == 0;
218-
let condition2 = [...ruleToIsSimpleMap.entries()].every(
219-
([, value]) => value
220-
);
221-
let condition3 = [...ruleToIsSnappingMap.entries()].some(
222-
([, value]) => !value
223-
);
216+
const condition1 = unusedTargets.size === 0;
217+
const condition2 = [...ruleToIsSimpleMap.entries()].every(([, value]) => value);
218+
const condition3 = [...ruleToIsSnappingMap.entries()].some(([, value]) => !value);
224219

225220
if (condition1 && condition2 && condition3) {
226221
// append trivial rule to the end of current ruleset
227-
if ((content.match(/\n<\/ruleset>/) || []).length != 1) {
222+
if ((content.match(/\n<\/ruleset>/) || []).length !== 1) {
228223
fail`ruleset contains zero or more than one </ruleset> tag`;
229224
return;
230225
} else {
@@ -235,18 +230,18 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
235230
}
236231

237232
// remove all non-snapping rules
238-
let nonSnappingRules = [...ruleToIsSnappingMap.entries()]
233+
const nonSnappingRules = [...ruleToIsSnappingMap.entries()]
239234
.filter(([, value]) => value === false)
240235
.map(([key]) => key);
241236

242-
for (let rule of nonSnappingRules) {
243-
let escapedRuleFrom = escapeStringRegexp(rule.from);
244-
let escapedRuleTo = escapeStringRegexp(rule.to);
237+
for (const rule of nonSnappingRules) {
238+
const escapedRuleFrom = escapeStringRegexp(rule.from);
239+
const escapedRuleTo = escapeStringRegexp(rule.to);
245240

246-
let regexSource = `\n([\t ]*)<rule\\s*from=\\s*"${escapedRuleFrom}"(\\s*)to=\\s*"${escapedRuleTo}"\\s*?/>[\t ]*\n`;
247-
let regex = new RegExp(regexSource);
241+
const regexSource = `\n([\t ]*)<rule\\s*from=\\s*"${escapedRuleFrom}"(\\s*)to=\\s*"${escapedRuleTo}"\\s*?/>[\t ]*\n`;
242+
const regex = new RegExp(regexSource);
248243

249-
let matches = content.match(regex);
244+
const matches = content.match(regex);
250245
if (!matches) {
251246
// should be unreachable.
252247
warn`unexpected regular expression error`;
@@ -269,7 +264,7 @@ const rulesDir = `${__dirname}/../../src/chrome/content/rules`;
269264
});
270265

271266
// use for-loop to await too many file opened error
272-
for (let fp of filePromises) {
267+
for (const fp of filePromises) {
273268
await fp.catch(error => console.log(error));
274269
}
275270
})();

0 commit comments

Comments
 (0)