Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): apply offsets for htmlish js file sources #3494

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions crates/biome_cli/src/execute/process_file/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 │ <div></div>

i Unsafe fix: Remove debugger statement

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 │ <script setup lang="js">
> 2 │ a == b;
│ ^^
2 │ delete a.c;
3
3 │ delete a.c;
4

i == is only allowed when comparing against null

> 1 │ a == b;
> 1 │ <script setup lang="js">
│ ^^
2 │ delete a.c;
3 │
2 │ a == b;
3 │ delete a.c;

i Using == may be unsafe if you are relying on type coercion

Expand All @@ -54,15 +55,16 @@ file.vue:1:3 lint/suspicious/noDoubleEquals FIXABLE ━━━━━━━━
```

```block
file.vue:2:1 lint/performance/noDelete FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
file.vue:3:1 lint/performance/noDelete FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Avoid the delete operator which can impact performance.

1 │ a == b;
> 2 │ delete a.c;
1 │ <script setup lang="js">
2 │ a == b;
> 3 │ delete a.c;
│ ^^^^^^^^^^
3
4 │ var foo = "";
4
5 │ var foo = "";

i Unsafe fix: Use an undefined assignment instead.

Expand All @@ -76,15 +78,16 @@ file.vue:2:1 lint/performance/noDelete 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 = "";
3 │ delete a.c;
4
> 5 │ var foo = "";
│ ^^^^^^^^^^^^
5 │
6 │ </script>
7 │ <template></template>

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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 │ <script setup lang="ts">
> 2 │ a == b;
│ ^^
2 │ delete a.c;
3
3 │ delete a.c;
4

i == is only allowed when comparing against null

> 1 │ a == b;
> 1 │ <script setup lang="ts">
│ ^^
2 │ delete a.c;
3 │
2 │ a == b;
3 │ delete a.c;

i Using == may be unsafe if you are relying on type coercion

Expand All @@ -54,15 +55,16 @@ file.vue:1:3 lint/suspicious/noDoubleEquals FIXABLE ━━━━━━━━
```

```block
file.vue:2:1 lint/performance/noDelete FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
file.vue:3:1 lint/performance/noDelete FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Avoid the delete operator which can impact performance.

1 │ a == b;
> 2 │ delete a.c;
1 │ <script setup lang="ts">
2 │ a == b;
> 3 │ delete a.c;
│ ^^^^^^^^^^
3
4 │ var foo: string = "";
4
5 │ var foo: string = "";

i Unsafe fix: Use an undefined assignment instead.

Expand All @@ -76,15 +78,16 @@ file.vue:2:1 lint/performance/noDelete FIXABLE ━━━━━━━━━━
```

```block
file.vue:4:8 lint/style/noInferrableTypes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
file.vue:5:8 lint/style/noInferrableTypes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× This type annotation is trivially inferred from its initialization.

2 │ delete a.c;
3
> 4 │ var foo: string = "";
3 │ delete a.c;
4
> 5 │ var foo: string = "";
│ ^^^^^^^^
5 │
6 │ </script>
7 │ <template></template>

i Safe fix: Remove the type annotation.

Expand All @@ -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 │ </script>
7 │ <template></template>

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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 9 additions & 0 deletions crates/biome_diagnostics/src/serde.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down