Skip to content

Commit adaeec9

Browse files
committed
fix(linter): let file-header disable directives suppress file-start diagnostics
1 parent dd0220f commit adaeec9

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"jsPlugins": ["./plugin.ts"],
3+
"rules": {
4+
"basic-custom-plugin/rule": "error"
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* oxlint-disable basic-custom-plugin/rule */
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Plugin } from "#oxlint/plugins";
2+
3+
const plugin: Plugin = {
4+
meta: {
5+
name: "basic-custom-plugin",
6+
},
7+
rules: {
8+
rule: {
9+
create(_context) {
10+
return {
11+
Program(_program) {
12+
_context.report({
13+
message: "oops",
14+
loc: {
15+
start: { line: 0, column: 0 },
16+
},
17+
});
18+
},
19+
};
20+
},
21+
},
22+
},
23+
};
24+
25+
export default plugin;

crates/oxc_linter/src/disable_directives.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ impl DisableDirectivesBuilder {
377377
// `eslint-disable`
378378
if text.trim().is_empty() {
379379
if self.disable_all_start.is_none() {
380-
self.disable_all_start = Some((comment_span.end, comment_span));
380+
// Start coverage at the beginning of the directive comment so
381+
// top-of-file headers can suppress file-start diagnostics.
382+
self.disable_all_start = Some((comment.span.start, comment_span));
381383
}
382384
self.disable_rule_comments.push(DisableRuleComment {
383385
span: comment_span,
@@ -493,7 +495,7 @@ impl DisableDirectivesBuilder {
493495
let mut rules = vec![];
494496
Self::get_rule_names(text, rule_name_start, |rule_name, name_span| {
495497
self.disable_start_map.entry(rule_name.to_string()).or_insert((
496-
comment_span.end,
498+
comment.span.start,
497499
name_span,
498500
comment_span,
499501
));
@@ -1391,4 +1393,20 @@ function test() {
13911393
"eslint-disable-next-line should NOT suppress diagnostics on lines after the next line"
13921394
);
13931395
}
1396+
1397+
#[test]
1398+
fn test_disable_file_header_suppresses_file_start_diagnostic() {
1399+
test_directives(
1400+
|prefix| {
1401+
format!(
1402+
"/* {prefix}-disable no-console */\nconsole.log('still disabled by header');\n"
1403+
)
1404+
},
1405+
|_, directives| {
1406+
// Some rules report at file start (line 1, column 0), e.g. Program-level diagnostics.
1407+
// A header disable should still suppress those diagnostics.
1408+
assert!(directives.contains("no-console", Span::new(0, 1)));
1409+
},
1410+
);
1411+
}
13941412
}

0 commit comments

Comments
 (0)