From 518d14da9d2d826be43b6dc16ad9914ece82e07c Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Thu, 10 Nov 2022 19:48:10 +0100 Subject: [PATCH] fix(scripts): make eslint run again on pre-commit (#25537) --- scripts/lint-staged/eslint-for-package.js | 6 ++++-- scripts/lint-staged/eslint.js | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/scripts/lint-staged/eslint-for-package.js b/scripts/lint-staged/eslint-for-package.js index dc3098a14c248..8a55b6ecb4b2f 100644 --- a/scripts/lint-staged/eslint-for-package.js +++ b/scripts/lint-staged/eslint-for-package.js @@ -32,7 +32,7 @@ async function run() { // (Note that until we start linting all files in the package and can remove the constants.directory // segment here, the glob needs to start with the absolute package path in case someone has named // the directory containing all their git repos "src".) - includePattern = path.join(packagePath, constants.directory, '**', `*{${constants.extensions}`); + includePattern = path.join(packagePath, constants.directory, '**', `*{${constants.extensions}}`); eslint = new ESLint({ fix: true, cache: true }); } else { // Otherwise, look for the --ext option to determine extensions @@ -42,8 +42,10 @@ async function run() { eslint = new ESLint({ fix: true, cache: lintScript.includes('--cache') }); } + // files are provided by @see `./eslint.js` via cli + const cliFiles = process.argv.slice(2); // Filter out files with non-linted extensions - const files = process.argv.slice(2).filter(file => micromatch.isMatch(file, includePattern)); + const files = cliFiles.filter(file => micromatch.isMatch(file, includePattern)); // Filter out ignored files (2-step process due to isPathIgnored returning a promise) const ignoreResults = await Promise.all(files.map(f => eslint.isPathIgnored(f))); diff --git a/scripts/lint-staged/eslint.js b/scripts/lint-staged/eslint.js index 2f0abc633ccf8..034c64b072419 100644 --- a/scripts/lint-staged/eslint.js +++ b/scripts/lint-staged/eslint.js @@ -33,7 +33,13 @@ function groupFilesByPackage() { ); for (const file of files) { - const packagePath = packagesWithEslint.find(packagePath => file.startsWith(packagePath)); + const packagePath = packagesWithEslint.find(packagePath => { + // if file lives within searched package we will get only shortened absolute path `/src/abc.ts` + // we add `.` to make it relative and thus have match pattern to check upon + const normalizedFilePath = file.replace(packagePath, '.'); + return normalizedFilePath.startsWith('./'); + }); + // Exclude files in a package without an eslintrc (or not in a package at all) if (packagePath) { if (!filesByPackage[packagePath]) {