Skip to content

Commit d48a8c0

Browse files
committed
style: "Rearrange order of task definitions and disable ESLint on non-literal fs filename"
Rearranged the order of task definitions in many "group" files to achieve consistent code organization. The change doesn't affect the functionality of the files. Also, ESLint is now disabled for non-literal file system filename invocations for better runtime security. Several eslint errors of security/detect-non-literal-fs-filename, security/detect-object-injection were also fixed throughout the diff. Some parameters are reordered in some files to make the code base more consistent and enhance readability. Signed-off-by: prisis <[email protected]>
1 parent dcd5718 commit d48a8c0

File tree

21 files changed

+143
-115
lines changed

21 files changed

+143
-115
lines changed

babel.config.cjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module.exports = {
2+
plugins: ["transform-es2015-modules-commonjs"],
23
presets: [
34
[
45
"@babel/preset-env",
56
{
7+
modules: "auto",
68
targets: {
79
node: "current",
810
},
9-
modules: "auto",
1011
},
1112
],
1213
],
13-
plugins: ["transform-es2015-modules-commonjs"],
1414
};

packages/babel-preset/src/index.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ const preset = declare((api: BabelAPI, options: Options): Record<string, any> =>
4141
api.assertVersion("^7.13");
4242

4343
const {
44-
modules = "auto",
45-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
46-
targets,
47-
removePropTypes: removePropertyTypes = false,
44+
corejs = false,
4845
loose = true,
4946
looseClasses = true,
50-
looseObjectRestSpread = true,
5147
looseComputedProperties = true,
48+
looseObjectRestSpread = true,
5249
looseParameters = true,
5350
looseTemplateLiterals = true,
54-
typescript = false,
55-
react = false,
51+
modules = "auto",
5652
polyfillRegenerator = false,
53+
react = false,
54+
removePropTypes: removePropertyTypes = false,
55+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
56+
targets,
57+
typescript = false,
5758
useBuiltIns = false,
58-
corejs = false,
5959
} = options;
6060

6161
if (typeof modules === "boolean" && typeof modules === "string") {
@@ -103,15 +103,15 @@ const preset = declare((api: BabelAPI, options: Options): Record<string, any> =>
103103
[
104104
"@babel/preset-env",
105105
{
106-
debug,
107106
bugfixes: true,
108-
useBuiltIns,
107+
debug,
109108
exclude: ["transform-async-to-generator", "transform-regenerator"],
109+
loose,
110110
modules: modules === false ? false : "auto",
111+
shippedProposals: api.env("modern"),
111112
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
112113
targets,
113-
shippedProposals: api.env("modern"),
114-
loose,
114+
useBuiltIns,
115115
},
116116
],
117117
typescript
@@ -191,8 +191,8 @@ const preset = declare((api: BabelAPI, options: Options): Record<string, any> =>
191191
? [
192192
"babel-plugin-transform-react-remove-prop-types",
193193
{
194-
mode: "unsafe-wrap",
195194
ignoreFilenames: ["node_modules"],
195+
mode: "unsafe-wrap",
196196
...(removePropertyTypes as object),
197197
},
198198
]
@@ -227,8 +227,8 @@ const preset = declare((api: BabelAPI, options: Options): Record<string, any> =>
227227
? [
228228
"babel-plugin-polyfill-corejs3",
229229
{
230-
method: corejs.method ?? "usage-global",
231230
absoluteImports: "core-js",
231+
method: corejs.method ?? "usage-global",
232232
version: corejs.version,
233233
...corejs,
234234
},
@@ -240,8 +240,8 @@ const preset = declare((api: BabelAPI, options: Options): Record<string, any> =>
240240
assumptions: {
241241
noDocumentAll: true,
242242
},
243-
presets,
244243
plugins,
244+
presets,
245245
};
246246
});
247247

packages/babel-preset/src/postinstall.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const writeBabelRc = () => {
2222
};
2323
`;
2424

25+
// eslint-disable-next-line security/detect-non-literal-fs-filename
2526
if (existsSync(babelPath)) {
2627
console.warn(`⚠️ babel.config.js already exists;
2728
Make sure that it includes the following for @anolilab/babel-preset'

packages/browserslist-config-anolilab/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const config = {
2-
production: ["> 1%", "last 2 versions", "Firefox ESR", "not dead"],
32
legacyBrowsers: ["> 1%", "last 2 versions", "Firefox ESR"],
43
modernBrowsers: [
54
"last 2 Chrome versions",
@@ -13,9 +12,10 @@ const config = {
1312
"last 2 Edge versions",
1413
"not Edge < 15",
1514
],
16-
ssr: ["node 16"],
1715
/** NOTE: Meaning LTS version. Any version above LTS is not considered as "major release", AFAIK. */
1816
node: ["last 2 node major versions"],
17+
production: ["> 1%", "last 2 versions", "Firefox ESR", "not dead"],
18+
ssr: ["node 16"],
1919
};
2020

2121
export default config;

packages/commitlint-config/src/postinstall.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ console.log("Configuring @anolilab/commitlint-config", projectPath, "\n");
1818
const writeCommitLintConfig = () => {
1919
const commitlintPath = join(projectPath, "commitlint.config.js");
2020

21+
// eslint-disable-next-line security/detect-non-literal-fs-filename
2122
if (existsSync(commitlintPath)) {
2223
console.warn("⚠️ commitlint.config.js already exists;");
2324

@@ -43,6 +44,7 @@ const writeCommitLintConfig = () => {
4344
const writeCzrc = () => {
4445
const filePath = join(projectPath, ".czrc");
4546

47+
// eslint-disable-next-line security/detect-non-literal-fs-filename
4648
if (existsSync(filePath)) {
4749
console.warn("⚠️ .czrc already exists;");
4850

packages/lint-staged-config/src/config.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,38 @@ type Groups = {
1818

1919
const groups: Groups = [
2020
{
21-
configName: "eslint",
2221
config: eslintConfig,
22+
configName: "eslint",
2323
dependencies: ["prettier", "eslint"],
2424
},
2525
{
26-
configName: "json",
2726
config: jsonConfig,
27+
configName: "json",
2828
dependencies: ["prettier", "sort-package-json"],
2929
},
3030
{
31-
configName: "markdown",
3231
config: markdownConfig,
32+
configName: "markdown",
3333
dependencies: ["prettier", "markdownlint-cli", "markdownlint-cli2"],
3434
},
3535
{
36-
configName: "secretlint",
3736
config: secretlintConfig,
37+
configName: "secretlint",
3838
dependencies: ["secretlint"],
3939
},
4040
{
41-
configName: "stylesheets",
4241
config: stylesheetsConfig,
42+
configName: "stylesheets",
4343
dependencies: ["stylelint"],
4444
},
4545
{
46-
configName: "tests",
4746
config: testsConfig,
47+
configName: "tests",
4848
dependencies: ["vite", "jest", "ava"],
4949
},
5050
{
51-
configName: "typescript",
5251
config: typescriptConfig,
52+
configName: "typescript",
5353
dependencies: ["typescript"],
5454
},
5555
];
@@ -60,8 +60,9 @@ const loadedPluginsNames: string[] = [];
6060
const possiblePlugins: { [rule: string]: { [packageName: string]: boolean } } = {};
6161

6262
groups.forEach((plugin) => {
63-
const { dependencies, config, configName } = plugin;
63+
const { config, configName, dependencies } = plugin;
6464

65+
// eslint-disable-next-line security/detect-object-injection
6566
if ((anolilabLintStagedConfig as unknown as { [key: string]: { [key: string]: false | undefined } })?.["plugin"]?.[configName] !== false) {
6667
const foundDependencies = [];
6768

@@ -75,9 +76,11 @@ groups.forEach((plugin) => {
7576
loadedPlugins = { ...loadedPlugins, ...config };
7677
loadedPluginsNames.push(configName);
7778
} else {
79+
// eslint-disable-next-line security/detect-object-injection
7880
possiblePlugins[configName] = {};
7981

8082
dependencies.forEach((dependency) => {
83+
// eslint-disable-next-line security/detect-object-injection
8184
(possiblePlugins[configName] as { [key: string]: boolean })[dependency] = hasDependency(dependency) || hasDevDependency(dependency);
8285
});
8386
}

packages/lint-staged-config/src/groups/eslint/group-file-paths-by-directory-name.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const groupFilePathsByDirectoryName = (filePaths: string[]): { [key: string]: st
66
filePaths.forEach((filePath) => {
77
const directoryName = dirname(filePath);
88

9+
// eslint-disable-next-line security/detect-object-injection
910
if (!groups[directoryName]) {
11+
// eslint-disable-next-line security/detect-object-injection
1012
groups[directoryName] = [];
1113
}
12-
14+
// eslint-disable-next-line security/detect-object-injection
1315
(groups[directoryName] as string[]).push(filePath);
1416
});
1517

packages/lint-staged-config/src/groups/eslint/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ if (!global.hasAnolilabLintStagedMarkdownCli && !global.hasAnolilabLintStagedMar
1919
}
2020

2121
const group: Config = {
22+
[`*.{${["json", "json5", "jsonc"].join(",")}}`]: async (filenames: string[]) => [...(await createEslintCommands(filenames))],
2223
[`*.{${[extensions].join(",")}}`]: async (filenames: string[]) => [
2324
`prettier --write ${concatFiles(filenames)}`,
2425
...(await createEslintCommands(filenames)),
2526
],
26-
[`*.{${["json", "json5", "jsonc"].join(",")}}`]: async (filenames: string[]) => [...(await createEslintCommands(filenames))],
2727
};
2828

2929
export default group;

packages/lint-staged-config/src/groups/eslint/remove-ignored-files.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const removeIgnoredFiles = async (filenames: string[]): Promise<string[]> => {
88
const eslint = new ESLint();
99
// eslint-disable-next-line compat/compat
1010
const ignoredFiles = await Promise.all(filenames.map((filename) => eslint.isPathIgnored(filename)));
11+
// eslint-disable-next-line security/detect-object-injection
1112
const filteredFiles = filenames.filter((_, index) => !ignoredFiles[index]);
1213

1314
return filteredFiles.map((filename) => `"${isWindows ? filename : quote([filename])}"`);

packages/lint-staged-config/src/groups/tests.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ const hasJest = hasDependency("jest") || hasDevDependency("jest");
88
const hasAva = hasDependency("ava") || hasDevDependency("ava");
99

1010
const group: Config = {
11-
...(hasVitest && { "**/__tests__/**/*.?(c|m)[jt]s?(x)": ["vitest related --run"], "**/?(*.){test,spec}.?(c|m)[jt]s?(x)": ["vitest related --run"] }),
11+
...(hasVitest && { "**/?(*.){test,spec}.?(c|m)[jt]s?(x)": ["vitest related --run"], "**/__tests__/**/*.?(c|m)[jt]s?(x)": ["vitest related --run"] }),
1212
...(hasJest && {
1313
"**/*.spec.{js,ts,tsx}": ["jest --findRelatedTests"],
1414
"**/*.test.{js,ts,tsx}": ["jest --findRelatedTests"],
15-
"**/test/*.{js,ts,tsx}": ["jest --findRelatedTests"],
15+
"**/?(*.){test,spec}.?(c|m)[jt]s?(x)": ["jest --findRelatedTests"],
1616
"**/__mocks__/*.{js,ts,tsx}": ["jest --findRelatedTests"],
17-
"**/__tests__/*.{js,ts,tsx}": ["jest --findRelatedTests"],
1817
"**/__tests__/**/*.?(c|m)[jt]s?(x)": ["jest --findRelatedTests"],
19-
"**/?(*.){test,spec}.?(c|m)[jt]s?(x)": ["jest --findRelatedTests"],
18+
"**/__tests__/*.{js,ts,tsx}": ["jest --findRelatedTests"],
19+
"**/test/*.{js,ts,tsx}": ["jest --findRelatedTests"],
2020
}),
2121
...(hasAva && {
22+
"**/(test|tests|__tests__)/**/*.js": (filenames: string[]) => [`ava ${concatFiles(filenames)}`],
23+
"**/*.(spec|test).js": (filenames: string[]) => [`ava ${concatFiles(filenames)}`],
2224
"**/test.js": (filenames: string[]) => [`ava ${concatFiles(filenames)}`],
2325
"**/test-*.js": (filenames: string[]) => [`ava ${concatFiles(filenames)}`],
24-
"**/*.(spec|test).js": (filenames: string[]) => [`ava ${concatFiles(filenames)}`],
25-
"**/(test|tests|__tests__)/**/*.js": (filenames: string[]) => [`ava ${concatFiles(filenames)}`],
2626
}),
2727
};
2828

packages/lint-staged-config/src/postinstall.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import {
2-
hasDependency, hasDevDependency, packageIsTypeModule, projectPath
3-
} from "@anolilab/package-json-utils";
1+
import { hasDependency, hasDevDependency, packageIsTypeModule, projectPath } from "@anolilab/package-json-utils";
42
import { existsSync, mkdir, writeFile } from "node:fs";
53
import { join } from "node:path";
4+
import { exit } from "node:process";
65
import { promisify } from "node:util";
76

87
import getNearestConfigPath from "./utils/get-nearest-config-path";
98

109
if (process.env["CI"]) {
11-
// eslint-disable-next-line unicorn/no-process-exit
12-
process.exit(0);
10+
exit(0);
1311
}
1412

1513
const writeFileAsync = promisify(writeFile);
@@ -20,6 +18,7 @@ console.log("Configuring @anolilab/lint-staged-config", projectPath, "\n");
2018
const configFile = ".lintstagedrc";
2119

2220
const checkIfFileExists = (filename: string): boolean => {
21+
// eslint-disable-next-line security/detect-non-literal-fs-filename
2322
if (existsSync(filename)) {
2423
console.warn(`⚠️ ${filename} already exists;`);
2524

@@ -78,6 +77,7 @@ const writeHuskyFiles = async () => {
7877

7978
const huskyFolderPath = join(projectPath, ".husky");
8079

80+
// eslint-disable-next-line security/detect-non-literal-fs-filename
8181
if (!existsSync(huskyFolderPath)) {
8282
await mkdirAsync(huskyFolderPath);
8383
}
@@ -209,13 +209,11 @@ echo --------------------------------------------
209209

210210
console.log("😎 Everything went well, have fun!");
211211

212-
// eslint-disable-next-line unicorn/no-process-exit
213-
process.exit(0);
212+
exit(0);
214213
} catch (error) {
215214
console.log("😬 something went wrong:");
216215
console.error(error);
217216

218-
// eslint-disable-next-line unicorn/no-process-exit
219-
process.exit(1);
217+
exit(1);
220218
}
221219
})();

packages/lint-staged-config/src/utils/get-nearest-config-path.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const getNearestConfigPath = <N extends ConfigFileName, A extends AbsolutePath =
3333
const packageRootPath = getNearestPackageRootPath(cwd);
3434
const configPath = joinPaths<[A, N]>([packageRootPath as A, fileName]);
3535

36+
// eslint-disable-next-line security/detect-non-literal-fs-filename
3637
if (existsSync(configPath)) {
3738
return configPath;
3839
}

packages/lint-staged-config/src/utils/logger.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { env } from "node:process";
22

33
const noop = () => undefined;
44

5-
const consolePrefix = (prefix: string) =>
6-
// eslint-disable-next-line implicit-arrow-linebreak
7-
(env["NO_LOGS"] ? noop : (message: string) => console.log(`${prefix}${message}`));
5+
const consolePrefix = (prefix: string) => (env["NO_LOGS"] ? noop : (message: string) => console.log(`${prefix}${message}`));
86

97
const consoleLog = consolePrefix("");
108

packages/package-json-utils/__tests__/index.test.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { dirname } from "node:path";
2-
import {
3-
describe, expect, it, vi,
4-
} from "vitest";
2+
import { describe, expect, it, vi } from "vitest";
53

64
import {
75
environmentIsSet,
8-
fromRoot, getPackageProperty, getPackageSubProperty,
6+
fromRoot,
7+
getPackageProperty,
8+
getPackageSubProperty,
99
hasAnyDep,
1010
hasDep,
1111
hasDevelopmentDep,
@@ -124,27 +124,30 @@ describe("package-json-utils", () => {
124124
expect(isPackageAvailable("vitest2")).toBeFalsy();
125125
});
126126

127-
it.each<"error" | "info" | "log" | "warn">(["warn", "log", "error", "info"])("showMissingPackages: logs a %type message with the missing packages", (type) => {
128-
const consoleMock = vi.spyOn(console, type);
127+
it.each<"error" | "info" | "log" | "warn">(["warn", "log", "error", "info"])(
128+
"showMissingPackages: logs a %type message with the missing packages",
129+
(type) => {
130+
const consoleMock = vi.spyOn(console, type);
129131

130-
showMissingPackages("example", ["package1", "package2"], {
131-
consoleType: type,
132-
});
132+
showMissingPackages("example", ["package1", "package2"], {
133+
consoleType: type,
134+
});
133135

134-
expect(consoleMock).toHaveBeenCalledTimes(1);
135-
expect(consoleMock).toHaveBeenCalledWith(expect.stringContaining("example could not find the following packages"));
136-
expect(consoleMock).toHaveBeenCalledWith(expect.stringContaining("package1"));
137-
expect(consoleMock).toHaveBeenCalledWith(expect.stringContaining("package2"));
136+
expect(consoleMock).toHaveBeenCalledTimes(1);
137+
expect(consoleMock).toHaveBeenCalledWith(expect.stringContaining("example could not find the following packages"));
138+
expect(consoleMock).toHaveBeenCalledWith(expect.stringContaining("package1"));
139+
expect(consoleMock).toHaveBeenCalledWith(expect.stringContaining("package2"));
138140

139-
consoleMock.mockRestore();
140-
});
141+
consoleMock.mockRestore();
142+
},
143+
);
141144

142145
it("showMissingPackages: logs a warning message with the missing packages, pre and post message", () => {
143146
const consoleMock = vi.spyOn(console, "warn");
144147

145148
showMissingPackages("example", ["package1", "package2"], {
146-
preMessage: "pre message",
147149
postMessage: "post message",
150+
preMessage: "pre message",
148151
});
149152

150153
expect(consoleMock).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)