Skip to content

Commit

Permalink
fix(linter): ignore invalid or partial disable directives (#6045)
Browse files Browse the repository at this point in the history
- closes #6041

We were not ignoring cases where the directive was invalid, now we do.
  • Loading branch information
camchenry committed Sep 24, 2024
1 parent f8464a3 commit 6f76ebe
Showing 1 changed file with 89 additions and 14 deletions.
103 changes: 89 additions & 14 deletions crates/oxc_linter/src/disable_directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ impl<'a> DisableDirectivesBuilder<'a> {
self.disable_all_comments.push(comment.span);
continue;
}

// `eslint-disable-next-line`
if let Some(text) = text.strip_prefix("-next-line") {
else if let Some(text) = text.strip_prefix("-next-line") {
// Get the span up to the next new line
let stop = self.source_text[comment.span.end as usize..]
.lines()
Expand All @@ -136,9 +135,8 @@ impl<'a> DisableDirectivesBuilder<'a> {
}
continue;
}

// `eslint-disable-line`
if let Some(text) = text.strip_prefix("-line") {
else if let Some(text) = text.strip_prefix("-line") {
// Get the span between the preceding newline to this comment
let start = self.source_text[..=comment.span.start as usize]
.lines()
Expand All @@ -162,16 +160,19 @@ impl<'a> DisableDirectivesBuilder<'a> {
}
continue;
}

// `eslint-disable rule-name1, rule-name2`
let mut rules = vec![];
Self::get_rule_names(text, |rule_name| {
self.disable_start_map.entry(rule_name).or_insert(comment.span.end);
rules.push(rule_name);
});
self.disable_rule_comments.push(DisableRuleComment { span: comment.span, rules });

continue;
// Remaining text should start with a space, else it's probably a typo of the correct syntax.
// Like `eslint-disable-lext-nine` where `text` is `-lext-nine`, or directive is `eslint-disablefoo`
else if text.starts_with(' ') {
// `eslint-disable rule-name1, rule-name2`
let mut rules = vec![];
Self::get_rule_names(text, |rule_name| {
self.disable_start_map.entry(rule_name).or_insert(comment.span.end);
rules.push(rule_name);
});
self.disable_rule_comments
.push(DisableRuleComment { span: comment.span, rules });
continue;
}
}

if let Some(text) =
Expand Down Expand Up @@ -341,6 +342,21 @@ fn test() {
debugger;
"
),
// Should only match `eslint-enable` comments, not `eslint-enablefoo`
format!("
/* {prefix}-disable */
debugger;
/* {prefix}-enablefoo */
debugger;
"
),
format!("
/* {prefix}-disable no-debugger, no-console */
debugger;
/* {prefix}-enablefoo no-debugger, no-console */
debugger;
"
),
];

let fail = vec![
Expand Down Expand Up @@ -395,6 +411,65 @@ fn test() {
debugger;
"
),
// Should not match invalid directives
// https://github.com/oxc-project/oxc/issues/6041
format!(
"// {prefix}-disable-lext-nine no-debugger
debugger;
"
),
format!(
"// {prefix}-disabled no-debugger
debugger;
"
),
format!(
"// {prefix}-disabled
debugger;
"
),
format!(
"
debugger; // {prefix}-disable-lext-nine no-debugger
// {prefix}-disable-lext-nine no-debugger
debugger;
debugger; /* {prefix}-disable-lin no-debugger */
/* {prefix}-disable-next-lin no-debugger */
debugger;
"
),
format!(
"debugger; // {prefix}-disable-linefoo
debugger; // {prefix}-disable-linefoo
// {prefix}-disable-next-linefoo
debugger;
/* {prefix}-disable-next-linefoo */
debugger;
debugger; /* {prefix}-disable-linefoo */
"
),
format!(
"
/* {prefix}-disable */
debugger;
/* {prefix}-enable */
debugger;
"
),
format!(
"
/* {prefix}-disable no-debugger, no-console */
debugger;
/* {prefix}-enable no-debugger, no-console */
debugger;
"
),
];

Tester::new("no-debugger", pass, fail).test();
Expand Down

0 comments on commit 6f76ebe

Please sign in to comment.