-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-staged.common.mjs
61 lines (57 loc) · 2.42 KB
/
lint-staged.common.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import path from 'path';
const eslintGlobalRulesForFix = [
// react-hooks/eslint and react in general is very strict about exhaustively
// declaring the dependencies when using the useEffect, useCallback... hooks.
//
// In some specific scenarios declaring the deps seems 'less' wanted or 'less' applicable
// by the developer, leading to some exceptions in the code. That said it should be avoided.
//
// While the 'react-hooks/exhaustive-deps' rule is a good rule of thumb, it's not recommended to
// automatically fix it from lint-staged as it can potentially break a legit intent.
//
// Reminder that a good practice is to always declare the dependencies when using the hooks,
// and if not applicable, add an eslint disable comment to the useEffect, useCallback... such as:
//
// // eslint-disable-next-line react-hooks/exhaustive-deps
//
// Another approach can be to use hooks such as https://github.com/kentcdodds/use-deep-compare-effect to quickly bypass
// shallow rendering limitations.
//
// @see https://reactjs.org/docs/hooks-rules.html
// @see https://eslint.org/docs/2.13.1/user-guide/configuring#disabling-rules-with-inline-comments
'react-hooks/exhaustive-deps: off',
];
/**
* Lint-staged command for running eslint in packages or apps.
* @param {{cwd: string, files: string[], fix: boolean, fixType?: ('problem'|'suggestion'|'layout'|'directive')[], cache: boolean, rules?: string[], maxWarnings?: number}} params
*/
export const getEslintFixCmd = ({
cwd,
files,
rules,
fix,
fixType,
cache,
maxWarnings,
}) => {
const cliRules = [...(rules ?? []), ...eslintGlobalRulesForFix]
.filter((rule) => rule.trim().length > 0)
.map((r) => `"${r.trim()}"`);
// For lint-staged it's safer to not apply the fix command if it changes the AST
// @see https://eslint.org/docs/user-guide/command-line-interface#--fix-type
const cliFixType = [...(fixType ?? ['layout'])].filter(
(type) => type.trim().length > 0
);
const args = [
cache ? '--cache' : '',
fix ? '--fix' : '',
cliFixType.length > 0 ? `--fix-type ${cliFixType.join(',')}` : '',
maxWarnings !== undefined ? `--max-warnings=${maxWarnings}` : '',
cliRules.length > 0 ? `--rule ${cliRules.join(' --rule ')}` : '',
files
// makes output cleaner by removing absolute paths from filenames
.map((f) => `"./${path.relative(cwd, f)}"`)
.join(' '),
].join(' ');
return `eslint ${args}`;
};