Skip to content

Commit

Permalink
feat(linter): add support eslint 9 --quiet param
Browse files Browse the repository at this point in the history
closes #28291
  • Loading branch information
pumano committed Nov 1, 2024
1 parent 9adb9e0 commit ac4de76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
16 changes: 15 additions & 1 deletion packages/eslint/src/executors/lint/utility/eslint-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jest.mock('eslint/use-at-your-own-risk', () => ({
}));

const { LegacyESLint } = require('eslint/use-at-your-own-risk');
import { resolveAndInstantiateESLint } from './eslint-utils';
import { getESLintVersion, resolveAndInstantiateESLint } from './eslint-utils';

describe('eslint-utils', () => {
beforeEach(() => {
Expand Down Expand Up @@ -208,4 +208,18 @@ describe('eslint-utils', () => {
);
});
});

describe('eslint version getter', () => {
it('should return valid major and minor versions correctly', () => {
expect(getESLintVersion('7.20.1')).toStrictEqual({ major: 7, minor: 20 });
expect(getESLintVersion('8.57.0')).toStrictEqual({ major: 8, minor: 57 });
expect(getESLintVersion('8.57.1')).toStrictEqual({ major: 8, minor: 57 });
expect(getESLintVersion('9.12.0')).toStrictEqual({ major: 9, minor: 12 });
});

it('should handle invalid case for resolve major and minor versions', () => {
expect(getESLintVersion('5')).toStrictEqual({ major: 5, minor: 0 });
expect(getESLintVersion(undefined)).toStrictEqual({ major: 0, minor: 0 });
});
});
});
19 changes: 18 additions & 1 deletion packages/eslint/src/executors/lint/utility/eslint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export async function resolveAndInstantiateESLint(
useFlatConfigOverrideVal: useFlatConfig,
});

const eslintOptions: ESLint.Options = {
// ruleFilter exist only in eslint 9+, remove this type when eslint 8 support dropped
const eslintOptions: ESLint.Options & { ruleFilter?: Function } = {
overrideConfigFile: eslintConfigPath,
fix: !!options.fix,
cache: !!options.cache,
Expand Down Expand Up @@ -72,10 +73,26 @@ export async function resolveAndInstantiateESLint(
options.reportUnusedDisableDirectives || undefined;
}

const eslintVersion = getESLintVersion(ESLint.version);

// pass --quiet to eslint 9+ directly: filter only errors
if (options.quiet && eslintVersion.major >= 9) {
eslintOptions.ruleFilter = (rule) => rule.severity === 2;
}

const eslint = new ESLint(eslintOptions);

return {
ESLint,
eslint,
};
}

export function getESLintVersion(eslintVersion: string) {
const version = eslintVersion?.split('.');

return {
major: Number(version?.[0] ?? 0),
minor: Number(version?.[1] ?? 0),
};
}

0 comments on commit ac4de76

Please sign in to comment.