@braineet/lint-staged-config
Version:
Shared lint-staged configuration for Braineet Frontend projects
51 lines (43 loc) • 1.52 kB
JavaScript
/* eslint-disable import/no-extraneous-dependencies */
const escape = require('shell-quote').quote;
const isWin = process.platform === 'win32';
const getEslintFixCmd = ({
files,
rules,
fix = true,
fixType,
cache = true,
maxWarnings,
quiet = true,
}) => {
const cliRules = [...(rules ?? [])]
.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 ')}` : '',
quiet ? `--quiet` : '',
files.join(' '),
].join(' ');
return `eslint ${args}`;
};
const concatFilesForPrettier = filenames =>
filenames
.map(filename => `"${isWin ? filename : escape([filename])}"`)
.join(' ');
module.exports = {
'**/*.{js,jsx,ts,tsx}': filenames =>
getEslintFixCmd({
files: filenames,
}),
'**/*.{json,md,mdx,css,html,yml,yaml,scss,ts,js,tsx,jsx,mjs}':
filenames => [`prettier --write ${concatFilesForPrettier(filenames)}`],
};