From 49e15933c5fa775acb6ca915dc12110cf3a21804 Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 25 Feb 2025 15:27:03 +0800 Subject: [PATCH 1/6] feat(eslint): improve the no-console rule to support allowable method configuration - Modified `no_console_diagnostic` function to add a list of allowed methods argument - Include the list of allowed console methods in the prompt message. - Updated diagnostic calls to support passing information about allowed methods. --- .../oxc_linter/src/rules/eslint/no_console.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_console.rs b/crates/oxc_linter/src/rules/eslint/no_console.rs index 2b4bd6703f244..5b5ecb0cf5673 100644 --- a/crates/oxc_linter/src/rules/eslint/no_console.rs +++ b/crates/oxc_linter/src/rules/eslint/no_console.rs @@ -10,10 +10,13 @@ use crate::{ rule::Rule, }; -fn no_console_diagnostic(span: Span) -> OxcDiagnostic { - OxcDiagnostic::warn("eslint(no-console): Unexpected console statement.") - .with_label(span) - .with_help("Delete this console statement.") +fn no_console_diagnostic(span: Span, allow: &Vec) -> OxcDiagnostic { + OxcDiagnostic::warn(format!( + "eslint(no-console): Unexpected console statement. Only supported methods: {}", + allow.iter().map(|s| s.to_string()).collect::>().join(", ") + )) + .with_label(span) + .with_help("Delete this console statement.") } #[derive(Debug, Default, Clone)] @@ -90,9 +93,11 @@ impl Rule for NoConsole { { if let Some((mem_span, _)) = mem.static_property_info() { let diagnostic_span = ident.span().merge(mem_span); - ctx.diagnostic_with_suggestion(no_console_diagnostic(diagnostic_span), |fixer| { - remove_console(fixer, ctx, node) - }); + + ctx.diagnostic_with_suggestion( + no_console_diagnostic(diagnostic_span, &self.allow), + |fixer| remove_console(fixer, ctx, node), + ); } } } From 745e2a44b9b5b63a2e2973fe4230b6beee85ed5b Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 25 Feb 2025 19:28:49 +0800 Subject: [PATCH 2/6] refactor(linter): improve warning message construction in no_console rules Refactored warning message construction logic in no_console rules to optimize message formatting when the allow list is empty. --- .../oxc_linter/src/rules/eslint/no_console.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_console.rs b/crates/oxc_linter/src/rules/eslint/no_console.rs index 5b5ecb0cf5673..8c1b19a363ae0 100644 --- a/crates/oxc_linter/src/rules/eslint/no_console.rs +++ b/crates/oxc_linter/src/rules/eslint/no_console.rs @@ -11,12 +11,18 @@ use crate::{ }; fn no_console_diagnostic(span: Span, allow: &Vec) -> OxcDiagnostic { - OxcDiagnostic::warn(format!( - "eslint(no-console): Unexpected console statement. Only supported methods: {}", - allow.iter().map(|s| s.to_string()).collect::>().join(", ") - )) - .with_label(span) - .with_help("Delete this console statement.") + let only_msg = if allow.is_empty() { + "".to_string() + } else { + format!( + "Only supported methods: {}", + allow.iter().map(|s| s.to_string()).collect::>().join(", ") + ) + }; + + OxcDiagnostic::warn(format!("eslint(no-console): Unexpected console statement. {}", only_msg)) + .with_label(span) + .with_help("Delete this console statement.") } #[derive(Debug, Default, Clone)] From 28098bee5796cd12766ecccd8246053fbdb720f2 Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 25 Feb 2025 20:16:15 +0800 Subject: [PATCH 3/6] refactor(eslint): optimizes string handling in no_console rules - Replacing `Vec` references with slices to improve performance - Replace `to_string` with `String::new` to optimize empty string creation. - Apply formatted strings to improve readability --- crates/oxc_linter/src/rules/eslint/no_console.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_console.rs b/crates/oxc_linter/src/rules/eslint/no_console.rs index 8c1b19a363ae0..8892694df3f1d 100644 --- a/crates/oxc_linter/src/rules/eslint/no_console.rs +++ b/crates/oxc_linter/src/rules/eslint/no_console.rs @@ -10,17 +10,17 @@ use crate::{ rule::Rule, }; -fn no_console_diagnostic(span: Span, allow: &Vec) -> OxcDiagnostic { +fn no_console_diagnostic(span: Span, allow: &[CompactStr]) -> OxcDiagnostic { let only_msg = if allow.is_empty() { - "".to_string() + String::new() } else { format!( "Only supported methods: {}", - allow.iter().map(|s| s.to_string()).collect::>().join(", ") + allow.iter().map(ToString::to_string).collect::>().join(", ") ) }; - OxcDiagnostic::warn(format!("eslint(no-console): Unexpected console statement. {}", only_msg)) + OxcDiagnostic::warn(format!("eslint(no-console): Unexpected console statement. {only_msg}")) .with_label(span) .with_help("Delete this console statement.") } From 4bbc5f19495eb1b7c208e5a0db5346428aa3ae9f Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 25 Feb 2025 20:19:23 +0800 Subject: [PATCH 4/6] fix(linter): update eslint_no_console.snap to specify supported console methods --- .../src/snapshots/eslint_no_console.snap | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/oxc_linter/src/snapshots/eslint_no_console.snap b/crates/oxc_linter/src/snapshots/eslint_no_console.snap index efda2e83cf95b..308e8beb1da1c 100644 --- a/crates/oxc_linter/src/snapshots/eslint_no_console.snap +++ b/crates/oxc_linter/src/snapshots/eslint_no_console.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_linter/src/tester.rs +assertion_line: 357 --- ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. ╭─[no_console.tsx:1:1] @@ -50,56 +51,56 @@ source: crates/oxc_linter/src/tester.rs ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: error ╭─[no_console.tsx:1:1] 1 │ console.log(foo) · ─────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn ╭─[no_console.tsx:1:1] 1 │ console.error(foo) · ───────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: log ╭─[no_console.tsx:1:1] 1 │ console.info(foo) · ──────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: error ╭─[no_console.tsx:1:1] 1 │ console.warn(foo) · ──────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn, info ╭─[no_console.tsx:1:1] 1 │ console.log(foo) · ─────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn, info, log ╭─[no_console.tsx:1:1] 1 │ console.error(foo) · ───────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn, error, log ╭─[no_console.tsx:1:1] 1 │ console.info(foo) · ──────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: info, log ╭─[no_console.tsx:1:1] 1 │ console.warn(foo) · ──────────── From d1d8006d5d8a43e4b63706197d3b90deff21972e Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 25 Feb 2025 22:46:16 +0800 Subject: [PATCH 5/6] fix(no_console): Update the formatting string to remove unnecessary string conversions. --- crates/oxc_linter/src/rules/eslint/no_console.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_console.rs b/crates/oxc_linter/src/rules/eslint/no_console.rs index 8892694df3f1d..7d14233cd99b9 100644 --- a/crates/oxc_linter/src/rules/eslint/no_console.rs +++ b/crates/oxc_linter/src/rules/eslint/no_console.rs @@ -16,7 +16,7 @@ fn no_console_diagnostic(span: Span, allow: &[CompactStr]) -> OxcDiagnostic { } else { format!( "Only supported methods: {}", - allow.iter().map(ToString::to_string).collect::>().join(", ") + allow.iter().map(oxc_span::CompactStr::as_str).join(",") ) }; From 20c4d62c38ee99489c07767e9c25f13f73f54449 Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 25 Feb 2025 23:10:46 +0800 Subject: [PATCH 6/6] fix(linter): Fix the string concatenation issue in the no_console rule - Modify the string concatenation logic in the `no_console.rs` file to uniformly use the `join(",")` method. - Update the corresponding snapshot tests to reflect these changes, ensuring no extra spaces are included. --- crates/oxc_linter/src/rules/eslint/no_console.rs | 5 +---- crates/oxc_linter/src/snapshots/eslint_no_console.snap | 8 ++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_console.rs b/crates/oxc_linter/src/rules/eslint/no_console.rs index 7d14233cd99b9..53aba59c4afb4 100644 --- a/crates/oxc_linter/src/rules/eslint/no_console.rs +++ b/crates/oxc_linter/src/rules/eslint/no_console.rs @@ -14,10 +14,7 @@ fn no_console_diagnostic(span: Span, allow: &[CompactStr]) -> OxcDiagnostic { let only_msg = if allow.is_empty() { String::new() } else { - format!( - "Only supported methods: {}", - allow.iter().map(oxc_span::CompactStr::as_str).join(",") - ) + format!("Only supported methods: {}", allow.join(",")) }; OxcDiagnostic::warn(format!("eslint(no-console): Unexpected console statement. {only_msg}")) diff --git a/crates/oxc_linter/src/snapshots/eslint_no_console.snap b/crates/oxc_linter/src/snapshots/eslint_no_console.snap index 308e8beb1da1c..f8d04c43747cf 100644 --- a/crates/oxc_linter/src/snapshots/eslint_no_console.snap +++ b/crates/oxc_linter/src/snapshots/eslint_no_console.snap @@ -79,28 +79,28 @@ assertion_line: 357 ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn, info + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn,info ╭─[no_console.tsx:1:1] 1 │ console.log(foo) · ─────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn, info, log + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn,info,log ╭─[no_console.tsx:1:1] 1 │ console.error(foo) · ───────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn, error, log + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: warn,error,log ╭─[no_console.tsx:1:1] 1 │ console.info(foo) · ──────────── ╰──── help: Delete this console statement. - ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: info, log + ⚠ eslint(no-console): eslint(no-console): Unexpected console statement. Only supported methods: info,log ╭─[no_console.tsx:1:1] 1 │ console.warn(foo) · ────────────