Skip to content

Commit

Permalink
chore: update lint-staged
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperse-net committed May 25, 2024
1 parent 4b5fd62 commit 50954ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
20 changes: 6 additions & 14 deletions lint-staged.common.cjs → lint-staged.common.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { relative } = require('path');
const { quote } = require('shell-quote');
import path from 'path';
import { quote } from 'shell-quote';

const isWin = process.platform === 'win32';

Expand Down Expand Up @@ -30,7 +30,7 @@ const eslintGlobalRulesForFix = [
* 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 = ({
export const getEslintFixCmd = ({
cwd,
files,
rules,
Expand All @@ -54,10 +54,10 @@ const getEslintFixCmd = ({
fix ? '--fix' : '',
cliFixType.length > 0 ? `--fix-type ${cliFixType.join(',')}` : '',
maxWarnings !== undefined ? `--max-warnings=${maxWarnings}` : '',
cliRules.length > 0 ? `--rule ${cliRules.join('--rule ')}` : '',
cliRules.length > 0 ? `--rule ${cliRules.join(' --rule ')}` : '',
files
// makes output cleaner by removing absolute paths from filenames
.map((f) => `"./${relative(cwd, f)}"`)
.map((f) => `"./${path.relative(cwd, f)}"`)
.join(' '),
].join(' ');
return `eslint ${args}`;
Expand All @@ -74,15 +74,7 @@ const getEslintFixCmd = ({
* @param {string[]} filenames
* @returns {string} Return concatenated and escaped filenames
*/
const concatFilesForPrettier = (filenames) =>
export const concatFilesForPrettier = (filenames) =>
filenames
.map((filename) => `"${isWin ? filename : quote([filename])}"`)
.join(' ');

const concatFilesForStylelint = concatFilesForPrettier;

module.exports = {
concatFilesForPrettier,
concatFilesForStylelint,
getEslintFixCmd,
};
23 changes: 18 additions & 5 deletions lint-staged.config.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
/**
* This is the base lint-staged rules config and just includes prettier by default.
* A good practice is to override this base configuration in each package and/or application
* where we are able to add customization depending on the nature of the project (eslint...).
*
* {@link https://github.com/okonet/lint-staged#how-to-use-lint-staged-in-a-multi-package-monorepo}
* {@link https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docs/about-lint-staged.md}
*/

import { concatFilesForPrettier } from './lint-staged.common.cjs';
import {
concatFilesForPrettier,
getEslintFixCmd,
} from './lint-staged.common.mjs';

/**
* @type {Record<string, (filenames: string[]) => string | string[] | Promise<string | string[]>>}
*/
const rules = {
'**/*.{js,jsx,ts,tsx,mjs,cjs}': (filenames) => {
return getEslintFixCmd({
cwd: dirname(fileURLToPath(import.meta.url)),
fix: true,
cache: true,
// when autofixing staged-files a good tip is to disable react-hooks/exhaustive-deps, cause
// a change here can potentially break things without proper visibility.
rules: ['react-hooks/exhaustive-deps: off'],
maxWarnings: 25,
files: filenames,
});
},
'**/*.{json,md,mdx,css,html,yml,yaml,scss,ts,js,tsx,jsx,mjs}': (
filenames
) => {
Expand Down
2 changes: 2 additions & 0 deletions next-mate.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma",
},
"i18n-ally.localesPaths": ["i18n"],
"i18n-ally.keystyle": "nested",
},
}

0 comments on commit 50954ae

Please sign in to comment.