Skip to content

Commit 485ae0c

Browse files
committedJun 23, 2023
feat: added regexp, optimized linting with added files regex to some rules, fixed the testing-library rules, to work correct
Signed-off-by: prisis <d.bannert@anolilab.de>
1 parent ee2cda1 commit 485ae0c

16 files changed

+198
-80
lines changed
 

‎packages/eslint-config/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"eslint-plugin-no-use-extend-native": "^0.5.0",
9090
"eslint-plugin-optimize-regex": "^1.2.1",
9191
"eslint-plugin-promise": "^6.1.1",
92+
"eslint-plugin-regexp": "^1.15.0",
9293
"eslint-plugin-simple-import-sort": "^10.0.0",
9394
"eslint-plugin-sonarjs": "^0.19.0",
9495
"eslint-plugin-sort-keys-fix": "^1.1.2",
@@ -100,8 +101,8 @@
100101
"jsonc-eslint-parser": "^2.3.0",
101102
"read-pkg-up": "^7.0.1",
102103
"semver": "^7.5.1",
103-
"tsup": "^7.0.0",
104104
"toml-eslint-parser": "^0.6.0",
105+
"tsup": "^7.0.0",
105106
"yaml-eslint-parser": "^1.2.2"
106107
},
107108
"devDependencies": {

‎packages/eslint-config/src/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const internalPluginConfig = [
1717
"no-secrets",
1818
"sonarjs",
1919
"security",
20+
"regexp",
2021
// file rules
2122
"jsonc",
2223
"markdown",

‎packages/eslint-config/src/config/plugins/antfu.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { hasTypescript, packageIsTypeModule } from "@anolilab/package-json-utils";
22
import type { Linter } from "eslint";
33

4-
import createConfig from "../../utils/create-config";
4+
import { createConfig } from "../../utils/create-config";
55

66
const config: Linter.Config = createConfig("all", {
77
plugins: ["antfu"],

‎packages/eslint-config/src/config/plugins/array-func.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// eslint-disable-next-line unicorn/prevent-abbreviations
22
import type { Linter } from "eslint";
33

4-
import createConfig from "../../utils/create-config";
4+
import { createConfig } from "../../utils/create-config";
55

66
const config: Linter.Config = createConfig("all", {
77
plugins: ["array-func"],

‎packages/eslint-config/src/config/plugins/babel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { hasAnyDep } from "@anolilab/package-json-utils";
22
import type { Linter } from "eslint";
33

4-
import createConfig from "../../utils/create-config";
4+
import { createConfig } from "../../utils/create-config";
55
import bestPracticesConfig from "../best-practices";
66
import errorsConfig from "../errors";
77
import styleConfig from "../style";

‎packages/eslint-config/src/config/plugins/import.ts

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ const config: Linter.Config = {
257257

258258
// Forbid cyclical dependencies between modules
259259
// https://github.com/benmosher/eslint-plugin-import/blob/d81f48a2506182738409805f5272eff4d77c9348/docs/rules/no-cycle.md
260+
// https://medium.com/@steven-lemon182/are-typescript-barrel-files-an-anti-pattern-72a713004250
260261
"import/no-cycle": ["error", { maxDepth: "∞" }],
261262

262263
// Ensures that there are no useless path segments

‎packages/eslint-config/src/config/plugins/jest.ts

+22-24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const config: Linter.Config = {
2222
],
2323
extends: ["plugin:jest/recommended", "plugin:jest/style"],
2424
env: {
25+
es6: true,
26+
node: true,
2527
jest: true,
2628
},
2729
rules: {
@@ -30,30 +32,26 @@ const config: Linter.Config = {
3032

3133
"jest/no-disabled-tests": "off",
3234

33-
// Not included in jest/recommended
34-
"jest/consistent-test-it": 0,
35-
"jest/lowercase-name": 0,
36-
"jest/no-conditional-expect": 0,
37-
"jest/no-deprecated-functions": 0,
38-
"jest/no-duplicate-hooks": 0,
39-
"jest/no-expect-resolves": 0,
40-
"jest/no-hooks": 0,
41-
"jest/no-if": 0,
42-
"jest/no-interpolation-in-snapshots": 0,
43-
"jest/no-large-snapshots": 0,
44-
"jest/no-restricted-matchers": 0,
45-
"jest/no-test-return-statement": 0,
46-
"jest/no-truthy-falsy": 0,
47-
"jest/prefer-called-with": 0,
48-
"jest/prefer-expect-assertions": 0,
49-
"jest/prefer-hooks-on-top": 0,
50-
"jest/prefer-inline-snapshots": 0,
51-
"jest/prefer-spy-on": 0,
52-
"jest/prefer-strict-equal": 0,
53-
"jest/prefer-todo": 0,
54-
"jest/require-to-throw-message": 0,
55-
"jest/require-top-level-describe": 0,
56-
"jest/valid-title": 0,
35+
"jest/prefer-hooks-in-order": "error",
36+
"jest/prefer-hooks-on-top": "error",
37+
"jest/no-duplicate-hooks": "error",
38+
"jest/no-test-return-statement": "error",
39+
"jest/prefer-strict-equal": "error",
40+
"jest/prefer-to-have-length": "error",
41+
"jest/consistent-test-it": ["error", { fn: "it" }],
42+
43+
// Relax rules that are known to be slow and less useful in a test context
44+
"import/namespace": "off",
45+
"import/default": "off",
46+
"import/no-duplicates": "off",
47+
// Relax rules that makes writing tests easier
48+
"import/no-named-as-default-member": "off",
49+
50+
"@typescript-eslint/no-non-null-assertion": "off",
51+
"@typescript-eslint/no-object-literal-type-assertion": "off",
52+
"@typescript-eslint/no-empty-function": "off",
53+
"@typescript-eslint/no-explicit-any": "off",
54+
"@typescript-eslint/ban-ts-comment": "off",
5755

5856
// you should turn the original rule off *only* for test files
5957
"@typescript-eslint/unbound-method": "off",

‎packages/eslint-config/src/config/plugins/mdx.ts

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ const config: Linter.Config = {
66
{
77
files: ["*.mdx"],
88
extends: ["plugin:mdx/recommended"],
9+
parser: "eslint-mdx",
10+
parserOptions: {
11+
ecmaVersion: "latest",
12+
},
13+
rules: {
14+
"import/namespace": "off",
15+
"import/order": "off",
16+
"@typescript-eslint/naming-convention": "off",
17+
"@typescript-eslint/explicit-module-boundary-types": "off",
18+
"@typescript-eslint/consistent-type-exports": "off",
19+
"@typescript-eslint/no-unused-vars": "off",
20+
},
921
},
1022
],
1123
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
import type { Linter } from "eslint";
22

3+
import { createConfigs } from "../../utils/create-config";
4+
35
// @see https://github.com/SonarSource/eslint-plugin-sonarjs
4-
const config: Linter.Config = {
5-
extends: ["plugin:sonarjs/recommended"],
6-
plugins: ["sonarjs"],
7-
};
6+
const config: Linter.Config = createConfigs([
7+
{
8+
type: "all",
9+
config: {
10+
excludedFiles: ["**/?(*.)+(test).{js,jsx,ts,tsx}", "*.stories.{js,ts,jsx,tsx}"],
11+
extends: ["plugin:sonarjs/recommended"],
12+
rules: {
13+
"sonarjs/no-nested-template-literals": "off",
14+
},
15+
},
16+
},
17+
{
18+
type: "js_and_ts",
19+
config: {
20+
rules: {
21+
// relax complexity for react code
22+
"sonarjs/cognitive-complexity": ["error", 15],
23+
// relax duplicate strings
24+
"sonarjs/no-duplicate-string": "off",
25+
},
26+
},
27+
},
28+
{
29+
type: "javascript",
30+
config: {
31+
parser: "espree",
32+
parserOptions: {
33+
ecmaVersion: 2020,
34+
},
35+
rules: {
36+
"sonarjs/no-duplicate-string": "off",
37+
"sonarjs/no-all-duplicated-branches": "off",
38+
},
39+
},
40+
},
41+
]);
842

943
export default config;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import type { Linter } from "eslint";
22

3-
const config: Linter.Config = {
4-
plugins: ["tailwindcss"],
5-
extends: ["plugin:tailwindcss/recommended"],
6-
};
3+
import { createConfig } from "../../utils/create-config";
4+
5+
// @see https://github.com/francoismassart/eslint-plugin-tailwindcss,
6+
const config: Linter.Config = createConfig(
7+
"jsx_and_tsx",
8+
{
9+
plugins: ["tailwindcss"],
10+
extends: ["plugin:tailwindcss/recommended"],
11+
},
12+
{
13+
browser: true,
14+
es6: true,
15+
node: true,
16+
},
17+
);
718

819
export default config;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { hasAnyDep, pkg } from "@anolilab/package-json-utils";
22
import type { Linter } from "eslint";
33

4+
import { createConfig } from "../../utils/create-config";
45
import { consolePlugin } from "../../utils/loggers";
56

67
let anolilabEslintConfig: { [key: string]: boolean | undefined } = {};
@@ -25,24 +26,17 @@ if (!global.hasAnolilabEsLintTestConfigLoaded) {
2526
global.hasAnolilabEsLintTestConfigLoaded = true;
2627
}
2728

28-
const config: Linter.Config = {
29-
extends: [`plugin:testing-library/${ruleset}`],
30-
rules: {
31-
// Not included in jest/recommended
32-
"testing-library/await-fire-event": "off",
33-
"testing-library/consistent-data-testid": "off",
34-
"testing-library/no-debug": "off",
35-
"testing-library/no-dom-import": "off",
36-
"testing-library/no-manual-cleanup": "off",
37-
"testing-library/no-render-in-setup": "off",
38-
"testing-library/no-await-sync-events": "off",
39-
"testing-library/no-wait-for-empty-callback": "off",
40-
"testing-library/no-wait-for-snapshot": "off",
41-
"testing-library/prefer-explicit-assert": "off",
42-
"testing-library/prefer-presence-queries": "off",
43-
"testing-library/prefer-screen-queries": "off",
44-
"testing-library/prefer-wait-for": "off",
29+
// For performance enable react-testing-library only on test files
30+
const config: Linter.Config = createConfig(
31+
"tests",
32+
{
33+
extends: [`plugin:testing-library/${ruleset}`],
4534
},
46-
};
35+
{
36+
browser: true,
37+
es6: true,
38+
node: true,
39+
},
40+
);
4741

4842
export default config;

‎packages/eslint-config/src/config/plugins/tsdoc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Linter } from "eslint";
22

3-
import createConfig from "../../utils/create-config";
3+
import { createConfig } from "../../utils/create-config";
44

55
const config: Linter.Config = createConfig("typescript", {
66
plugins: ["eslint-plugin-tsdoc"],

‎packages/eslint-config/src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
/**
2-
* @rushstack/eslint-patch is used to include plugins as dev
2+
* rushstack eslint-patch is used to include plugins as dev
33
* dependencies instead of imposing them as peer dependencies
44
*
5-
* https://www.npmjs.com/package/@rushstack/eslint-patch
5+
* {@link https://www.npmjs.com/package/@rushstack/eslint-patch}
6+
* {@link https://stackoverflow.com/a/74478635/1392749}
7+
* {@link https://github.com/eslint/eslint/issues/3458}
8+
* {@link https://eslint.org/blog/2022/08/new-config-system-part-1/}
9+
* {@link https://eslint.org/blog/2022/08/new-config-system-part-2/}
10+
* {@link https://eslint.org/blog/2022/08/new-config-system-part-3/}
11+
* {@link https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new}
612
*/
713
import "@rushstack/eslint-patch/modern-module-resolution";
814

‎packages/eslint-config/src/typescript-type-checking.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Linter } from "eslint";
22

33
import bestPracticesConfig from "./config/best-practices";
4-
import createConfig from "./utils/create-config";
4+
import { createConfig } from "./utils/create-config";
55

66
const bestPracticesRules = bestPracticesConfig.rules as Linter.RulesRecord;
77

Original file line numberDiff line numberDiff line change
@@ -1,45 +1,106 @@
11
import type { Linter } from "eslint";
22

3-
const createConfig = (
4-
type: "all" | "javascript" | "js_and_ts" | "jsx_and_tsx" | "typescript",
5-
config: Omit<Linter.Config, "files|overrides">,
6-
): Linter.Config => {
7-
let files = ["*.js", "*.mjs", "*.cjs"];
3+
type FileType = "all" | "ava" | "javascript" | "jest" | "js_and_ts" | "jsx_and_tsx" | "tests" | "typescript" | "vitest";
84

9-
// eslint-disable-next-line default-case
5+
const getType = (type: FileType) => {
106
switch (type) {
117
case "typescript": {
128
// @see https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#new-file-extensions
13-
files = ["*.ts", "*.tsx", "*.mts", "*.cts"];
14-
15-
break;
9+
return ["*.ts", "*.tsx", "*.mts", "*.cts"];
1610
}
1711
case "jsx_and_tsx": {
18-
files = ["*.jsx", "*.tsx"];
19-
20-
break;
12+
return ["*.jsx", "*.tsx"];
2113
}
2214
case "js_and_ts": {
23-
files = ["*.js", "*.mjs", "*.cjs", "*.ts", "*.mts", "*.cts"];
24-
25-
break;
15+
return ["*.js", "*.mjs", "*.cjs", "*.ts", "*.mts", "*.cts"];
2616
}
2717
case "all": {
28-
files = ["*.js", "*.jsx", "*.mjs", "*.cjs", "*.ts", "*.tsx", "*.mts", "*.cts"];
18+
return ["*.js", "*.jsx", "*.mjs", "*.cjs", "*.ts", "*.tsx", "*.mts", "*.cts"];
19+
}
20+
case "jest": {
21+
return [
22+
// Test files
23+
"**/*.spec.{js,ts,tsx}",
24+
"**/*.test.{js,ts,tsx}",
25+
"**/test/*.{js,ts,tsx}",
26+
27+
// Facebook convention
28+
"**/__mocks__/*.{js,ts,tsx}",
29+
"**/__tests__/*.{js,ts,tsx}",
30+
];
31+
}
32+
case "ava": {
33+
return [
34+
"test.js",
35+
"src/test.js",
36+
"source/test.js",
37+
"**/test-*.js",
38+
"**/*.spec.js",
39+
"**/*.test.js",
40+
"**/test/**/*.js",
41+
"**/tests/**/*.js",
42+
"**/__tests__/**/*.js",
43+
];
44+
}
45+
case "vitest": {
46+
return ["**/__tests__/**/*.?(c|m)[jt]s?(x)", "**/?(*.){test,spec}.?(c|m)[jt]s?(x)"];
47+
}
48+
case "tests": {
49+
return [
50+
// ava
51+
"test.js",
52+
"src/test.js",
53+
"source/test.js",
54+
"**/test-*.js",
55+
"**/*.spec.js",
56+
"**/*.test.js",
57+
"**/test/**/*.js",
58+
"**/tests/**/*.js",
59+
"**/__tests__/**/*.js",
2960

30-
break;
61+
// jest
62+
"**/*.spec.{js,ts,tsx}",
63+
"**/*.test.{js,ts,tsx}",
64+
"**/test/*.{js,ts,tsx}",
65+
"**/__mocks__/*.{js,ts,tsx}",
66+
"**/__tests__/*.{js,ts,tsx}",
67+
"**/__tests__/**/*.?(c|m)[jt]s?(x)",
68+
"**/?(*.){test,spec}.?(c|m)[jt]s?(x)",
69+
];
70+
}
71+
default: {
72+
throw new Error(`Unknown type: ${type}`);
3173
}
32-
// No default
3374
}
75+
};
3476

77+
export const createConfig = (
78+
type: FileType,
79+
config: Omit<Linter.ConfigOverride<Linter.RulesRecord>, "files">,
80+
environment?: { [name: string]: boolean },
81+
): Linter.Config => {
3582
return {
83+
env: environment,
3684
overrides: [
3785
{
38-
files,
86+
files: getType(type),
3987
...config,
4088
},
4189
],
4290
};
4391
};
44-
45-
export default createConfig;
92+
export const createConfigs = (
93+
rules: {
94+
config: Omit<Linter.ConfigOverride<Linter.RulesRecord>, "files">;
95+
type: FileType;
96+
}[],
97+
): Linter.Config => {
98+
return {
99+
overrides: rules.map(({ type, config }) => {
100+
return {
101+
files: getType(type),
102+
...config,
103+
};
104+
}),
105+
};
106+
};

‎packages/eslint-config/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
"$schema": "https://json.schemastore.org/tsconfig",
33
"display": "Node 16",
44
"extends": "../../tsconfig.json",
5-
"baseUrl": "./",
65
"include": ["src/**/*", "*.d.ts"]
76
}

0 commit comments

Comments
 (0)
Please sign in to comment.