Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter/max-lines): only report codes that exceed the line limit #2778

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/oxc_linter/src/rules/eslint/max_lines.rs
Expand Up @@ -126,9 +126,19 @@ impl Rule for MaxLines {
"File has too many lines ({}). Maximum allowed is {}.",
lines_in_file, self.max,
));

let start = ctx
.source_text()
.lines()
.take(self.max)
.map(|line| line.chars().count() + 1) // padding 1 each line for '\n'
.sum::<usize>();
ctx.diagnostic(MaxLinesDiagnostic(
error,
Span::new(0, u32::try_from(ctx.source_text().len()).unwrap_or(u32::MAX)),
Span::new(
u32::try_from(start).unwrap_or(u32::MIN),
u32::try_from(ctx.source_text().len()).unwrap_or(u32::MAX),
),
));
}
}
Expand Down
171 changes: 77 additions & 94 deletions crates/oxc_linter/src/snapshots/max_lines.snap
Expand Up @@ -3,45 +3,42 @@ source: crates/oxc_linter/src/tester.rs
expression: max_lines
---
⚠ eslint(max-lines): "File has too many lines (3). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var xyz;
2 │ │ var xyz;
3 │ ╰─▶ var xyz;
╭─[max_lines.tsx:3:1]
2 │ var xyz;
3 │ var xyz;
· ────────
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (4). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ /* a multiline comment
2 │ │ that goes to many lines*/
3 │ │ var xy;
╭─[max_lines.tsx:3:1]
2 │ that goes to many lines*/
3 │ ╭─▶ var xy;
4 │ ╰─▶ var xy;
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (3). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1╭─▶ //a single line comment
2 │ │ var xy;
3 │ ╰─▶ var xy;
╭─[max_lines.tsx:3:1]
2var xy;
3 │ var xy;
· ───────
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (5). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var x;
2 │ │
3 │ │
╭─[max_lines.tsx:3:1]
2 │
3 │ ╭─▶
4 │ │
5 │ ╰─▶ var y;
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (8). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ //a single line comment
2 │ │ var xy;
3 │ │
╭─[max_lines.tsx:3:1]
2 │ var xy;
3 │ ╭─▶
4 │ │ var xy;
5 │ │
6 │ │ /* a multiline
Expand All @@ -51,27 +48,25 @@ expression: max_lines
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (3). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1╭─▶ var x; // inline comment
2 var y;
3 │ ╰─▶ var z;
╭─[max_lines.tsx:3:1]
2 var y;
3 │ var z;
· ──────────────────
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (4). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var x; /* inline comment
2 │ │ spanning multiple lines */
3 │ │ var y;
╭─[max_lines.tsx:3:1]
2 │ spanning multiple lines */
3 │ ╭─▶ var y;
4 │ ╰─▶ var z;
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (8). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ //a single line comment
2 │ │ var xy;
3 │ │
╭─[max_lines.tsx:3:1]
2 │ var xy;
3 │ ╭─▶
4 │ │ var xy;
5 │ │
6 │ │ /* a multiline
Expand Down Expand Up @@ -121,94 +116,88 @@ expression: max_lines
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (2). Maximum allowed is 1."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ A
2 │ ╰─▶
╭─[max_lines.tsx:2:1]
1 │ A
2 │
· ─────────────
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (3). Maximum allowed is 1."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ A
2 │
╭─[max_lines.tsx:2:1]
1 │ A
2 │ ╭─▶
3 │ ╰─▶
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (4). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ var x
3 │ │ var c;
╭─[max_lines.tsx:3:1]
2 │ var x
3 │ ╭─▶ var c;
4 │ ╰─▶ console.log
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (3). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1╭─▶ var a = 'a',
2 c,
3 │ ╰─▶ x;
╭─[max_lines.tsx:3:1]
2 c,
3x;
· ───────────────
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (4). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a',
2 │ │ c,
3 │ │ x;
╭─[max_lines.tsx:3:1]
2 │ c,
3 │ ╭─▶ x;
4 │ ╰─▶
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (6). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶
2 │ │
3 │ │ var a = 'a',
╭─[max_lines.tsx:3:1]
2 │
3 │ ╭─▶ var a = 'a',
4 │ │ c,
5 │ │ x;
6 │ ╰─▶
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (6). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ var x
3 │ │ var c;
╭─[max_lines.tsx:3:1]
2 │ var x
3 │ ╭─▶ var c;
4 │ │ console.log
5 │ │ // some block
6 │ ╰─▶ // comments
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (5). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ var x
3 │ │ var c;
╭─[max_lines.tsx:3:1]
2 │ var x
3 │ ╭─▶ var c;
4 │ │ console.log
5 │ ╰─▶ /* block comments */
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (6). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ var x
3 │ │ var c;
╭─[max_lines.tsx:3:1]
2 │ var x
3 │ ╭─▶ var c;
4 │ │ console.log
5 │ │ /* block comments */
6 │ ╰─▶
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (7). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ var x
3 │ │ var c;
╭─[max_lines.tsx:3:1]
2 │ var x
3 │ ╭─▶ var c;
4 │ │ console.log
5 │ │ /** block
6 │ │
Expand All @@ -217,19 +206,17 @@ expression: max_lines
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (4). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │
3 │ │
╭─[max_lines.tsx:3:1]
2 │
3 │ ╭─▶
4 │ ╰─▶ // comment
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (8). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ var x
3 │ │
╭─[max_lines.tsx:3:1]
2 │ var x
3 │ ╭─▶
4 │ │
5 │ │ var c;
6 │ │ console.log
Expand All @@ -239,10 +226,9 @@ expression: max_lines
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (8). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │
3 │ │
╭─[max_lines.tsx:3:1]
2 │
3 │ ╭─▶
4 │ │ var x
5 │ │ var c;
6 │ │ console.log
Expand All @@ -252,21 +238,19 @@ expression: max_lines
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (6). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ var a = 'a';
2 │ │ //
3 │ │ var x
╭─[max_lines.tsx:3:1]
2 │ //
3 │ ╭─▶ var x
4 │ │ var c;
5 │ │ console.log
6 │ ╰─▶ //
╰────
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (9). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶ // hello world
2 │ │ /*hello
3 │ │ world 2 */
╭─[max_lines.tsx:3:1]
2 │ /*hello
3 │ ╭─▶ world 2 */
4 │ │ var a,
5 │ │ b
6 │ │ // hh
Expand All @@ -277,10 +261,9 @@ expression: max_lines
help: Reduce the number of lines in this file

⚠ eslint(max-lines): "File has too many lines (11). Maximum allowed is 2."
╭─[max_lines.tsx:1:1]
1 │ ╭─▶
2 │ │ var x = '';
3 │ │
╭─[max_lines.tsx:3:1]
2 │ var x = '';
3 │ ╭─▶
4 │ │ // comment
5 │ │
6 │ │ var b = '',
Expand Down