From 49b6be607ee98dc4edf4fde75d86cc982b0b8bd3 Mon Sep 17 00:00:00 2001 From: Hao Cheng Date: Thu, 9 Nov 2023 02:34:50 +0100 Subject: [PATCH] fix(linter): fix handling of repeated eslint-disable comments (#1200) This PR fixes how the linter handles repeated `eslint-disable` comments. As shown by examples below, in ESLint, repeated `eslint-disable` comments are allowed and all errors below the first `eslint-disable` comment are ignored. The first new test case in ESLint Playground: https://eslint.org/play/#eyJ0ZXh0IjoiICAgICAgICAvKiBlc2xpbnQtZGlzYWJsZSAqL1xuICAgICAgICAgICAgZGVidWdnZXI7XG4gICAgICAgIC8qIGVzbGludC1kaXNhYmxlICovXG4gICAgICAgICAgICBkZWJ1Z2dlcjsiLCJvcHRpb25zIjp7ImVudiI6e30sInJ1bGVzIjp7Im5vLWRlYnVnZ2VyIjpbImVycm9yIl19LCJwYXJzZXJPcHRpb25zIjp7ImVjbWFGZWF0dXJlcyI6e30sImVjbWFWZXJzaW9uIjoibGF0ZXN0Iiwic291cmNlVHlwZSI6Im1vZHVsZSJ9fX0= The second new test case in ESLint Playground: https://eslint.org/play/#eyJ0ZXh0IjoiICAgICAgICAvKiBlc2xpbnQtZGlzYWJsZSBuby1kZWJ1Z2dlciAqL1xuICAgICAgICAgICAgZGVidWdnZXI7XG4gICAgICAgIC8qIGVzbGludC1kaXNhYmxlIG5vLWRlYnVnZ2VyICovXG4gICAgICAgICAgICBkZWJ1Z2dlcjsiLCJvcHRpb25zIjp7ImVudiI6e30sInJ1bGVzIjp7Im5vLWRlYnVnZ2VyIjpbImVycm9yIl19LCJwYXJzZXJPcHRpb25zIjp7ImVjbWFGZWF0dXJlcyI6e30sImVjbWFWZXJzaW9uIjoibGF0ZXN0Iiwic291cmNlVHlwZSI6Im1vZHVsZSJ9fX0= --- crates/oxc_linter/src/disable_directives.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/disable_directives.rs b/crates/oxc_linter/src/disable_directives.rs index 4f0aeaed8..d6fc0602d 100644 --- a/crates/oxc_linter/src/disable_directives.rs +++ b/crates/oxc_linter/src/disable_directives.rs @@ -71,7 +71,9 @@ impl<'a, 'b> DisableDirectivesBuilder<'a, 'b> { if let Some(text) = text.strip_prefix("eslint-disable") { // `eslint-disable` if text.trim().is_empty() { - self.disable_all_start = Some(span.end); + if self.disable_all_start.is_none() { + self.disable_all_start = Some(span.end); + } continue; } @@ -116,7 +118,7 @@ impl<'a, 'b> DisableDirectivesBuilder<'a, 'b> { // `eslint-disable rule-name1, rule-name2` Self::get_rule_names(text, |rule_name| { - self.disable_start_map.insert(rule_name, span.end); + self.disable_start_map.entry(rule_name).or_insert(span.end); }); continue; @@ -237,6 +239,20 @@ fn test() { */ debugger; ", + // To disable all rules twice: + " + /* eslint-disable */ + debugger; + /* eslint-disable */ + debugger; + ", + // To disable a rule twice: + " + /* eslint-disable no-debugger */ + debugger; + /* eslint-disable no-debugger */ + debugger; + ", // Comment descriptions " // eslint-disable-next-line no-debugger -- Here's a description about why this configuration is necessary.