-
-
Notifications
You must be signed in to change notification settings - Fork 582
/
lint-staged.common.js
90 lines (81 loc) · 3.19 KB
/
lint-staged.common.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// @ts-check
const path = require('path');
const escape = require('shell-quote').quote;
const isWin = process.platform === 'win32';
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
*/
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}`;
};
/**
* Concatenate and escape a list of filenames that can be passed as args to prettier cli
*
* Prettier has an issue with special characters in filenames,
* such as the ones uses for nextjs dynamic routes (ie: [id].tsx...)
*
* @link https://github.com/okonet/lint-staged/issues/676
*
* @param {string[]} filenames
* @returns {string} Return concatenated and escaped filenames
*/
const concatFilesForPrettier = (filenames) =>
filenames
.map((filename) => `"${isWin ? filename : escape([filename])}"`)
.join(' ');
const concatFilesForStylelint = concatFilesForPrettier;
module.exports = {
concatFilesForPrettier,
concatFilesForStylelint,
getEslintFixCmd,
};