Skip to content

Commit

Permalink
remove information we don't need anymore and DRY the business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 27, 2024
1 parent 474b112 commit 95c5cd4
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 65 deletions.
11 changes: 9 additions & 2 deletions crates/biome_cli/src/execute/process_file/assists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::execute::process_file::workspace_file::WorkspaceFile;
use crate::execute::process_file::{
DiffKind, FileResult, FileStatus, Message, SharedTraversalOptions,
};
use biome_analyze::RuleCategoriesBuilder;
use biome_diagnostics::category;
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use biome_service::workspace::FixFileMode;
Expand All @@ -19,10 +20,16 @@ pub(crate) fn assists_with_guard<'ctx>(
let skip = Vec::new();
let fix_result = workspace_file
.guard()
.fix_file(FixFileMode::SafeFixes, false, only.clone(), skip.clone())
.fix_file(
FixFileMode::SafeFixes,
false,
RuleCategoriesBuilder::default().with_action().build(),
only.clone(),
skip.clone(),
)
.with_file_path_and_code(
workspace_file.path.display().to_string(),
category!("lint"),
category!("assists"),
)?;

ctx.push_message(Message::SkippedFixes {
Expand Down
11 changes: 10 additions & 1 deletion crates/biome_cli/src/execute/process_file/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ pub(crate) fn lint_with_guard<'ctx>(
if let Some(fix_mode) = ctx.execution.as_fix_file_mode() {
let fix_result = workspace_file
.guard()
.fix_file(*fix_mode, false, only.clone(), skip.clone())
.fix_file(
*fix_mode,
false,
RuleCategoriesBuilder::default()
.with_syntax()
.with_lint()
.build(),
only.clone(),
skip.clone(),
)
.with_file_path_and_code(
workspace_file.path.display().to_string(),
category!("lint"),
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ pub(crate) fn run<'a>(
should_format: mode.is_check() && file_features.supports_format(),
only: only.clone(),
skip: skip.clone(),
rule_categories: RuleCategoriesBuilder::default()
.with_syntax()
.with_lint()
.build(),
})?;
let code = fix_file_result.code;
let output = match biome_path.extension_as_str() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{ "assists": { "enabled": true }, "formatter": { "enabled": false } }
```

## `file.json`

```json
{ "zod": true, "lorem": "ipsum", "foo": "bar" }
```

# Termination Message

```block
check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Some errors were emitted while running checks.
```

# Emitted Messages

```block
file.json assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Not all assists were applied:
- {·"zod":·true,·"lorem":·"ipsum",·"foo":·"bar"·}
+ {·"foo":·"bar"·,"lorem":·"ipsum","zod":·true·}
```

```block
Checked 1 file in <TIME>. No fixes applied.
Found 1 error.
```
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ check.js organizeImports ━━━━━━━━━━━━━━━━━━
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
check.js assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of check.js is 1.0 MiB which exceeds configured maximum of 1.0 MiB for this project.
The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ check.js organizeImports ━━━━━━━━━━━━━━━━━━
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
check.js assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of check.js is 27 B which exceeds configured maximum of 16 B for this project.
The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ check.js organizeImports ━━━━━━━━━━━━━━━━━━
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
check.js assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of check.js is 27 B which exceeds configured maximum of 16 B for this project.
The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ ci.js organizeImports ━━━━━━━━━━━━━━━━━━━
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
ci.js assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of ci.js is 1.0 MiB which exceeds configured maximum of 1.0 MiB for this project.
The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ ci.js organizeImports ━━━━━━━━━━━━━━━━━━━
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
ci.js assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of ci.js is 27 B which exceeds configured maximum of 16 B for this project.
The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ ci.js organizeImports ━━━━━━━━━━━━━━━━━━━
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
ci.js assists ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Size of ci.js is 27 B which exceeds configured maximum of 16 B for this project.
The file size limit exists to prevent us inadvertently slowing down and loading large files that we shouldn't.
Use the `files.maxSize` configuration to change the maximum size of files processed.
```

```block
Expand Down
6 changes: 5 additions & 1 deletion crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::converters::line_index::LineIndex;
use crate::session::Session;
use crate::utils;
use anyhow::{Context, Result};
use biome_analyze::{ActionCategory, SourceActionKind};
use biome_analyze::{ActionCategory, RuleCategoriesBuilder, SourceActionKind};
use biome_diagnostics::Applicability;
use biome_fs::BiomePath;
use biome_rowan::{TextRange, TextSize};
Expand Down Expand Up @@ -243,6 +243,10 @@ fn fix_all(
should_format,
only: vec![],
skip: vec![],
rule_categories: RuleCategoriesBuilder::default()
.with_syntax()
.with_lint()
.build(),
})?;

if fixed.actions.is_empty() {
Expand Down
48 changes: 24 additions & 24 deletions crates/biome_service/src/file_handlers/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use biome_parser::AnyParse;
use biome_rowan::{AstNode, NodeCache};
use biome_rowan::{TextRange, TextSize, TokenAtOffset};
use std::borrow::Cow;
use tracing::{debug_span, error, info, trace, trace_span};
use tracing::{debug_span, error, info, trace_span};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -463,41 +463,41 @@ pub(crate) fn code_actions(params: CodeActionsParams) -> PullActionsResult {
path,
manifest: _,
language,
settings,
only: _,
skip: _,
only,
skip,
} = params;
debug_span!("Code actions CSS", range =? range, path =? path).in_scope(move || {
let tree = parse.tree();
trace_span!("Parsed file", tree =? tree).in_scope(move || {
let rules = settings.as_linter_rules(params.path.as_path());
let filter = rules
.as_ref()
.map(|rules| rules.as_enabled_rules())
.unwrap_or_default()
.into_iter()
.collect::<Vec<_>>();

let mut filter = AnalysisFilter::from_enabled_rules(filter.as_slice());

let mut categories = RuleCategoriesBuilder::default().with_syntax().with_lint();
if settings.organize_imports.enabled {
categories = categories.with_action();
}
filter.categories = categories.build();
filter.range = range;

let analyzer_options = workspace.analyzer_options::<CssLanguage>(path, &language);

let Some(_) = language.to_css_file_source() else {
error!("Could not determine the file source of the file");
return PullActionsResult {
actions: Vec::new(),
};
};

trace!("CSS runs the analyzer");
let analyzer_options = workspace.analyzer_options::<CssLanguage>(path, &language);
let mut actions = Vec::new();
let mut lint_visitor =
LintVisitor::new(&only, &skip, workspace.settings(), path.as_path());
let mut syntax_visitor = SyntaxVisitor::default();
biome_js_analyze::visit_registry(&mut lint_visitor);
biome_js_analyze::visit_registry(&mut syntax_visitor);
let (mut enabled_rules, disabled_rules) = lint_visitor.finish();
enabled_rules.extend(syntax_visitor.enabled_rules);

let filter = AnalysisFilter {
categories: RuleCategoriesBuilder::default()
.with_syntax()
.with_lint()
.with_action()
.build(),
enabled_rules: Some(enabled_rules.as_slice()),
disabled_rules: &disabled_rules,
range,
};

info!("CSS runs the analyzer");

analyze(&tree, filter, &analyzer_options, |signal| {
actions.extend(signal.actions().into_code_action_iter().map(|item| {
Expand Down
44 changes: 22 additions & 22 deletions crates/biome_service/src/file_handlers/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use biome_graphql_syntax::{GraphqlLanguage, GraphqlRoot, GraphqlSyntaxNode, Text
use biome_parser::AnyParse;
use biome_rowan::{AstNode, NodeCache, TokenAtOffset};
use std::borrow::Cow;
use tracing::{debug_span, error, info, trace, trace_span};
use tracing::{debug_span, error, info, trace_span};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -416,41 +416,41 @@ pub(crate) fn code_actions(params: CodeActionsParams) -> PullActionsResult {
path,
manifest: _,
language,
settings,
only,
skip,
} = params;
debug_span!("Code actions GraphQL", range =? range, path =? path).in_scope(move || {
let tree = parse.tree();
trace_span!("Parsed file", tree =? tree).in_scope(move || {
let rules = settings.as_linter_rules(params.path.as_path());
let filter = rules
.as_ref()
.map(|rules| rules.as_enabled_rules())
.unwrap_or_default()
.into_iter()
.collect::<Vec<_>>();

let mut filter = AnalysisFilter::from_enabled_rules(filter.as_slice());

let mut categories = RuleCategoriesBuilder::default().with_syntax().with_lint();
if settings.organize_imports.enabled {
categories = categories.with_action();
}
filter.categories = categories.build();
filter.range = range;

let analyzer_options = workspace.analyzer_options::<GraphqlLanguage>(path, &language);

let Some(_) = language.to_graphql_file_source() else {
error!("Could not determine the file source of the file");
return PullActionsResult {
actions: Vec::new(),
};
};

trace!("GraphQL runs the analyzer");
let analyzer_options = workspace.analyzer_options::<GraphqlLanguage>(path, &language);
let mut actions = Vec::new();
let mut lint_visitor =
LintVisitor::new(&only, &skip, workspace.settings(), path.as_path());
let mut syntax_visitor = SyntaxVisitor::default();
biome_js_analyze::visit_registry(&mut lint_visitor);
biome_js_analyze::visit_registry(&mut syntax_visitor);
let (mut enabled_rules, disabled_rules) = lint_visitor.finish();
enabled_rules.extend(syntax_visitor.enabled_rules);

let filter = AnalysisFilter {
categories: RuleCategoriesBuilder::default()
.with_syntax()
.with_lint()
.with_action()
.build(),
enabled_rules: Some(enabled_rules.as_slice()),
disabled_rules: &disabled_rules,
range,
};

info!("GraphQL runs the analyzer");

analyze(&tree, filter, &analyzer_options, |signal| {
actions.extend(signal.actions().into_code_action_iter().map(|item| {
Expand Down
10 changes: 3 additions & 7 deletions crates/biome_service/src/file_handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
};
use biome_analyze::options::PreferredQuote;
use biome_analyze::{
AnalysisFilter, AnalyzerConfiguration, AnalyzerOptions, ControlFlow, Never,
RuleCategoriesBuilder, RuleCategory, RuleError,
AnalysisFilter, AnalyzerConfiguration, AnalyzerOptions, ControlFlow, Never, QueryMatch,
RuleCategoriesBuilder, RuleCategory, RuleError, RuleFilter,
};
use biome_configuration::javascript::JsxRuntime;
use biome_diagnostics::{category, Applicability, Diagnostic, DiagnosticExt, Severity};
Expand Down Expand Up @@ -539,7 +539,6 @@ pub(crate) fn code_actions(params: CodeActionsParams) -> PullActionsResult {
path,
manifest,
language,
settings,
only,
skip,
} = params;
Expand Down Expand Up @@ -629,10 +628,7 @@ pub(crate) fn fix_all(params: FixAllParams) -> Result<FixFileResult, WorkspaceEr
enabled_rules.extend(syntax_visitor.enabled_rules);

let filter = AnalysisFilter {
categories: RuleCategoriesBuilder::default()
.with_syntax()
.with_lint()
.build(),
categories: params.rule_categories,
enabled_rules: Some(enabled_rules.as_slice()),
disabled_rules: &disabled_rules,
range: None,
Expand Down
Loading

0 comments on commit 95c5cd4

Please sign in to comment.