From 16095ab3a463c9e6732e1f13d8259c061ec50714 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 22 Jul 2024 09:38:59 -0400 Subject: [PATCH] fix(cli): apply offsets for htmlish js file sources --- .../src/execute/process_file/lint.rs | 18 +++++-- .../lint_astro_files.snap | 4 +- .../lint_vue_js_files.snap | 37 +++++++------- .../lint_vue_ts_files.snap | 48 ++++++++++--------- .../vue_compiler_macros_as_globals.snap | 10 ++-- crates/biome_diagnostics/src/serde.rs | 9 ++++ 6 files changed, 76 insertions(+), 50 deletions(-) diff --git a/crates/biome_cli/src/execute/process_file/lint.rs b/crates/biome_cli/src/execute/process_file/lint.rs index 91eecd29f672..fb319a1c5733 100644 --- a/crates/biome_cli/src/execute/process_file/lint.rs +++ b/crates/biome_cli/src/execute/process_file/lint.rs @@ -4,6 +4,7 @@ use crate::execute::process_file::{FileResult, FileStatus, Message, SharedTraver use crate::TraversalMode; use biome_analyze::RuleCategoriesBuilder; use biome_diagnostics::{category, Error}; +use biome_rowan::TextSize; use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler}; use std::path::Path; use std::sync::atomic::Ordering; @@ -83,11 +84,11 @@ pub(crate) fn lint_with_guard<'ctx>( && pull_diagnostics_result.skipped_diagnostics == 0; if !no_diagnostics { - let input = match workspace_file.as_extension() { - Some("astro") => AstroFileHandler::input(input.as_str()).to_string(), - Some("vue") => VueFileHandler::input(input.as_str()).to_string(), - Some("svelte") => SvelteFileHandler::input(input.as_str()).to_string(), - _ => input, + let offset = match workspace_file.as_extension() { + Some("vue") => VueFileHandler::start(input.as_str()), + Some("astro") => AstroFileHandler::start(input.as_str()), + Some("svelte") => SvelteFileHandler::start(input.as_str()), + _ => None, }; ctx.push_message(Message::Diagnostics { @@ -96,6 +97,13 @@ pub(crate) fn lint_with_guard<'ctx>( diagnostics: pull_diagnostics_result .diagnostics .into_iter() + .map(|d| { + if let Some(offset) = offset { + d.with_offset(TextSize::from(offset)) + } else { + d + } + }) .map(Error::from) .collect(), skipped_diagnostics: pull_diagnostics_result.skipped_diagnostics as u32, diff --git a/crates/biome_cli/tests/snapshots/main_cases_handle_astro_files/lint_astro_files.snap b/crates/biome_cli/tests/snapshots/main_cases_handle_astro_files/lint_astro_files.snap index 890cdfa7c6f1..3e1749644670 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_handle_astro_files/lint_astro_files.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_handle_astro_files/lint_astro_files.snap @@ -29,9 +29,11 @@ file.astro:2:1 lint/suspicious/noDebugger FIXABLE ━━━━━━━━━ × This is an unexpected use of the debugger statement. + 1 │ --- > 2 │ debugger; │ ^^^^^^^^^ - 3 │ + 3 │ --- + 4 │
i Unsafe fix: Remove debugger statement diff --git a/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_js_files.snap b/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_js_files.snap index 41d6eb587412..3f6424daa4b6 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_js_files.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_js_files.snap @@ -28,21 +28,22 @@ lint ━━━━━━━━━━━━━━━━━━━━━━━━━ # Emitted Messages ```block -file.vue:1:3 lint/suspicious/noDoubleEquals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +file.vue:2:3 lint/suspicious/noDoubleEquals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Use === instead of == - > 1 │ a == b; + 1 │ + 7 │ i A variable declared with var is accessible in the whole module. Thus, the variable can be accessed before its initialization and outside the block where it is declared. diff --git a/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_ts_files.snap b/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_ts_files.snap index dd57a801ca72..63f6890c1f1d 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_ts_files.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/lint_vue_ts_files.snap @@ -28,21 +28,22 @@ lint ━━━━━━━━━━━━━━━━━━━━━━━━━ # Emitted Messages ```block -file.vue:1:3 lint/suspicious/noDoubleEquals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +file.vue:2:3 lint/suspicious/noDoubleEquals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Use === instead of == - > 1 │ a == b; + 1 │ + 7 │ i Safe fix: Remove the type annotation. @@ -98,15 +101,16 @@ file.vue:4:8 lint/style/noInferrableTypes FIXABLE ━━━━━━━━━ ``` ```block -file.vue:4:1 lint/style/noVar FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +file.vue:5:1 lint/style/noVar FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Use let or const instead of var. - 2 │ delete a.c; - 3 │ - > 4 │ var foo: string = ""; + 3 │ delete a.c; + 4 │ + > 5 │ var foo: string = ""; │ ^^^^^^^^^^^^^^^^^^^^ - 5 │ + 6 │ + 7 │ i A variable declared with var is accessible in the whole module. Thus, the variable can be accessed before its initialization and outside the block where it is declared. diff --git a/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/vue_compiler_macros_as_globals.snap b/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/vue_compiler_macros_as_globals.snap index 47ef437a8721..74cb0e6a406e 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/vue_compiler_macros_as_globals.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_handle_vue_files/vue_compiler_macros_as_globals.snap @@ -42,15 +42,15 @@ lint ━━━━━━━━━━━━━━━━━━━━━━━━━ # Emitted Messages ```block -file.vue:16:36 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +file.vue:17:36 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Unexpected any. Specify a different type. - 15 │ const slots = defineSlots<{ - > 16 │ default(props: { msg: string }): any + 16 │ const slots = defineSlots<{ + > 17 │ default(props: { msg: string }): any │ ^^^ - 17 │ }>() - 18 │ + 18 │ }>() + 19 │ i any disables many type checking rules. Its use should be avoided. diff --git a/crates/biome_diagnostics/src/serde.rs b/crates/biome_diagnostics/src/serde.rs index e64a65f74ac5..6fd78b4a0f14 100644 --- a/crates/biome_diagnostics/src/serde.rs +++ b/crates/biome_diagnostics/src/serde.rs @@ -1,6 +1,7 @@ use std::io; use biome_console::{fmt, markup, MarkupBuf}; +use biome_rowan::TextSize; use biome_text_edit::TextEdit; use biome_text_size::TextRange; use serde::{ @@ -73,6 +74,14 @@ impl Diagnostic { source, } } + + pub fn with_offset(mut self, offset: TextSize) -> Self { + self.location.span = self + .location + .span + .map(|span| TextRange::new(span.start() + offset, span.end() + offset)); + self + } } impl super::Diagnostic for Diagnostic {