Skip to content

Commit ed9a836

Browse files
fix: lint issues
1 parent 5de9179 commit ed9a836

File tree

8 files changed

+101
-94
lines changed

8 files changed

+101
-94
lines changed

eslint-warning-thresholds.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,14 @@
300300
"jsdoc/check-tag-names": 39,
301301
"jsdoc/tag-lines": 1
302302
},
303-
"packages/phishing-controller/src/PhishingDetector.ts": {
304-
"@typescript-eslint/no-unused-vars": 1,
305-
"@typescript-eslint/prefer-readonly": 2,
306-
"jsdoc/tag-lines": 2
307-
},
308303
"packages/phishing-controller/src/tests/utils.ts": {
309304
"@typescript-eslint/no-unused-vars": 1
310305
},
311306
"packages/phishing-controller/src/utils.test.ts": {
312307
"import-x/namespace": 5
313308
},
314309
"packages/phishing-controller/src/utils.ts": {
315-
"@typescript-eslint/no-unsafe-enum-comparison": 1,
316-
"@typescript-eslint/no-unused-vars": 1
310+
"@typescript-eslint/no-unsafe-enum-comparison": 1
317311
},
318312
"packages/polling-controller/src/AbstractPollingController.ts": {
319313
"@typescript-eslint/prefer-readonly": 1

packages/phishing-controller/src/PathTrie.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {
22
deleteFromTrie,
33
isTerminalPath,
44
insertToTrie,
5-
PathTrie,
5+
type PathTrie,
66
} from './PathTrie';
77

88
const emptyPathTrie: PathTrie = {};
99

10-
describe.only('PathTrie', () => {
10+
describe('PathTrie', () => {
1111
describe('insertToTrie', () => {
1212
let pathTrie: PathTrie;
1313

packages/phishing-controller/src/PathTrie.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,29 @@ export type PathNode = {
66

77
export type PathTrie = Record<string, PathNode>;
88

9+
const isTerminal = (node: PathNode): boolean => {
10+
return Object.keys(node).length === 0;
11+
};
12+
913
/**
10-
* Insert a URL into the trie, mutating `pathTrie` in place.
11-
* - If an ancestor path already exists as a terminal ({}), do nothing.
12-
* - If inserting an ancestor of existing entries, prune descendants by setting that node to {}.
13-
* - If no path segments exist (bare host or "/"), do nothing.
14+
* Insert a URL into the trie.
15+
*
16+
* @param url - The URL to insert into the trie.
17+
* @param pathTrie - The trie to insert the URL into.
1418
*/
1519
export const insertToTrie = (url: string, pathTrie: PathTrie) => {
16-
var { hostname, pathComponents } = getHostnameAndPathComponents(url);
20+
const { hostname, pathComponents } = getHostnameAndPathComponents(url);
1721

1822
if (pathComponents.length === 0 || !hostname) {
1923
return;
2024
}
2125

22-
hostname = hostname.toLowerCase();
23-
if (!pathTrie[hostname]) {
24-
pathTrie[hostname] = {} as PathNode;
26+
const lowerHostname = hostname.toLowerCase();
27+
if (!pathTrie[lowerHostname]) {
28+
pathTrie[lowerHostname] = {} as PathNode;
2529
}
2630

27-
let curr: PathNode = pathTrie[hostname];
31+
let curr: PathNode = pathTrie[lowerHostname];
2832
for (let i = 0; i < pathComponents.length; i++) {
2933
const pathComponent = pathComponents[i];
3034
const isLast = i === pathComponents.length - 1;
@@ -56,20 +60,25 @@ export const insertToTrie = (url: string, pathTrie: PathTrie) => {
5660
}
5761
};
5862

63+
/**
64+
* Delete a URL from the trie.
65+
*
66+
* @param url - The URL to delete from the trie.
67+
* @param pathTrie - The trie to delete the URL from.
68+
*/
5969
export const deleteFromTrie = (url: string, pathTrie: PathTrie) => {
60-
var { hostname, pathComponents } = getHostnameAndPathComponents(url);
70+
const { hostname, pathComponents } = getHostnameAndPathComponents(url);
6171

62-
if (pathComponents.length === 0 || !pathTrie[hostname]) {
72+
const lowerHostname = hostname.toLowerCase();
73+
if (pathComponents.length === 0 || !pathTrie[lowerHostname]) {
6374
return;
6475
}
6576

6677
const pathToNode: { node: PathNode; key: string }[] = [
67-
{ node: pathTrie, key: hostname },
78+
{ node: pathTrie, key: lowerHostname },
6879
];
69-
let curr: PathNode = pathTrie[hostname];
70-
for (let i = 0; i < pathComponents.length; i++) {
71-
const pathComponent = pathComponents[i];
72-
80+
let curr: PathNode = pathTrie[lowerHostname];
81+
for (const pathComponent of pathComponents) {
7382
if (!curr[pathComponent]) {
7483
return;
7584
}
@@ -90,25 +99,27 @@ export const deleteFromTrie = (url: string, pathTrie: PathTrie) => {
9099
}
91100
};
92101

102+
/**
103+
* Check if a URL is a terminal path i.e. the last path component is a terminal node.
104+
*
105+
* @param url - The URL to check.
106+
* @param pathTrie - The trie to check the URL in.
107+
* @returns True if the URL is a terminal path, false otherwise.
108+
*/
93109
export const isTerminalPath = (url: string, pathTrie: PathTrie): boolean => {
94-
var { hostname, pathComponents } = getHostnameAndPathComponents(url);
110+
const { hostname, pathComponents } = getHostnameAndPathComponents(url);
95111

96-
hostname = hostname.toLowerCase();
97-
if (pathComponents.length === 0 || !hostname || !pathTrie[hostname]) {
112+
const lowerHostname = hostname.toLowerCase();
113+
if (pathComponents.length === 0 || !hostname || !pathTrie[lowerHostname]) {
98114
return false;
99115
}
100116

101-
let curr: PathNode = pathTrie[hostname];
102-
for (let i = 0; i < pathComponents.length; i++) {
103-
const pathComponent = pathComponents[i];
117+
let curr: PathNode = pathTrie[lowerHostname];
118+
for (const pathComponent of pathComponents) {
104119
if (!curr[pathComponent]) {
105120
return false;
106121
}
107122
curr = curr[pathComponent];
108123
}
109124
return isTerminal(curr);
110125
};
111-
112-
const isTerminal = (node: PathNode): boolean => {
113-
return Object.keys(node).length === 0;
114-
};

packages/phishing-controller/src/PhishingController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@metamask/controller-utils';
1212
import { toASCII } from 'punycode/punycode.js';
1313

14+
import { type PathTrie } from './PathTrie';
1415
import { PhishingDetector } from './PhishingDetector';
1516
import {
1617
PhishingDetectorResultType,
@@ -33,7 +34,6 @@ import {
3334
getPathnameFromUrl,
3435
separateBlocklistEntries,
3536
} from './utils';
36-
import { PathTrie } from './PathTrie';
3737

3838
export const PHISHING_CONFIG_BASE_URL =
3939
'https://phishing-detection.api.cx.metamask.io';

packages/phishing-controller/src/PhishingDetector.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ describe('PhishingDetector', () => {
12781278
expect(result).toBe(true);
12791279
expect(type).toBe(PhishingDetectorResultType.Blocklist);
12801280
expect(name).toBe('test-config');
1281-
expect(version).toBe(undefined);
1281+
expect(version).toBeUndefined();
12821282
},
12831283
);
12841284
});

packages/phishing-controller/src/PhishingDetector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { distance } from 'fastest-levenshtein';
22

3+
import { isTerminalPath, type PathTrie } from './PathTrie';
34
import {
45
PhishingDetectorResultType,
56
type PhishingDetectorResult,
@@ -15,7 +16,6 @@ import {
1516
processConfigs,
1617
sha256Hash,
1718
} from './utils';
18-
import { isTerminalPath, PathTrie } from './PathTrie';
1919

2020
export type LegacyPhishingDetectorList = {
2121
whitelist?: string[];
@@ -59,9 +59,9 @@ export type PhishingDetectorConfiguration = {
5959
};
6060

6161
export class PhishingDetector {
62-
#configs: PhishingDetectorConfiguration[];
62+
readonly #configs: PhishingDetectorConfiguration[];
6363

64-
#legacyConfig: boolean;
64+
readonly #legacyConfig: boolean;
6565

6666
/**
6767
* Construct a phishing detector, which can check whether origins are known
@@ -149,7 +149,7 @@ export class PhishingDetector {
149149
let domain;
150150
try {
151151
domain = new URL(url).hostname;
152-
} catch (error) {
152+
} catch {
153153
return {
154154
result: false,
155155
type: PhishingDetectorResultType.All,
@@ -258,7 +258,6 @@ export class PhishingDetector {
258258
* Checks if a URL is blocked against the hashed request blocklist.
259259
* This is done by hashing the URL's hostname and checking it against the hashed request blocklist.
260260
*
261-
*
262261
* @param urlString - The URL to check.
263262
* @returns An object indicating if the URL is blocked and relevant metadata.
264263
*/
@@ -328,6 +327,7 @@ export class PhishingDetector {
328327

329328
/**
330329
* Runs a regex match to determine if a string is a IPFS CID
330+
*
331331
* @returns Regex string for IPFS CID
332332
*/
333333
function ipfsCidRegex() {

packages/phishing-controller/src/utils.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import * as sinon from 'sinon';
22

3-
import { ListKeys, ListNames } from './PhishingController';
3+
import {
4+
ListKeys,
5+
ListNames,
6+
type PhishingListState,
7+
} from './PhishingController';
48
import {
59
applyDiffs,
610
domainToParts,
711
fetchTimeNow,
812
generateParentDomains,
9-
getDefaultPhishingDetectorConfig,
1013
getHostnameAndPathComponents,
1114
getHostnameFromUrl,
1215
getHostnameFromWebUrl,
@@ -271,7 +274,7 @@ describe('applyDiffs', () => {
271274
});
272275

273276
describe('adding URLs to blocklistPaths', () => {
274-
let listState: any;
277+
let listState: PhishingListState;
275278

276279
beforeEach(() => {
277280
listState = {
@@ -381,7 +384,7 @@ describe('applyDiffs', () => {
381384
});
382385

383386
describe('removing URLs from blocklistPaths', () => {
384-
let listState: any;
387+
let listState: PhishingListState;
385388

386389
beforeEach(() => {
387390
listState = {

0 commit comments

Comments
 (0)