Skip to content

Commit

Permalink
fix(scripts): make eslint run again on pre-commit (#25537)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell authored Nov 10, 2022
1 parent 63c8aa4 commit 518d14d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions scripts/lint-staged/eslint-for-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)));
Expand Down
8 changes: 7 additions & 1 deletion scripts/lint-staged/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down

0 comments on commit 518d14d

Please sign in to comment.