Skip to content

Commit c548e68

Browse files
chore: add ./script/**/*.ts and ./rspack.config.ts to ESLint targets (#1108)
1 parent 73d3f85 commit c548e68

File tree

10 files changed

+89
-31
lines changed

10 files changed

+89
-31
lines changed

knip.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"script/addonsLinter.ts",
66
"script/updatePrivacyPolicy.ts",
77
"script/addUserScriptsComment.ts",
8+
"script/util.ts",
89
"script/copyManifest.ts",
910
"src/ts/userScript/*.ts"
1011
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
9-
"lint": "npx eslint ./src/ts/**/*.ts ./src/types/**/*.ts && npx knip && tsx ./script/addonsLinter.ts",
9+
"lint": "npx eslint ./src/ts/**/*.ts ./src/types/**/*.ts ./script/**/*.ts ./rspack.config.ts && npx knip && tsx ./script/addonsLinter.ts",
1010
"format": "npx prettier --write ./src/ts/**/*.ts ./src/html/**/*.html ./src/css/**/*.css",
1111
"format:check": "npx prettier --check ./src/ts/**/*.ts ./src/html/**/*.html ./src/css/**/*.css",
1212
"build": "npx cross-env NODE_OPTIONS=--experimental-transform-types NODE_ENV=production npx rspack build",

rspack.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class RunCommandsPlugin {
8484
}
8585
});
8686

87+
// eslint-disable-next-line max-statements
8788
compiler.hooks.watchRun.tapAsync("RunCommandsPlugin", (_params, callback) => {
8889
isWatchMode = true;
8990

@@ -105,7 +106,7 @@ class RunCommandsPlugin {
105106
// eslint-disable-next-line no-console
106107
console.log(`Type definition file changed: ${pathString}`);
107108
RunCommandsPlugin.generateTypeGuards(() => {
108-
if (compiler && compiler.watching) {
109+
if (compiler.watching) {
109110
compiler.watching.invalidate();
110111
}
111112
});

script/addUserScriptsComment.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
import { glob } from "glob";
21
import fs from "fs";
2+
import { glob } from "glob";
3+
import { isPlainObject } from "./util";
34
import packagejson from "../package.json";
45
import path from "path";
56

7+
// eslint-disable-next-line no-console
68
console.log("Adding userScript comments...");
79
const userScriptFiles = glob.sync("./userScript/*.user.js");
810

911
for (const userScript of userScriptFiles) {
1012
const scriptString = fs.readFileSync(userScript, "utf-8");
13+
1114
const languageCode = path.basename(userScript, ".user.js");
1215
const formattedLanguageCode = languageCode.toLowerCase().replace("_", "-");
1316
const languageName =
14-
new Intl.DisplayNames([formattedLanguageCode], { type: "language" }).of(formattedLanguageCode) ||
17+
new Intl.DisplayNames([formattedLanguageCode], { type: "language" }).of(formattedLanguageCode) ??
1518
formattedLanguageCode;
16-
const localizedMessages = fs.readFileSync(`./src/_locales/${languageCode}/messages.json`, "utf-8");
19+
20+
const localizedMessages: unknown = JSON.parse(
21+
fs.readFileSync(`./src/_locales/${languageCode}/messages.json`, "utf-8")
22+
);
23+
if (
24+
!isPlainObject(localizedMessages) ||
25+
!isPlainObject(localizedMessages.manifest_description) ||
26+
typeof localizedMessages.manifest_description.message !== "string"
27+
) {
28+
throw new Error(`Invalid localized messages for ${languageCode}`);
29+
}
30+
1731
const userScriptComment = `
1832
// ==UserScript==
1933
// @name Shadowban Scanner (${languageName})
2034
// @namespace https://github.com/Robot-Inventor/shadowban-scanner/
2135
// @version ${packagejson.version}
22-
// @description ${JSON.parse(localizedMessages)["manifest_description"]["message"]}
36+
// @description ${localizedMessages.manifest_description.message}
2337
// @author Robot-Inventor (ろぼいん / @keita_roboin)
2438
// @match https://*.twitter.com/*
2539
// @match https://*.x.com/*
@@ -35,8 +49,9 @@ for (const userScript of userScriptFiles) {
3549
// @grant none
3650
// ==/UserScript==
3751
`.trim();
38-
const newScriptString = userScriptComment + "\n\n" + scriptString;
52+
const newScriptString = `${userScriptComment}\n\n${scriptString}`;
3953
fs.writeFileSync(userScript, newScriptString);
4054
}
4155

56+
// eslint-disable-next-line no-console
4257
console.log("Done!");

script/addonsLinter.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,63 @@
1-
import colors from "colors/safe";
1+
import * as colors from "colors/safe";
2+
import { type Options as LinterOptions, createInstance } from "addons-linter";
23
import { execSync } from "child_process";
3-
import linter from "addons-linter";
44

5-
const main = async () => {
6-
const linterConfigFirefox: linter.Options = {
5+
const ERROR_EXIT_CODE = 1;
6+
7+
// eslint-disable-next-line max-statements
8+
const main = async (): Promise<void> => {
9+
const linterConfigFirefox: LinterOptions = {
710
config: {
11+
// eslint-disable-next-line id-length
812
_: ["./dist/firefox/"],
913
logLevel: process.env.VERBOSE ? "debug" : "fatal",
10-
shouldScanFile: (fileName, isDir) => {
11-
return true;
12-
}
14+
shouldScanFile: () => true
1315
}
1416
};
1517

16-
const linterConfigChrome: linter.Options = {
18+
const linterConfigChrome: LinterOptions = {
1719
config: {
1820
...linterConfigFirefox.config,
21+
// eslint-disable-next-line id-length
1922
_: ["./dist/chrome/"],
2023
enableBackgroundServiceWorker: true
2124
}
2225
};
2326

24-
const lintForFirefox = linter.createInstance(linterConfigFirefox);
25-
const lintForChrome = linter.createInstance(linterConfigChrome);
27+
const lintForFirefox = createInstance(linterConfigFirefox);
28+
const lintForChrome = createInstance(linterConfigChrome);
2629

30+
// eslint-disable-next-line no-console
2731
console.log("Building...");
2832
execSync("npm run build");
2933

34+
// eslint-disable-next-line no-console
3035
console.log("Linting for Firefox...");
3136
const firefoxResult = await lintForFirefox.run();
3237
if (firefoxResult.errors.length) {
38+
// eslint-disable-next-line no-console
3339
console.error(colors.red("Errors found when linting for Firefox."));
34-
process.exit(1);
40+
process.exit(ERROR_EXIT_CODE);
3541
}
3642

43+
// eslint-disable-next-line no-console
3744
console.log("Linting for Chrome...");
3845
const chromeResult = await lintForChrome.run();
3946
if (chromeResult.errors.length) {
47+
// eslint-disable-next-line no-console
4048
console.error(colors.red("Errors found when linting for Chrome."));
41-
process.exit(1);
49+
process.exit(ERROR_EXIT_CODE);
4250
}
4351

52+
// eslint-disable-next-line no-console
4453
console.log("Done.");
4554
};
4655

4756
try {
4857
void main();
4958
} catch (error) {
50-
// @ts-expect-error
59+
// @ts-expect-error error is not an instance of Error
60+
// eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
5161
console.error(colors.red(error.stdout.toString()));
52-
process.exit(1);
62+
process.exit(ERROR_EXIT_CODE);
5363
}

script/copyManifest.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import fs from "fs";
22

3-
import packageJson from "../package.json";
43
import manifestV2 from "../src/manifest/v2.json";
54
import manifestV3 from "../src/manifest/v3.json";
5+
import packageJson from "../package.json";
66

77
const { version } = packageJson;
88

9+
const JSON_INDENT = 4;
10+
911
manifestV2.version = version;
10-
const manifestV2Text = JSON.stringify(manifestV2, null, 4);
12+
const manifestV2Text = JSON.stringify(manifestV2, null, JSON_INDENT);
1113
fs.writeFileSync("./dist/firefox/manifest.json", manifestV2Text);
14+
// eslint-disable-next-line no-console
1215
console.log(`Generated manifest v2 file.`);
1316

1417
manifestV3.version = version;
15-
const manifestV3Text = JSON.stringify(manifestV3, null, 4);
18+
const manifestV3Text = JSON.stringify(manifestV3, null, JSON_INDENT);
1619
fs.writeFileSync("./dist/chrome/manifest.json", manifestV3Text);
20+
// eslint-disable-next-line no-console
1721
console.log(`Generated manifest v3 file.`);

script/package.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { execSync } from "child_process";
22

3+
// eslint-disable-next-line no-console
34
console.log("Building...");
45
execSync("npm run build");
56

7+
// eslint-disable-next-line no-console
68
console.log("Packaging for Chrome...");
79
execSync(
810
`npx web-ext build --source-dir "./dist/chrome/" --artifacts-dir "./web-ext-artifacts/manifestV3" --filename "{short_name}-{version}-manifestV3.zip"`
911
);
1012

13+
// eslint-disable-next-line no-console
1114
console.log("Packaging for Firefox...");
1215
execSync(
1316
`npx web-ext build --source-dir "./dist/firefox/" --artifacts-dir "./web-ext-artifacts/manifestV2" --filename "{short_name}-{version}-manifestV2.zip"`
1417
);
18+
// eslint-disable-next-line no-console
1519
console.log("Done.");

script/updatePrivacyPolicy.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
import { glob } from "glob";
21
import fs from "fs";
2+
import { glob } from "glob";
3+
import { isPlainObject } from "./util";
34

45
const START_MARKER = "<!-- PRIVACY_POLICY_TEXT_START -->";
56
const END_MARKER = "<!-- PRIVACY_POLICY_TEXT_END -->";
67

8+
// eslint-disable-next-line no-console
79
console.log("Updating privacy policy...");
810
const files = glob.sync("./README*.md");
911

1012
for (const file of files) {
1113
const readmeText = fs.readFileSync(file, "utf8");
12-
const languageCode = file.replace(/README_?/, "").replace(".md", "") || "en";
13-
const messagePath = `./src/_locales/${languageCode.replace(/(_\w+)/, (match) => match.toUpperCase())}/messages.json`;
14-
const privacyPolicyText = JSON.parse(fs.readFileSync(messagePath, "utf8")).privacyPolicyText.message;
14+
const languageCode = file.replace(/README_?/u, "").replace(".md", "") || "en";
15+
// eslint-disable-next-line prefer-named-capture-group
16+
const messagePath = `./src/_locales/${languageCode.replace(/(_\w+)/u, (match) => match.toUpperCase())}/messages.json`;
17+
18+
const parsedMessages: unknown = JSON.parse(fs.readFileSync(messagePath, "utf8"));
19+
if (
20+
!isPlainObject(parsedMessages) ||
21+
!isPlainObject(parsedMessages.privacyPolicyText) ||
22+
typeof parsedMessages.privacyPolicyText.message !== "string"
23+
) {
24+
throw new Error(`Privacy policy text not found in ${messagePath}`);
25+
}
26+
const privacyPolicyText = parsedMessages.privacyPolicyText.message;
1527

1628
const newPrivacyPolicySection = `
1729
${START_MARKER}
@@ -23,11 +35,12 @@ ${END_MARKER}
2335
`.trim();
2436

2537
const updatedReadmeText = readmeText.replace(
26-
new RegExp(`${START_MARKER}[\\s\\S]+${END_MARKER}`, "m"),
38+
new RegExp(`${START_MARKER}[\\s\\S]+${END_MARKER}`, "mu"),
2739
newPrivacyPolicySection
2840
);
2941

3042
fs.writeFileSync(file, updatedReadmeText);
3143
}
3244

45+
// eslint-disable-next-line no-console
3346
console.log("Updated privacy policy");

script/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
2+
typeof value === "object" && value !== null && !Array.isArray(value);
3+
4+
export { isPlainObject };

tsconfig.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
"declaration": false,
55
"experimentalDecorators": true,
66
"useDefineForClassFields": false,
7-
"isolatedModules": true
8-
}
7+
"isolatedModules": true,
8+
"rootDir": "."
9+
},
10+
"include": [
11+
"./rspack.config.ts",
12+
"./src/**/*.ts",
13+
"./script/**/*.ts"
14+
]
915
}

0 commit comments

Comments
 (0)