From ec2128e6140d502f1321f76c0fdd29c609c10c9d Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Tue, 17 Dec 2024 15:53:56 +0300 Subject: [PATCH] fix(linter): fix line calculation for `eslint/max-lines` in diagnostics (#7962) closes #7961 --- .../oxc_linter/src/rules/eslint/max_lines.rs | 6 +++- .../src/snapshots/eslint_max_lines.snap | 29 +++++++++---------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/max_lines.rs b/crates/oxc_linter/src/rules/eslint/max_lines.rs index feac4985ac044..ab8885d84f506 100644 --- a/crates/oxc_linter/src/rules/eslint/max_lines.rs +++ b/crates/oxc_linter/src/rules/eslint/max_lines.rs @@ -129,7 +129,11 @@ impl Rule for MaxLines { if lines_in_file.saturating_sub(blank_lines).saturating_sub(comment_lines) > self.max { // Point to end of the file for `eslint-disable max-lines` to work. let end = ctx.source_text().len().saturating_sub(1) as u32; - ctx.diagnostic(max_lines_diagnostic(lines_in_file, self.max, Span::new(end, end))); + ctx.diagnostic(max_lines_diagnostic( + lines_in_file.saturating_sub(blank_lines).saturating_sub(comment_lines), + self.max, + Span::new(end, end), + )); } } } diff --git a/crates/oxc_linter/src/snapshots/eslint_max_lines.snap b/crates/oxc_linter/src/snapshots/eslint_max_lines.snap index 0464fdeb4c4db..9ba276781e473 100644 --- a/crates/oxc_linter/src/snapshots/eslint_max_lines.snap +++ b/crates/oxc_linter/src/snapshots/eslint_max_lines.snap @@ -1,6 +1,5 @@ --- source: crates/oxc_linter/src/tester.rs -snapshot_kind: text --- ⚠ eslint(max-lines): File has too many lines (3). ╭─[max_lines.tsx:3:8] @@ -34,7 +33,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (8). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:8:18] 7 │ really really 8 │ long comment*/ @@ -50,7 +49,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (4). + ⚠ eslint(max-lines): File has too many lines (3). ╭─[max_lines.tsx:4:9] 3 │ var y; 4 │ var z; @@ -58,7 +57,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (8). + ⚠ eslint(max-lines): File has too many lines (6). ╭─[max_lines.tsx:8:18] 7 │ really really 8 │ long comment*/ @@ -149,7 +148,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (6). + ⚠ eslint(max-lines): File has too many lines (3). ╭─[max_lines.tsx:6:3] 5 │ x; 6 │ @@ -157,7 +156,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (6). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:6:14] 5 │ // some block 6 │ // comments @@ -165,7 +164,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (5). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:5:23] 4 │ console.log 5 │ /* block comments */ @@ -173,7 +172,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (6). + ⚠ eslint(max-lines): File has too many lines (5). ╭─[max_lines.tsx:6:3] 5 │ /* block comments */ 6 │ @@ -181,7 +180,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (7). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:7:15] 6 │ 7 │ comments */ @@ -189,7 +188,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (4). + ⚠ eslint(max-lines): File has too many lines (3). ╭─[max_lines.tsx:4:13] 3 │ 4 │ // comment @@ -197,7 +196,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (8). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:8:3] 7 │ 8 │ @@ -205,7 +204,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (8). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:8:3] 7 │ 8 │ @@ -213,7 +212,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (6). + ⚠ eslint(max-lines): File has too many lines (4). ╭─[max_lines.tsx:6:5] 5 │ console.log 6 │ // @@ -221,7 +220,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (9). + ⚠ eslint(max-lines): File has too many lines (5). ╭─[max_lines.tsx:9:5] 8 │ e, 9 │ f; @@ -229,7 +228,7 @@ snapshot_kind: text ╰──── help: Maximum allowed is 2. - ⚠ eslint(max-lines): File has too many lines (11). + ⚠ eslint(max-lines): File has too many lines (5). ╭─[max_lines.tsx:11:13] 10 │ 11 │ // comment