Skip to content

Commit 5b33d89

Browse files
authored
feat!: resolve slow lint time from configuration
BREAKING CHANGE: Previously a warning was logged if linting took more than 5s. Now by default no such warnings are logged. Timelimit for such logs can be configured via `config.slowLintTimeLimit`
1 parent 67f759c commit 5b33d89

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

lib/engine/worker-task.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const UNKNOWN_RULE_ID = 'unable-to-parse-rule-id';
3939
// https://github.com/eslint/eslint/blob/ed1da5d96af2587b7211854e45cf8657ef808710/lib/linter/linter.js#L1194
4040
const LINE_REGEX = /Occurred while linting \S*:([0-9]+)?/;
4141

42-
const MAX_LINT_TIME_SECONDS = 5;
4342
const MAX_ROW_LENGTH = 1000;
4443

4544
/**
@@ -238,18 +237,21 @@ export default async function workerTask(): Promise<void> {
238237
let result: ESLint.LintResult[];
239238

240239
try {
241-
result = await executionTimeWarningWrapper(
242-
() => linter.lintFiles(path),
243-
244-
// Warn about files taking more than 5s to lint
245-
// Useful to identify minified files committed to remote
246-
lintTime =>
247-
postMessage({
248-
type: 'FILE_LINT_SLOW',
249-
payload: { path, lintTime },
250-
}),
251-
MAX_LINT_TIME_SECONDS
252-
);
240+
const lintFile = () => linter.lintFiles(path);
241+
242+
if (config.slowLintTimeLimit) {
243+
result = await executionTimeWarningWrapper(
244+
lintFile,
245+
lintTime =>
246+
postMessage({
247+
type: 'FILE_LINT_SLOW',
248+
payload: { path, lintTime },
249+
}),
250+
config.slowLintTimeLimit
251+
);
252+
} else {
253+
result = await lintFile();
254+
}
253255
} catch (error) {
254256
// Catch crashing linter
255257
const crashMessage = parseErrorStack(error, file);

test/unit/__mocks__/eslint.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ type LintResult = actual.ESLint.LintResult;
44

55
export class ESLint {
66
static mockConstructor = jest.fn();
7+
static delay: number | null = null;
78

89
constructor(config: unknown) {
910
ESLint.mockConstructor(config);
1011
}
1112

1213
async lintFiles(filePath: string): Promise<LintResult[]> {
14+
if (ESLint.delay) {
15+
const delay = ESLint.delay * 1000;
16+
17+
await new Promise(resolve => setTimeout(resolve, delay));
18+
}
19+
1320
return [
1421
{
1522
filePath,

test/unit/worker-task.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ESLint } from 'eslint';
22

33
import workerTask, { WorkerMessage } from '@engine/worker-task';
44
import { parentPort } from '__mocks__/worker_threads';
5+
import { mockConfigValue } from '__mocks__/@config';
56

67
jest.mock('worker_threads', () => require('./__mocks__/worker_threads'));
78

@@ -23,6 +24,11 @@ beforeEach(() => {
2324
parentPort.postMessage.mockClear();
2425
});
2526

27+
afterEach(() => {
28+
// @ts-expect-error -- Mock's API
29+
ESLint.delay = null;
30+
});
31+
2632
test('should limit length of results to 1000 characters', async () => {
2733
await workerTask();
2834

@@ -45,3 +51,38 @@ test('should limit length of results to 1000 characters', async () => {
4551
// Last three characters should be '...'
4652
expect(source!.slice(-3)).toBe('...');
4753
});
54+
55+
test('should warn if lint takes longer than config.slowLintTime', async () => {
56+
// @ts-expect-error -- Mock's API
57+
ESLint.delay = 3;
58+
mockConfigValue({ slowLintTimeLimit: 1 });
59+
60+
await workerTask();
61+
62+
const resultMessages = getPostMessageCalls('FILE_LINT_SLOW');
63+
64+
expect(resultMessages).toHaveLength(1);
65+
expect(resultMessages[0].payload.path).toBe('./mock/path/file.ts');
66+
});
67+
68+
test('should not warn if lint takes less than config.slowLintTime', async () => {
69+
// @ts-expect-error -- Mock's API
70+
ESLint.delay = 1;
71+
mockConfigValue({ slowLintTimeLimit: 3 });
72+
73+
await workerTask();
74+
75+
const resultMessages = getPostMessageCalls('FILE_LINT_SLOW');
76+
expect(resultMessages).toHaveLength(0);
77+
});
78+
79+
test('should not warn if config.slowLintTime is not set', async () => {
80+
// @ts-expect-error -- Mock's API
81+
ESLint.delay = 3;
82+
mockConfigValue({ slowLintTimeLimit: undefined });
83+
84+
await workerTask();
85+
86+
const resultMessages = getPostMessageCalls('FILE_LINT_SLOW');
87+
expect(resultMessages).toHaveLength(0);
88+
});

0 commit comments

Comments
 (0)