Skip to content

Commit c8833a2

Browse files
committed
AG-29670: added types checking
Merge in ADGUARD-FILTERS/scriptlets from feature/AG-29670 to master Squashed commit of the following: commit e1751f0 Merge: aba5fd8 7446aa3 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Thu Feb 8 17:32:19 2024 +0300 Merge branch 'master' into feature/AG-29670 commit aba5fd8 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Wed Feb 7 16:20:22 2024 +0300 use Pick commit 7bb0a5b Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Wed Feb 7 16:18:41 2024 +0300 fixes commit 2c946fe Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Feb 6 17:33:33 2024 +0300 add types linter cache. AG-29670 Squashed commit of the following: commit e01d28a Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Feb 6 15:47:00 2024 +0200 add types linter cache commit e495417 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Feb 5 15:36:35 2024 +0300 changed name commit 1180c8c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Thu Feb 1 18:58:12 2024 +0300 AG-29670: added types checking
1 parent 7446aa3 commit c8833a2

File tree

10 files changed

+58
-51
lines changed

10 files changed

+58
-51
lines changed

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
'import/resolver': {
2525
typescript: {
2626
alwaysTryTypes: true,
27-
project: 'tsconfig.eslint.json',
27+
project: 'tsconfig.json',
2828
},
2929
},
3030
},
@@ -67,7 +67,7 @@ module.exports = {
6767
parser: '@typescript-eslint/parser',
6868
parserOptions: {
6969
tsconfigRootDir: path.join(__dirname),
70-
project: 'tsconfig.eslint.json',
70+
project: 'tsconfig.json',
7171
},
7272
extends: [
7373
'airbnb-typescript/base',

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ output.md
1111
tests/dist
1212
browserstack.err
1313
local.log
14+
15+
# linters cache
1416
.eslintcache
17+
tsconfig.tsbuildinfo

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"browserstack": "yarn test --build && node browserstack.js",
99
"test:qunit": "babel-node -x .js,.ts scripts/test.js",
1010
"test:jest": "jest",
11-
"lint": "eslint --cache . && yarn lint:md",
11+
"lint": "eslint --cache . && yarn lint:types && yarn lint:md",
12+
"lint:types": "tsc --noEmit",
1213
"lint:md": "markdownlint .",
1314
"lint-staged": "lint-staged",
1415
"prepare": "husky install",

src/helpers/converter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import validator from './validator';
1313

1414
import { ADG_SCRIPTLET_MASK, parseRule } from './parse-rule';
1515

16-
import * as scriptletList from '../scriptlets/scriptlets-list';
16+
import * as scriptletListRaw from '../scriptlets/scriptlets-list';
17+
18+
const scriptletList: ScriptletsList = scriptletListRaw;
1719

1820
type AdgScriptletObject = {
1921
name: string;
@@ -420,7 +422,7 @@ export const convertAdgScriptletToUbo = (rule: string): string | undefined => {
420422
// object of name and aliases for the Adg-scriptlet
421423
const scriptletNames = Object.keys(scriptletList);
422424
const adgScriptletObject = scriptletNames
423-
.map((name) => (scriptletList as ScriptletstList)[name])
425+
.map((name) => scriptletList[name])
424426
.map((scriptlet) => {
425427
const [name, ...aliases] = scriptlet.names;
426428
return { name, aliases };

src/helpers/observer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { throttle } from './throttle';
22

33
/**
4-
* DOM tree changes observer. Used for 'remove-attr' and 'remove-class' scriptlets
4+
* DOM tree changes observer.
55
*
66
* @param callback function to call on each mutation
77
* @param observeAttrs if observer should observe attributes changes

src/helpers/string-utils.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,21 @@ export const objectToString = (obj: ArbitraryObject): string => {
312312
return String(obj);
313313
}
314314

315-
return isEmptyObject(obj)
316-
? '{}'
317-
: Object.entries(obj)
318-
.map((pair) => {
319-
const key = pair[0];
320-
const value = pair[1];
321-
let recordValueStr = value;
322-
if (value instanceof Object) {
323-
recordValueStr = `{ ${objectToString(value as ArbitraryObject)} }`;
324-
}
325-
return `${key}:"${recordValueStr}"`;
326-
})
327-
.join(' ');
315+
if (isEmptyObject(obj)) {
316+
return '{}';
317+
}
318+
319+
return Object.entries(obj)
320+
.map((pair) => {
321+
const key = pair[0];
322+
const value = pair[1];
323+
let recordValueStr = value;
324+
if (value instanceof Object) {
325+
recordValueStr = `{ ${objectToString(value as ArbitraryObject)} }`;
326+
}
327+
return `${key}:"${recordValueStr}"`;
328+
})
329+
.join(' ');
328330
};
329331

330332
/**
@@ -334,20 +336,18 @@ export const objectToString = (obj: ArbitraryObject): string => {
334336
* @returns type's string representation
335337
*/
336338
export const convertTypeToString = (value: unknown): string => {
337-
let output;
338339
if (typeof value === 'undefined') {
339-
output = 'undefined';
340-
} else if (typeof value === 'object') {
340+
return 'undefined';
341+
}
342+
if (typeof value === 'object') {
341343
if (value === null) {
342-
output = 'null';
343-
} else {
344-
output = objectToString(value as Record<string, unknown>);
344+
return 'null';
345345
}
346-
} else {
347-
output = value.toString();
346+
347+
return objectToString(value as ArbitraryObject);
348348
}
349349

350-
return output;
350+
return (value as Pick<Object, 'toString'>).toString();
351351
};
352352

353353
/**
@@ -403,11 +403,11 @@ export function generateRandomResponse(customResponseText: string): string | nul
403403
}
404404

405405
const LENGTH_RANGE_LIMIT = 500 * 1000;
406-
if ((rangeMax as number) > LENGTH_RANGE_LIMIT) {
406+
if ((rangeMax) > LENGTH_RANGE_LIMIT) {
407407
return null;
408408
}
409409

410-
const length = getRandomIntInclusive((rangeMin as number), (rangeMax as number));
410+
const length = getRandomIntInclusive((rangeMin), (rangeMax));
411411
customResponse = getRandomStrByLength(length);
412412
return customResponse;
413413
}

src/helpers/validator.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
toRegExp,
44
} from './string-utils';
55
import { ADG_SCRIPTLET_MASK } from './parse-rule';
6-
import * as scriptletsList from '../scriptlets/scriptlets-list';
6+
import * as scriptletListRaw from '../scriptlets/scriptlets-list';
77
import redirects from './compatibility-redirects';
88

99
interface UboToAdgCompatibilityObject extends RedirectCompatibilityMap {
@@ -99,7 +99,7 @@ const isAbpSnippetRule = (rule: string): boolean => {
9999
* @returns Array of all scriptlet objects.
100100
*/
101101
const getScriptletsObjList = () => {
102-
return Object.values(scriptletsList as ScriptletstList);
102+
return Object.values(scriptletListRaw);
103103
};
104104

105105
/**
@@ -110,18 +110,16 @@ const getScriptletsObjList = () => {
110110
* @returns {Function} Scriptlet function.
111111
*/
112112
const getScriptletByName = (name: string, scriptlets: Scriptlet[]): Scriptlet | undefined => {
113-
if (!scriptlets) {
114-
scriptlets = getScriptletsObjList();
115-
}
116-
return scriptlets
117-
.find((s) => {
118-
return s.names
119-
// full match name checking
120-
&& (s.names.includes(name)
121-
// or check ubo alias name without '.js' at the end
122-
|| (!name.endsWith('.js') && s.names.includes(`${name}.js`))
123-
);
124-
});
113+
const allScriptletsFns = scriptlets || getScriptletsObjList();
114+
115+
return allScriptletsFns.find((s) => {
116+
return s.names
117+
// full match name checking
118+
&& (s.names.includes(name)
119+
// or check ubo alias name without '.js' at the end
120+
|| (!name.endsWith('.js') && s.names.includes(`${name}.js`))
121+
);
122+
});
125123
};
126124

127125
const scriptletObjects = getScriptletsObjList();

tsconfig.eslint.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
"target": "esnext",
44
"moduleResolution": "node",
55
"allowJs": true,
6+
"skipLibCheck": true,
67
"noEmit": true,
78
"strict": true,
89
"lib":["DOM"],
10+
"incremental": true,
911
"esModuleInterop": true,
10-
}
12+
},
13+
"exclude": [
14+
"node_modules",
15+
"dist",
16+
],
1117
}

types/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Source {
99
}
1010

1111
type ArbitraryObject = { [key: string | symbol | number]: unknown };
12-
type ArbitraryFunction = (...args: unknown[]) => unknown;
12+
type ArbitraryFunction = (...args: any[]) => unknown;
1313
type NoopFunc = () => void;
1414
type TrueFunc = () => true;
1515
type Helper = ArbitraryFunction;
@@ -20,14 +20,14 @@ interface PageFunction {
2020
}
2121

2222
interface Scriptlet extends PageFunction {
23-
(source: Source, ...args: Array<string | number>): void;
23+
(source: Source, ...args: any[]): void;
2424
}
2525

2626
interface Redirect extends PageFunction {
2727
(source: Source): void;
2828
}
2929

30-
type ScriptletstList = Record<string, Scriptlet>;
30+
type ScriptletsList = Record<string, Scriptlet>;
3131

3232
type ChainBase = {
3333
[key: string]: ChainBase;

0 commit comments

Comments
 (0)