Skip to content

Commit d4e0e64

Browse files
authored
perf: minor performance tweaks (#7122)
1 parent 76df523 commit d4e0e64

7 files changed

Lines changed: 138 additions & 82 deletions

File tree

crates/biome_analyze/src/categories.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ impl RuleCategories {
327327
pub fn is_syntax(&self) -> bool {
328328
self.0.contains(Categories::Syntax)
329329
}
330+
331+
pub fn is_lint(&self) -> bool {
332+
self.0.contains(Categories::Lint)
333+
}
334+
335+
pub fn is_assist(&self) -> bool {
336+
self.0.contains(Categories::Assist)
337+
}
330338
}
331339

332340
impl From<RuleCategory> for RuleCategories {

crates/biome_cli/tests/commands/format.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,3 +3507,29 @@ fn format_skip_parse_errors_continues_with_valid_files() {
35073507
result,
35083508
));
35093509
}
3510+
3511+
#[test]
3512+
fn should_not_format_file_with_syntax_errors() {
3513+
let fs = MemoryFileSystem::default();
3514+
let mut console = BufferConsole::default();
3515+
3516+
let invalid = Utf8Path::new("invalid.js");
3517+
fs.insert(invalid.into(), "while ) {}".as_bytes());
3518+
3519+
let (fs, result) = run_cli(
3520+
fs,
3521+
&mut console,
3522+
Args::from(["format", "--write", invalid.as_str()].as_slice()),
3523+
);
3524+
3525+
assert!(result.is_err(), "run_cli returned {result:?}");
3526+
3527+
assert_file_contents(&fs, invalid, "while ) {}");
3528+
assert_cli_snapshot(SnapshotPayload::new(
3529+
module_path!(),
3530+
"should_not_format_file_with_syntax_errors",
3531+
fs,
3532+
console,
3533+
result,
3534+
));
3535+
}

crates/biome_cli/tests/snapshots/main_commands_check/suppression_syntax_error.snap

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/biome_cli/tests/snap_test.rs
33
expression: redactor(content)
4-
snapshot_kind: text
54
---
65
## `check.js`
76

@@ -37,25 +36,9 @@ check.js:1:16 suppressions/parse ━━━━━━━━━━━━━━━
3736
i Example of suppression: // biome-ignore lint: reason
3837
3938
40-
```
41-
42-
```block
43-
check.js:1:16 suppressions/parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
44-
45-
× Unexpected token, expected one of ':' or whitespace.
46-
47-
> 1 │ // biome-ignore(:
48-
│ ^
49-
2 │
50-
51-
i A category is mandatory: try lint, format, assist or plugin.
52-
53-
i Example of suppression: // biome-ignore lint: reason
54-
55-
5639
```
5740

5841
```block
5942
Checked 1 file in <TIME>. No fixes applied.
60-
Found 2 errors.
43+
Found 1 error.
6144
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
source: crates/biome_cli/tests/snap_test.rs
3+
expression: redactor(content)
4+
---
5+
## `invalid.js`
6+
7+
```js
8+
while ) {}
9+
```
10+
11+
# Termination Message
12+
13+
```block
14+
format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
15+
16+
× No files were processed in the specified paths.
17+
18+
i Check your biome.json or biome.jsonc to ensure the paths are not ignored by the configuration.
19+
20+
i These paths were provided but ignored:
21+
22+
- invalid.js
23+
24+
25+
26+
```
27+
28+
# Emitted Messages
29+
30+
```block
31+
invalid.js:1:7 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
32+
33+
× expected `(` but instead found `)`
34+
35+
> 1 │ while ) {}
36+
│ ^
37+
38+
i Remove )
39+
40+
41+
```
42+
43+
```block
44+
invalid.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
45+
46+
× Code formatting aborted due to parsing errors. To format code with errors, enable the 'formatter.formatWithErrors' option.
47+
48+
49+
```
50+
51+
```block
52+
Formatted 1 file in <TIME>. No fixes applied.
53+
Found 1 error.
54+
```

crates/biome_fs/src/path.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ pub enum FileKinds {
3131
Manifest,
3232
/// An ignore file, like `.gitignore`
3333
Ignore,
34-
/// The path is a directory
35-
Directory,
3634
/// A file to handle has the lowest priority. It's usually a traversed file, or a file opened by the LSP
3735
#[default]
3836
Handleable,
@@ -54,11 +52,7 @@ pub struct BiomePath {
5452
impl BiomePath {
5553
pub fn new(path_to_file: impl Into<Utf8PathBuf>) -> Self {
5654
let path = path_to_file.into();
57-
let kind = if path.is_dir() {
58-
FileKinds::Directory
59-
} else {
60-
path.file_name().map(Self::priority).unwrap_or_default()
61-
};
55+
let kind = path.file_name().map(Self::priority).unwrap_or_default();
6256
Self {
6357
path,
6458
kind,
@@ -145,11 +139,6 @@ impl BiomePath {
145139
matches!(self.kind, FileKinds::Ignore)
146140
}
147141

148-
#[inline(always)]
149-
pub fn is_dir(&self) -> bool {
150-
matches!(self.kind, FileKinds::Directory)
151-
}
152-
153142
#[inline(always)]
154143
pub fn is_handleable(&self) -> bool {
155144
matches!(self.kind, FileKinds::Handleable)

crates/biome_service/src/settings.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -858,13 +858,7 @@ impl VcsIgnoredPatterns {
858858
};
859859

860860
let nested_ignored = nested.iter().any(|gitignore| {
861-
let ignore_directory = if gitignore.path().is_file() {
862-
// SAFETY: if it's a file, it always has a parent
863-
gitignore.path().parent().unwrap()
864-
} else {
865-
gitignore.path()
866-
};
867-
if let Ok(stripped_path) = path.strip_prefix(ignore_directory) {
861+
if let Ok(stripped_path) = path.strip_prefix(gitignore.path()) {
868862
gitignore.matched(stripped_path, is_dir).is_ignore()
869863
} else {
870864
false

crates/biome_service/src/workspace/server.rs

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,54 +1227,56 @@ impl Workspace for WorkspaceServer {
12271227
let parse = self.get_parse(&path)?;
12281228
let language = self.get_file_source(&path);
12291229
let capabilities = self.features.get_capabilities(language);
1230-
let (diagnostics, errors, skipped_diagnostics) =
1231-
if let Some(lint) = capabilities.analyzer.lint {
1232-
let settings = self
1233-
.projects
1234-
.get_settings_based_on_path(project_key, &path)
1235-
.ok_or_else(WorkspaceError::no_project)?;
1236-
1237-
let plugins = self
1238-
.get_analyzer_plugins_for_project(
1239-
settings.source_path().unwrap_or_default().as_path(),
1240-
&settings.get_plugins_for_path(&path),
1241-
)
1242-
.map_err(WorkspaceError::plugin_errors)?;
1243-
1244-
let results = lint(LintParams {
1245-
parse,
1246-
settings: &settings,
1247-
path: &path,
1248-
only,
1249-
skip,
1250-
language,
1251-
categories,
1252-
module_graph: self.module_graph.clone(),
1253-
project_layout: self.project_layout.clone(),
1254-
suppression_reason: None,
1255-
enabled_rules,
1256-
pull_code_actions,
1257-
plugins: if categories.contains(RuleCategory::Lint) {
1258-
plugins
1259-
} else {
1260-
Vec::new()
1261-
},
1262-
});
1230+
let (diagnostics, errors, skipped_diagnostics) = if (categories.is_lint()
1231+
|| categories.is_assist())
1232+
&& let Some(lint) = capabilities.analyzer.lint
1233+
{
1234+
let settings = self
1235+
.projects
1236+
.get_settings_based_on_path(project_key, &path)
1237+
.ok_or_else(WorkspaceError::no_project)?;
12631238

1264-
(
1265-
results.diagnostics,
1266-
results.errors,
1267-
results.skipped_diagnostics,
1239+
let plugins = self
1240+
.get_analyzer_plugins_for_project(
1241+
settings.source_path().unwrap_or_default().as_path(),
1242+
&settings.get_plugins_for_path(&path),
12681243
)
1269-
} else {
1270-
let parse_diagnostics = parse.into_diagnostics();
1271-
let errors = parse_diagnostics
1272-
.iter()
1273-
.filter(|diag| diag.severity() <= Severity::Error)
1274-
.count();
1244+
.map_err(WorkspaceError::plugin_errors)?;
1245+
1246+
let results = lint(LintParams {
1247+
parse,
1248+
settings: &settings,
1249+
path: &path,
1250+
only,
1251+
skip,
1252+
language,
1253+
categories,
1254+
module_graph: self.module_graph.clone(),
1255+
project_layout: self.project_layout.clone(),
1256+
suppression_reason: None,
1257+
enabled_rules,
1258+
pull_code_actions,
1259+
plugins: if categories.is_lint() {
1260+
plugins
1261+
} else {
1262+
Vec::new()
1263+
},
1264+
});
12751265

1276-
(parse_diagnostics, errors, 0)
1277-
};
1266+
(
1267+
results.diagnostics,
1268+
results.errors,
1269+
results.skipped_diagnostics,
1270+
)
1271+
} else {
1272+
let parse_diagnostics = parse.into_diagnostics();
1273+
let errors = parse_diagnostics
1274+
.iter()
1275+
.filter(|diag| diag.severity() <= Severity::Error)
1276+
.count();
1277+
1278+
(parse_diagnostics, errors, 0)
1279+
};
12781280

12791281
info!(
12801282
"Pulled {:?} diagnostic(s), skipped {:?} diagnostic(s) from {}",

0 commit comments

Comments
 (0)