Skip to content

Commit ab2a954

Browse files
committedJun 18, 2023
fix(eslint-config): disabling all rules that are crashing with the prettier config, updated the readme with the info
Signed-off-by: prisis <d.bannert@anolilab.de>
1 parent 744e6b3 commit ab2a954

File tree

11 files changed

+215
-35
lines changed

11 files changed

+215
-35
lines changed
 

‎packages/eslint-config/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The goal is to reduce noise in code version control and promote use of the lates
3434
- Includes many useful ESLint plugins, like [unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn), [import](https://github.com/benmosher/eslint-plugin-import) and [more](#plugins).
3535
- Automatically enables rules based on the [engines](https://docs.npmjs.com/files/package.json#engines) field in your package.json.
3636
- Specify indent and semicolon preferences easily without messing with the rule config.
37-
- Disables rules that conflict with Prettier.
37+
- Disables rules that conflict with [Prettier](#let-prettier-handle-style-related-rules).
3838

3939
## Install
4040

@@ -215,13 +215,13 @@ Default: `undefined`
215215

216216
Prettier is a code formatting tool that offers fewer options but is more professional than the style-related rules in ESLint.
217217

218-
Now that Prettier has become a necessary tool in front end projects, `@anolilab/eslint-config` does not need to maintain the style-related rules in ESLint anymore, so we completely removed all Prettier related rules in the v3 version, and use ESLint to check logical errors which it’s good at.
218+
Now that Prettier has become a necessary tool in front end projects, `@anolilab/eslint-config` does not need to maintain the style-related rules in ESLint anymore,
219+
so we completely removed all Prettier related rules, if `prettier` is found in your `package.json` and use ESLint to check logical errors which it’s good at.
219220

220-
As for whether two spaces or four spaces are used for indentation and whether there is a semicolon at the end, you can configure it in the project’s .prettierrc.cjs. Of course, we also provide a recommended Prettier configuration for your reference.
221+
As for whether two spaces or four spaces are used for indentation and whether there is a semicolon at the end, you can configure it in the project’s `.prettierrc.js`.
222+
Of course, we also provide a recommended Prettier [configuration](../prettier-config/README.md) for your reference.
221223

222-
`@anolilab/eslint-config` does not include all style-related rules, so there is no need to install `eslint-config-prettier`. Install `prettier` and if you use `VSCode` the related plugins.
223-
224-
This the used [.prettierrc.cjs](../prettier-config/index.cjs) configuration by Anolilab Team only for reference.
224+
`@anolilab/eslint-config` does disable all included style-related rules, so there is no need to install [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier).
225225

226226
## Using experimental features with JavaScript
227227

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const pluginConfig: PackageRules = [
142142
const loadedPlugins: string[] = [...internalPluginConfig];
143143
const possiblePlugins: { [rule: string]: { [packageName: string]: boolean } } = {};
144144

145-
let anolilabEslintConfig: { [key: string]: { [key: string]: false | undefined} } = {};
145+
let anolilabEslintConfig: { [key: string]: { [key: string]: false | undefined } } = {};
146146

147147
if (pkg) {
148148
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access

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

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { hasAnyDep } from "@anolilab/package-json-utils";
12
import type { Linter } from "eslint";
23

34
import createConfig from "../../utils/create-config";
5+
import { consoleLog } from "../../utils/loggers";
46
import bestPracticesConfig from "../best-practices";
57
import errorsConfig from "../errors";
68
import styleConfig from "../style";
@@ -9,6 +11,28 @@ const bestPracticesRules = bestPracticesConfig.rules as Linter.RulesRecord;
911
const errorsRules = errorsConfig.rules as Linter.RulesRecord;
1012
const styleRules = styleConfig.rules as Linter.RulesRecord;
1113

14+
let prettierRules: Linter.RulesRecord = {};
15+
16+
if (
17+
hasAnyDep(["prettier"], {
18+
peerDeps: false,
19+
})
20+
) {
21+
// Workaround VS Code trying to run this file twice!
22+
if (!global.hasAnolilabEsLintConfigBabelPrettier) {
23+
global.hasAnolilabEsLintConfigBabelPrettier = true;
24+
25+
consoleLog("\nFound prettier as dependency, disabling some rules to fix wrong behavior of the rule with eslint and prettier");
26+
}
27+
28+
prettierRules = {
29+
"babel/quotes": 0,
30+
31+
"@babel/object-curly-spacing": "off",
32+
"@babel/semi": "off",
33+
};
34+
}
35+
1236
const config: Linter.Config = createConfig("all", {
1337
plugins: ["babel"],
1438
rules: {
@@ -39,6 +63,8 @@ const config: Linter.Config = createConfig("all", {
3963

4064
"valid-typeof": "off",
4165
"babel/valid-typeof": errorsRules["valid-typeof"],
66+
67+
...prettierRules,
4268
},
4369
});
4470

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ const overrides: Linter.Config["overrides"] = [
1212
];
1313

1414
if (hasTypescript) {
15-
if (hasAnyDep(["eslint-plugin-tsdoc"])) {
15+
if (
16+
hasAnyDep(["eslint-plugin-tsdoc"], {
17+
peerDeps: false,
18+
})
19+
) {
1620
consoleLog("\nFound eslint-plugin-tsdoc as dependency, disabling the jsdoc rules for *.ts and *.tsx files.");
1721
} else {
1822
overrides.push({

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { consoleLog } from "../../utils/loggers";
55

66
const extendedPlugins: string[] = [];
77

8-
if (hasAnyDep(["prettier"])) {
8+
if (
9+
hasAnyDep(["prettier"], {
10+
peerDeps: false,
11+
})
12+
) {
913
// Workaround VS Code trying to run this file twice!
1014
if (!global.hasAnolilabEsLintConfigJsonCPrettier) {
1115
global.hasAnolilabEsLintConfigJsonCPrettier = true;

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ const config: Linter.Config = {
9090
files: ["*.js", "*.mjs", "*.cjs"],
9191
rules: {
9292
// We have this enabled in addition to `import/extensions` as this one has an auto-fix.
93-
"n/file-extension-in-import": [
94-
"error",
95-
"always",
96-
],
93+
"n/file-extension-in-import": ["error", "always"],
9794
},
9895
},
9996
],

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,33 @@ const dangleRules = styleRules["no-underscore-dangle"] as any[];
1212
let prettierReactRules = {};
1313

1414
if (
15-
hasAnyDep(["react", "react-dom"], {
15+
hasAnyDep(["prettier"], {
1616
peerDeps: false,
1717
})
1818
) {
19+
// Workaround VS Code trying to run this file twice!
20+
if (!global.hasAnolilabEsLintConfigReactPrettier) {
21+
global.hasAnolilabEsLintConfigReactPrettier = true;
22+
23+
consoleLog("\nFound prettier as dependency, disabling some rules to fix wrong behavior of the rule with eslint and prettier");
24+
}
25+
1926
prettierReactRules = {
20-
"react/jsx-indent": "off",
21-
"react/jsx-closing-tag-location": "off",
27+
"react/jsx-child-element-spacing": "off",
2228
"react/jsx-closing-bracket-location": "off",
29+
"react/jsx-closing-tag-location": "off",
30+
"react/jsx-curly-newline": "off",
2331
"react/jsx-curly-spacing": "off",
2432
"react/jsx-equals-spacing": "off",
2533
"react/jsx-first-prop-new-line": "off",
34+
"react/jsx-indent": "off",
2635
"react/jsx-indent-props": "off",
2736
"react/jsx-max-props-per-line": "off",
28-
"react/jsx-tag-spacing": "off",
29-
"react/jsx-wrap-multilines": "off",
30-
"react/prefer-stateless-function": "off",
37+
"react/jsx-newline": "off",
3138
"react/jsx-one-expression-per-line": "off",
3239
"react/jsx-props-no-multi-spaces": "off",
40+
"react/jsx-tag-spacing": "off",
41+
"react/jsx-wrap-multilines": "off",
3342
};
3443
}
3544

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

+33-6
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,43 @@ const importsRules = importsConfig.rules as Linter.RulesRecord;
2020
const variablesRules = variablesConfig.rules as Linter.RulesRecord;
2121

2222
const { quotes, semi } = styleRules;
23-
let { indent } = styleRules;
23+
const { indent } = styleRules;
2424

25-
if (hasAnyDep(["prettier"])) {
25+
let prettierRules: Linter.RulesRecord = {};
26+
27+
if (
28+
hasAnyDep(["prettier"], {
29+
peerDeps: false,
30+
})
31+
) {
2632
// Workaround VS Code trying to run this file twice!
2733
if (!global.hasAnolilabEsLintConfigTypescriptPrettier) {
2834
global.hasAnolilabEsLintConfigTypescriptPrettier = true;
2935

30-
consoleLog(
31-
"\nFound prettier as dependency, disabling the '@typescript-eslint/indent' rule to fix wrong behavior of the rule; @see https://github.com/typescript-eslint/typescript-eslint/issues/1824",
32-
);
36+
consoleLog("\nFound prettier as dependency, disabling some rules to fix wrong behavior of the rule with eslint and prettier");
3337
}
3438

35-
indent = "off";
39+
prettierRules = {
40+
"@typescript-eslint/lines-around-comment": 0,
41+
"@typescript-eslint/quotes": 0,
42+
"@typescript-eslint/block-spacing": "off",
43+
"@typescript-eslint/brace-style": "off",
44+
"@typescript-eslint/comma-dangle": "off",
45+
"@typescript-eslint/comma-spacing": "off",
46+
"@typescript-eslint/func-call-spacing": "off",
47+
"@typescript-eslint/indent": "off",
48+
"@typescript-eslint/key-spacing": "off",
49+
"@typescript-eslint/keyword-spacing": "off",
50+
"@typescript-eslint/member-delimiter-style": "off",
51+
"@typescript-eslint/no-extra-parens": "off",
52+
"@typescript-eslint/no-extra-semi": "off",
53+
"@typescript-eslint/object-curly-spacing": "off",
54+
"@typescript-eslint/semi": "off",
55+
"@typescript-eslint/space-before-blocks": "off",
56+
"@typescript-eslint/space-before-function-paren": "off",
57+
"@typescript-eslint/space-infix-ops": "off",
58+
"@typescript-eslint/type-annotation-spacing": "off",
59+
};
3660
}
3761

3862
const importExtensions = importsRules["import/extensions"] as any[];
@@ -490,6 +514,9 @@ const config: Linter.Config = {
490514

491515
// https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin/docs/rules/no-import-type-side-effects.md
492516
"@typescript-eslint/no-import-type-side-effects": "error",
517+
518+
// Disable rules that are handled by prettier
519+
...prettierRules,
493520
},
494521
},
495522
{

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@ import { hasAnyDep, packageIsTypeModule } from "@anolilab/package-json-utils";
22
import type { Linter } from "eslint";
33
import semver from "semver";
44

5+
import { consoleLog } from "../../utils/loggers";
56
import styleConfig from "../style";
67

78
const styleRules = styleConfig.rules as Linter.RulesRecord;
89

9-
let identLevel: Linter.RuleLevel = "error";
10+
let prettierRules: Linter.RulesRecord = {};
1011

11-
if (hasAnyDep(["prettier"])) {
12-
identLevel = "off";
12+
if (
13+
hasAnyDep(["prettier"], {
14+
peerDeps: false,
15+
})
16+
) {
17+
// Workaround VS Code trying to run this file twice!
18+
if (!global.hasAnolilabEsLintConfigUnicornPrettier) {
19+
global.hasAnolilabEsLintConfigUnicornPrettier = true;
20+
21+
consoleLog("\nFound prettier as dependency, disabling some rules to fix wrong behavior of the rule with eslint and prettier");
22+
}
23+
24+
prettierRules = {
25+
"unicorn/empty-brace-spaces": "off",
26+
"unicorn/no-nested-ternary": "off",
27+
"unicorn/number-literal-case": "off",
28+
"unicorn/template-indent": "off",
29+
};
1330
}
1431

1532
// @see https://github.com/sindresorhus/eslint-plugin-unicorn
@@ -18,7 +35,7 @@ const config: Linter.Config = {
1835
extends: ["plugin:unicorn/recommended"],
1936
rules: {
2037
"unicorn/prefer-node-protocol": semver.gte(process.version, "v16.0.0") ? "error" : "off",
21-
"unicorn/template-indent": [identLevel, { indent: (styleRules["indent"] as any[])[1] as number }],
38+
"unicorn/template-indent": ["error", { indent: (styleRules["indent"] as any[])[1] as number }],
2239
"unicorn/no-array-for-each": "off",
2340
"unicorn/prefer-module": packageIsTypeModule ? "error" : "off",
2441

@@ -50,6 +67,8 @@ const config: Linter.Config = {
5067

5168
// TODO: Temporarily disabled as the rule is buggy.
5269
"function-call-argument-newline": "off",
70+
71+
...prettierRules,
5372
},
5473
overrides: [
5574
{

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

+8
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ declare global {
88
var hasAnolilabEsLintConfigReactRuntimePath: boolean;
99

1010
var hasAnolilabEsLintTestConfigLoaded: boolean;
11+
12+
var hasAnolilabEsLintConfigUnicornPrettier: boolean;
13+
14+
var hasAnolilabEsLintConfigReactPrettier: boolean;
15+
16+
var hasAnolilabEsLintConfigBabelPrettier: boolean;
17+
18+
var hasAnolilabEsLintConfigPrettier: boolean;
1119
}

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

+92-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import "@rushstack/eslint-patch/modern-module-resolution";
88

9-
import { packageIsTypeModule, pkg } from "@anolilab/package-json-utils";
9+
import { hasAnyDep, packageIsTypeModule, pkg } from "@anolilab/package-json-utils";
1010
import type { Linter } from "eslint";
1111
import { join } from "node:path";
1212
import semver from "semver";
@@ -67,8 +67,7 @@ if (!global.hasAnolilabEsLintConfigLoaded) {
6767
global.hasAnolilabEsLintConfigLoaded = true;
6868
}
6969

70-
const configRules: Linter.RulesRecord = {};
71-
70+
let configRules: Linter.RulesRecord = {};
7271
let nodeVersion: string | undefined;
7372

7473
if (pkg?.engines?.["node"]) {
@@ -85,6 +84,95 @@ Object.entries(engineRules).forEach(([rule, ruleConfig]) => {
8584
});
8685
});
8786

87+
if (
88+
hasAnyDep(["prettier"], {
89+
peerDeps: false,
90+
})
91+
) {
92+
// Workaround VS Code trying to run this file twice!
93+
if (!global.hasAnolilabEsLintConfigPrettier) {
94+
global.hasAnolilabEsLintConfigPrettier = true;
95+
96+
consoleLog("\nFound prettier as dependency, disabling some rules to fix wrong behavior of the rule with eslint and prettier");
97+
}
98+
99+
configRules = {
100+
...configRules,
101+
102+
// The following rules can be used in some cases. See the README for more
103+
// information. (These are marked with `0` instead of `"off"` so that a
104+
// script can distinguish them.)
105+
curly: 0,
106+
"lines-around-comment": 0,
107+
"max-len": 0,
108+
"no-confusing-arrow": 0,
109+
"no-mixed-operators": 0,
110+
"no-tabs": 0,
111+
"no-unexpected-multiline": 0,
112+
quotes: 0,
113+
114+
// The rest are rules that you never need to enable when using Prettier.
115+
"array-bracket-newline": "off",
116+
"array-bracket-spacing": "off",
117+
"array-element-newline": "off",
118+
"arrow-parens": "off",
119+
"arrow-spacing": "off",
120+
"block-spacing": "off",
121+
"brace-style": "off",
122+
"comma-dangle": "off",
123+
"comma-spacing": "off",
124+
"comma-style": "off",
125+
"computed-property-spacing": "off",
126+
"dot-location": "off",
127+
"eol-last": "off",
128+
"func-call-spacing": "off",
129+
"function-call-argument-newline": "off",
130+
"function-paren-newline": "off",
131+
"generator-star-spacing": "off",
132+
"implicit-arrow-linebreak": "off",
133+
indent: "off",
134+
"jsx-quotes": "off",
135+
"key-spacing": "off",
136+
"keyword-spacing": "off",
137+
"linebreak-style": "off",
138+
"multiline-ternary": "off",
139+
"newline-per-chained-call": "off",
140+
"new-parens": "off",
141+
"no-extra-parens": "off",
142+
"no-extra-semi": "off",
143+
"no-floating-decimal": "off",
144+
"no-mixed-spaces-and-tabs": "off",
145+
"no-multi-spaces": "off",
146+
"no-multiple-empty-lines": "off",
147+
"no-trailing-spaces": "off",
148+
"no-whitespace-before-property": "off",
149+
"nonblock-statement-body-position": "off",
150+
"object-curly-newline": "off",
151+
"object-curly-spacing": "off",
152+
"object-property-newline": "off",
153+
"one-var-declaration-per-line": "off",
154+
"operator-linebreak": "off",
155+
"padded-blocks": "off",
156+
"quote-props": "off",
157+
"rest-spread-spacing": "off",
158+
semi: "off",
159+
"semi-spacing": "off",
160+
"semi-style": "off",
161+
"space-before-blocks": "off",
162+
"space-before-function-paren": "off",
163+
"space-in-parens": "off",
164+
"space-infix-ops": "off",
165+
"space-unary-ops": "off",
166+
"switch-colon-spacing": "off",
167+
"template-curly-spacing": "off",
168+
"template-tag-spacing": "off",
169+
"unicode-bom": "off",
170+
"wrap-iife": "off",
171+
"wrap-regex": "off",
172+
"yield-star-spacing": "off",
173+
};
174+
}
175+
88176
const config: Linter.Config = {
89177
// After an .eslintrc.js file is loaded, ESLint will normally continue visiting all parent folders
90178
// to look for other .eslintrc.js files, and also consult a personal file ~/.eslintrc.js. If any files
@@ -98,9 +186,7 @@ const config: Linter.Config = {
98186
parser: "",
99187

100188
extends: [
101-
...rules
102-
103-
.map((plugin) => join(__dirname, `./config/${plugin}.${packageIsTypeModule ? "m" : ""}js`)),
189+
...rules.map((plugin) => join(__dirname, `./config/${plugin}.${packageIsTypeModule ? "m" : ""}js`)),
104190

105191
...pluginRules.map((plugin) => join(__dirname, `./config/plugins/${plugin}.${packageIsTypeModule ? "m" : ""}js`)),
106192
],

0 commit comments

Comments
 (0)
Please sign in to comment.