From ff656de82b26d23e0618bfb351650e52656ad0f2 Mon Sep 17 00:00:00 2001 From: William Grant Date: Mon, 4 Sep 2023 16:33:59 +1000 Subject: [PATCH 1/2] fix: lint-staged ignores files using eslintignore --- .lintstagedrc.js | 53 ++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 61152b6fb..d1b83dbc7 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,25 +1,38 @@ -const ignoredFiles = [ - 'package.json', - 'yarn.lock', - 'tsconfig.json', - '.lintstagedrc.js', - '.eslintrc.js', -]; - -const path = require('path'); - -const buildFormatCommand = filenames => { - const results = filenames - .map(f => path.relative(process.cwd(), f)) - .filter(f => !ignoredFiles.includes(f)); - - return results.length ? `prettier --list-different --write ${results.join(' ')}` : ''; +const { ESLint } = require('eslint'); + +const removeIgnoredFiles = async files => { + const eslint = new ESLint(); + const isIgnored = await Promise.all( + files.map(file => { + return eslint.isPathIgnored(file); + }) + ); + const filteredFiles = files.filter((_, i) => !isIgnored[i]); + return filteredFiles.join(' '); +}; + +const buildFormatCommand = async files => { + const filesToLint = await removeIgnoredFiles(files); + + if (!filesToLint || !filesToLint.length) { + return ''; + } + + const results = filesToLint.map(f => path.relative(process.cwd(), f)); + + return results.length + ? `prettier --ignore-unknown --list-different --write ${results.join(' ')}` + : ''; }; -const buildLintCommand = filenames => { - const results = filenames - .map(f => path.relative(process.cwd(), f)) - .filter(f => !ignoredFiles.includes(f)); +const buildLintCommand = async files => { + const filesToLint = await removeIgnoredFiles(files); + + if (!filesToLint || !filesToLint.length) { + return ''; + } + + const results = filesToLint.map(f => path.relative(process.cwd(), f)); return results.length ? `eslint --cache ${results.join(' ')}` : ''; }; From 153a6499ef2c8137cb32d1521185dc0707b7b204 Mon Sep 17 00:00:00 2001 From: William Grant Date: Mon, 4 Sep 2023 16:37:46 +1000 Subject: [PATCH 2/2] chore: disable pre commit lint-staged hook for now buildLintCommand is broken if you have a lint error in a file the commit fails silently --- .husky/pre-commit | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index d24fdfc60..9d4f45ace 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,5 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npx lint-staged +# Disabling this hook for now because the BuildLintCommand has issues. If you have an error in a file that eslint catches the commit fails silently instead of explaining the error (Will 04/09/2023) +# npx lint-staged