Skip to content

Commit 432d6d9

Browse files
committedJul 2, 2024·
fix(linter): find disabled directives using the message's Span. (#4010)
fixes #4005
1 parent 1854a52 commit 432d6d9

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed
 

‎crates/oxc_linter/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
33
use oxc_cfg::ControlFlowGraph;
44
use oxc_diagnostics::{OxcDiagnostic, Severity};
55
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
6-
use oxc_span::{SourceType, Span};
6+
use oxc_span::{GetSpan, SourceType, Span};
77
use oxc_syntax::module_record::ModuleRecord;
88

99
use crate::{
@@ -157,7 +157,7 @@ impl<'a> LintContext<'a> {
157157
}
158158

159159
fn add_diagnostic(&self, message: Message<'a>) {
160-
if !self.disable_directives.contains(self.current_rule_name, message.start()) {
160+
if !self.disable_directives.contains(self.current_rule_name, message.span()) {
161161
let mut message = message;
162162
if message.error.severity != self.severity {
163163
message.error = message.error.with_severity(self.severity);

‎crates/oxc_linter/src/disable_directives.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub struct DisableDirectives<'a> {
2727
}
2828

2929
impl<'a> DisableDirectives<'a> {
30-
pub fn contains(&self, rule_name: &'static str, start: u32) -> bool {
31-
self.intervals.find(start, start + 1).any(|interval| {
30+
pub fn contains(&self, rule_name: &'static str, span: Span) -> bool {
31+
self.intervals.find(span.start, span.end).any(|interval| {
3232
interval.val == DisabledRule::All
3333
// Our rule name currently does not contain the prefix.
3434
// For example, this will match `@typescript-eslint/no-var-requires` given

‎crates/oxc_linter/src/fixer.rs

+9
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,24 @@ impl<'a> Message<'a> {
189189
Self { error, start, end, fix, fixed: false }
190190
}
191191

192+
#[inline]
192193
pub fn start(&self) -> u32 {
193194
self.start
194195
}
195196

197+
#[inline]
196198
pub fn end(&self) -> u32 {
197199
self.end
198200
}
199201
}
200202

203+
impl<'a> GetSpan for Message<'a> {
204+
#[inline]
205+
fn span(&self) -> Span {
206+
Span::new(self.start(), self.end())
207+
}
208+
}
209+
201210
/// The fixer of the code.
202211
/// Note that our parser has handled the BOM, so we don't need to port the BOM test cases from `ESLint`.
203212
pub struct Fixer<'a> {

‎crates/oxc_linter/src/rules/unicorn/no_empty_file.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ impl Rule for NoEmptyFile {
5858
return;
5959
}
6060

61-
ctx.diagnostic(no_empty_file_diagnostic(Span::new(0, 0)));
61+
let mut span = program.span;
62+
// only show diagnostic for the first 100 characters to avoid huge diagnostic messages with
63+
// empty programs containing a bunch of comments.
64+
// NOTE: if the enable/disable directives come after the first 100 characters they won't be
65+
// respected by this diagnostic.
66+
span.end = std::cmp::min(span.end, 100);
67+
ctx.diagnostic(no_empty_file_diagnostic(span));
6268
}
6369
}
6470

@@ -104,6 +110,7 @@ fn test() {
104110
r"[]",
105111
r"(() => {})()",
106112
"(() => {})();",
113+
"/* eslint-disable no-empty-file */",
107114
];
108115

109116
let fail = vec![

‎crates/oxc_linter/src/snapshots/no_empty_file.snap

+22-23
Original file line numberDiff line numberDiff line change
@@ -9,141 +9,140 @@ source: crates/oxc_linter/src/tester.rs
99
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
1010
╭─[no_empty_file.tsx:1:1]
1111
1
12-
·
12+
·
1313
╰────
1414
help: Delete this file or add some code to it.
1515

1616
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
1717
╭─[no_empty_file.tsx:1:1]
1818
1
19-
·
19+
· ────
2020
╰────
2121
help: Delete this file or add some code to it.
2222

2323
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
2424
╭─[no_empty_file.tsx:1:1]
2525
1
26-
·
26+
·
2727
╰────
2828
help: Delete this file or add some code to it.
2929

3030
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
3131
╭─[no_empty_file.tsx:1:1]
3232
0
33-
·
33+
·
3434
╰────
3535
help: Delete this file or add some code to it.
3636

3737
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
3838
╭─[no_empty_file.tsx:1:1]
3939
1
40-
·
40+
·
4141
╰────
4242
help: Delete this file or add some code to it.
4343

4444
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
4545
╭─[no_empty_file.tsx:1:1]
46-
1
47-
· ▲
48-
2
49-
3
46+
1 │ ╭─▶
47+
2 │ │
48+
3 │ ╰─▶
5049
╰────
5150
help: Delete this file or add some code to it.
5251

5352
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
5453
╭─[no_empty_file.tsx:1:1]
5554
1// comment
56-
·
55+
· ──────────
5756
╰────
5857
help: Delete this file or add some code to it.
5958

6059
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
6160
╭─[no_empty_file.tsx:1:1]
6261
1/* comment */
63-
·
62+
· ─────────────
6463
╰────
6564
help: Delete this file or add some code to it.
6665

6766
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
6867
╭─[no_empty_file.tsx:1:1]
6968
1 │ #!/usr/bin/env node
70-
·
69+
· ───────────────────
7170
╰────
7271
help: Delete this file or add some code to it.
7372

7473
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
7574
╭─[no_empty_file.tsx:1:1]
7675
1'use asm';
77-
·
76+
· ──────────
7877
╰────
7978
help: Delete this file or add some code to it.
8079

8180
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
8281
╭─[no_empty_file.tsx:1:1]
8382
1'use strict';
84-
·
83+
· ─────────────
8584
╰────
8685
help: Delete this file or add some code to it.
8786

8887
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
8988
╭─[no_empty_file.tsx:1:1]
9089
1"use strict"
91-
·
90+
· ────────────
9291
╰────
9392
help: Delete this file or add some code to it.
9493

9594
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
9695
╭─[no_empty_file.tsx:1:1]
9796
1""
98-
·
97+
· ──
9998
╰────
10099
help: Delete this file or add some code to it.
101100

102101
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
103102
╭─[no_empty_file.tsx:1:1]
104103
1 │ ;
105-
·
104+
·
106105
╰────
107106
help: Delete this file or add some code to it.
108107

109108
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
110109
╭─[no_empty_file.tsx:1:1]
111110
1 │ ;;
112-
·
111+
· ──
113112
╰────
114113
help: Delete this file or add some code to it.
115114

116115
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
117116
╭─[no_empty_file.tsx:1:1]
118117
1 │ {}
119-
·
118+
· ──
120119
╰────
121120
help: Delete this file or add some code to it.
122121

123122
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
124123
╭─[no_empty_file.tsx:1:1]
125124
1 │ {;;}
126-
·
125+
· ────
127126
╰────
128127
help: Delete this file or add some code to it.
129128

130129
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
131130
╭─[no_empty_file.tsx:1:1]
132131
1 │ {{}}
133-
·
132+
· ────
134133
╰────
135134
help: Delete this file or add some code to it.
136135

137136
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
138137
╭─[no_empty_file.tsx:1:1]
139138
1"";
140-
·
139+
· ───
141140
╰────
142141
help: Delete this file or add some code to it.
143142

144143
eslint-plugin-unicorn(no-empty-file): Empty files are not allowed.
145144
╭─[no_empty_file.tsx:1:1]
146145
1"use strict";
147-
·
146+
· ─────────────
148147
╰────
149148
help: Delete this file or add some code to it.

0 commit comments

Comments
 (0)
Please sign in to comment.