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

feat(analyzer): assists #3399

Merged
merged 29 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
66f234c
feat(analyzer): assists
ematipico Jul 9, 2024
eb669f2
chore: fix codegen and move tests
ematipico Jul 16, 2024
2b66a85
update snapshot
ematipico Jul 17, 2024
9003f22
rebase
ematipico Jul 23, 2024
032e6c7
chore: add assists in settings
ematipico Jul 26, 2024
97f62e4
chore: fix regression
ematipico Jul 26, 2024
f20dc4c
chore: fix regression in rules_check
ematipico Jul 26, 2024
241ae69
chore: apply assist via CLI
ematipico Jul 26, 2024
8accf96
remove information we don't need anymore and DRY the business logic
ematipico Jul 27, 2024
eded27e
rename visitor
ematipico Jul 27, 2024
a6e5ea0
rebase
ematipico Jul 27, 2024
c6a45a7
fix configuration for assists
ematipico Jul 29, 2024
1056515
new test
ematipico Jul 29, 2024
9f9366b
chore: rename `source` because they are applied to the whole file
ematipico Jul 29, 2024
5cab7b1
chore: fix the configuration merging
ematipico Jul 29, 2024
8d5d85d
more refactors yaaaay
ematipico Jul 29, 2024
395f740
fix the off setting
ematipico Jul 29, 2024
1f76553
update `.gitattributes`
ematipico Jul 29, 2024
6cab559
enable assists by default
ematipico Jul 29, 2024
b0dde12
add CLI flags
ematipico Jul 29, 2024
cda9a7b
change name of the folder
ematipico Jul 30, 2024
a9c83a8
chore: name to `actions`, remove `recommended` and `all`
ematipico Jul 31, 2024
2fca9b2
codegen
ematipico Jul 31, 2024
67cce26
Update crates/biome_cli/src/commands/mod.rs
ematipico Jul 31, 2024
15da4d6
update snapshots
ematipico Jul 31, 2024
1a17ffb
fix command
ematipico Aug 1, 2024
dc19ab2
address feedback
ematipico Aug 4, 2024
ef34e55
apply feedback
ematipico Aug 5, 2024
7a76baa
rebase
ematipico Aug 5, 2024
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
27 changes: 19 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@
/crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs linguist-generated=true text=auto eol=lf
/crates/biome_configuration/src/generated.rs linguist-generated=true text=auto eol=lf
/crates/biome_configuration/src/linter/rules.rs linguist-generated=true text=auto eol=lf
/crates/biome_configuration/src/assists/rules.rs linguist-generated=true text=auto eol=lf
/crates/biome_configuration/src/parse/json/rules.rs linguist-generated=true text=auto eol=lf
/crates/biome_css_analyze/src/lint.rs linguist-generated=true text=auto eol=lf
/crates/biome_css_analyze/src/lint/*.rs linguist-generated=true text=auto eol=lf
# GraphQL
/crates/biome_graphql_analyze/src/{lint,assists,syntax}.rs linguist-generated=true text=auto eol=lf
/crates/biome_graphql_analyze/src/{lint,assists,syntax}/*.rs linguist-generated=true text=auto eol=lf
/crates/biome_graphql_analyze/src/options.rs linguist-generated=true text=auto eol=lf
/crates/biome_graphql_analyze/src/registry.rs linguist-generated=true text=auto eol=lf
# CSS
/crates/biome_css_analyze/src/{lint,assists,syntax}.rs linguist-generated=true text=auto eol=lf
/crates/biome_css_analyze/src/{lint,assists,syntax}/*.rs linguist-generated=true text=auto eol=lf
/crates/biome_css_analyze/src/options.rs linguist-generated=true text=auto eol=lf
/crates/biome_css_analyze/src/registry.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/assists.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/assists/*.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/lint.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/lint/*.rs linguist-generated=true text=auto eol=lf
# JSON
/crates/biome_json_analyze/src/{lint,assists,syntax}.rs linguist-generated=true text=auto eol=lf
/crates/biome_json_analyze/src/{lint,assists,syntax}/*.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/options.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/registry.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/syntax.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/syntax/*.rs linguist-generated=true text=auto eol=lf
# JS
/crates/biome_js_analyze/src/{lint,assists,syntax}.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/{lint,assists,syntax}/*.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/options.rs linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/src/registry.rs linguist-generated=true text=auto eol=lf

# Other
/crates/biome_unicode_table/src/tables.rs linguist-generated=true text=auto eol=lf
/packages/@biomejs/backend-jsonrpc/src/workspace.ts linguist-generated=true text=auto eol=lf
/packages/@biomejs/biome/configuration_schema.json linguist-generated=true text=auto eol=lf
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
{
"$schema": "./packages/@biomejs/biome/configuration_schema.json",
"assists": {
"enabled": true,
"ignore": [
"./packages/@biomejs/biome/configuration_schema.json"
],
"actions": {
"source": {
"useSortedKeys": "off"
}
}
},
"css": {
"linter": {
"enabled": true
}
},
"files": {
"ignore": [
"crates/**",
Expand Down Expand Up @@ -29,11 +45,6 @@
"lineWidth": 1
}
},
"vcs": {
"clientKind": "git",
"enabled": true,
"useIgnoreFile": true
},
"linter": {
"enabled": true,
"rules": {
Expand All @@ -45,5 +56,10 @@
},
"organizeImports": {
"enabled": true
},
"vcs": {
"clientKind": "git",
"enabled": true,
"useIgnoreFile": true
}
}
4 changes: 2 additions & 2 deletions crates/biome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ macro_rules! declare_rule {
/// Check [crate](module documentation) for a better
/// understanding of how the macro works
#[macro_export]
macro_rules! declare_refactor_rule {
macro_rules! declare_source_rule {
( $( #[doc = $doc:literal] )+ $vis:vis $id:ident {
version: $version:literal,
name: $name:tt,
Expand All @@ -515,7 +515,7 @@ macro_rules! declare_refactor_rule {
/// This macro returns the corresponding [ActionCategory] to use inside the [RuleAction]
#[allow(unused_macros)]
macro_rules! rule_action_category {
() => { ActionCategory::Refactor(RefactorKind::Other(Cow::Borrowed(concat!($language, ".", $name) ))) };
() => { ActionCategory::Source(SourceActionKind::Other(Cow::Borrowed(concat!($language, ".", $name) ))) };
}
};
}
Expand Down
11 changes: 11 additions & 0 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::execute::VcsTargeted;
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_configuration::analyzer::assists::PartialAssistsConfiguration;
use biome_configuration::{
organize_imports::PartialOrganizeImports, PartialConfiguration, PartialFormatterConfiguration,
PartialLinterConfiguration,
Expand Down Expand Up @@ -36,6 +37,7 @@ pub(crate) struct CheckCommandPayload {
pub(crate) formatter_enabled: Option<bool>,
pub(crate) linter_enabled: Option<bool>,
pub(crate) organize_imports_enabled: Option<bool>,
pub(crate) assists_enabled: Option<bool>,
pub(crate) staged: bool,
pub(crate) changed: bool,
pub(crate) since: Option<String>,
Expand All @@ -60,6 +62,7 @@ pub(crate) fn check(
organize_imports_enabled,
formatter_enabled,
since,
assists_enabled,
staged,
changed,
} = payload;
Expand Down Expand Up @@ -148,6 +151,14 @@ pub(crate) fn check(
organize_imports.enabled = organize_imports_enabled;
}

let assists = fs_configuration
.assists
.get_or_insert_with(PartialAssistsConfiguration::default);

if assists_enabled.is_some() {
assists.enabled = assists_enabled;
}

if let Some(mut configuration) = configuration {
if let Some(linter) = configuration.linter.as_mut() {
// Don't overwrite rules from the CLI configuration.
Expand Down
11 changes: 11 additions & 0 deletions crates/biome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::cli_options::CliOptions;
use crate::commands::{resolve_manifest, validate_configuration_diagnostics};
use crate::execute::VcsTargeted;
use crate::{execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution};
use biome_configuration::analyzer::assists::PartialAssistsConfiguration;
use biome_configuration::{organize_imports::PartialOrganizeImports, PartialConfiguration};
use biome_configuration::{PartialFormatterConfiguration, PartialLinterConfiguration};
use biome_deserialize::Merge;
Expand All @@ -16,6 +17,7 @@ pub(crate) struct CiCommandPayload {
pub(crate) formatter_enabled: Option<bool>,
pub(crate) linter_enabled: Option<bool>,
pub(crate) organize_imports_enabled: Option<bool>,
pub(crate) assists_enabled: Option<bool>,
pub(crate) paths: Vec<OsString>,
pub(crate) configuration: Option<PartialConfiguration>,
pub(crate) cli_options: CliOptions,
Expand All @@ -30,6 +32,7 @@ pub(crate) fn ci(session: CliSession, payload: CiCommandPayload) -> Result<(), C
formatter_enabled,
linter_enabled,
organize_imports_enabled,
assists_enabled,
configuration,
mut paths,
since,
Expand Down Expand Up @@ -76,6 +79,14 @@ pub(crate) fn ci(session: CliSession, payload: CiCommandPayload) -> Result<(), C
organize_imports.enabled = organize_imports_enabled;
}

let assists = fs_configuration
.assists
.get_or_insert_with(PartialAssistsConfiguration::default);

if assists_enabled.is_some() {
assists.enabled = assists_enabled;
}

// no point in doing the traversal if all the checks have been disabled
if fs_configuration.is_formatter_disabled()
&& fs_configuration.is_linter_disabled()
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::execute::VcsTargeted;
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_configuration::analyzer::RuleSelector;
use biome_configuration::css::PartialCssLinter;
use biome_configuration::javascript::PartialJavascriptLinter;
use biome_configuration::json::PartialJsonLinter;
use biome_configuration::linter::RuleSelector;
use biome_configuration::vcs::PartialVcsConfiguration;
use biome_configuration::{
PartialConfiguration, PartialFilesConfiguration, PartialGraphqlLinter,
Expand Down
11 changes: 10 additions & 1 deletion crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::diagnostics::{DeprecatedArgument, DeprecatedConfigurationFile};
use crate::execute::Stdin;
use crate::logging::LoggingKind;
use crate::{CliDiagnostic, CliSession, LoggingLevel, VERSION};
use biome_configuration::analyzer::RuleSelector;
use biome_configuration::css::PartialCssLinter;
use biome_configuration::javascript::PartialJavascriptLinter;
use biome_configuration::json::PartialJsonLinter;
use biome_configuration::linter::RuleSelector;
use biome_configuration::{
css::partial_css_formatter, css::partial_css_linter, graphql::partial_graphql_formatter,
graphql::partial_graphql_linter, javascript::partial_javascript_formatter,
Expand Down Expand Up @@ -120,6 +120,11 @@ pub enum BiomeCommand {
hide_usage
)]
organize_imports_enabled: Option<bool>,

/// Allow to enable or disable the assists.
#[bpaf(long("assists-enabled"), argument("true|false"), optional)]
assists_enabled: Option<bool>,

#[bpaf(external(partial_configuration), hide_usage, optional)]
configuration: Option<PartialConfiguration>,
#[bpaf(external, hide_usage)]
Expand Down Expand Up @@ -312,6 +317,10 @@ pub enum BiomeCommand {
#[bpaf(long("organize-imports-enabled"), argument("true|false"), optional)]
organize_imports_enabled: Option<bool>,

/// Allow to enable or disable the assists.
#[bpaf(long("assists-enabled"), argument("true|false"), optional)]
assists_enabled: Option<bool>,

#[bpaf(external(partial_configuration), hide_usage, optional)]
configuration: Option<PartialConfiguration>,
#[bpaf(external, hide_usage)]
Expand Down
24 changes: 24 additions & 0 deletions crates/biome_cli/src/execute/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ pub(crate) struct CIOrganizeImportsDiffDiagnostic {
#[advice]
pub(crate) diff: ContentDiffAdvice,
}
#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "assists",
message = "Applied assists differs from the output"
)]
pub(crate) struct CIAssistsDiffDiagnostic {
#[location(resource)]
pub(crate) file_name: String,
#[advice]
pub(crate) diff: ContentDiffAdvice,
}

#[derive(Debug, Diagnostic)]
#[diagnostic(
Expand Down Expand Up @@ -56,6 +67,19 @@ pub(crate) struct OrganizeImportsDiffDiagnostic {
pub(crate) diff: ContentDiffAdvice,
}

#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "assists",
severity = Error,
message = "Not all assists were applied:"
)]
pub(crate) struct AssistsDiffDiagnostic {
#[location(resource)]
pub(crate) file_name: String,
#[advice]
pub(crate) diff: ContentDiffAdvice,
}

#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "migrate",
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::reporter::junit::{JunitReporter, JunitReporterVisitor};
use crate::reporter::summary::{SummaryReporter, SummaryReporterVisitor};
use crate::reporter::terminal::{ConsoleReporter, ConsoleReporterVisitor};
use crate::{CliDiagnostic, CliSession, DiagnosticsPayload, Reporter};
use biome_configuration::linter::RuleSelector;
use biome_configuration::analyzer::RuleSelector;
use biome_console::{markup, ConsoleExt};
use biome_diagnostics::adapters::SerdeJsonError;
use biome_diagnostics::{category, Category};
Expand Down Expand Up @@ -69,6 +69,7 @@ impl Execution {
.with_organize_imports()
.with_formatter()
.with_linter()
.with_assists()
.build(),
TraversalMode::Migrate { .. } => FeatureName::empty(),
TraversalMode::Search { .. } => FeaturesBuilder::new().with_search().build(),
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_cli/src/execute/process_file.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod assists;
mod check;
mod format;
mod lint;
Expand Down Expand Up @@ -71,6 +72,7 @@ impl Message {
pub(crate) enum DiffKind {
Format,
OrganizeImports,
Assists,
}

impl<D> From<D> for Message
Expand Down
72 changes: 72 additions & 0 deletions crates/biome_cli/src/execute/process_file/assists.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::execute::diagnostics::ResultExt;
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;

/// Lints a single file and returns a [FileResult]
pub(crate) fn assists_with_guard<'ctx>(
ctx: &'ctx SharedTraversalOptions<'ctx, '_>,
workspace_file: &mut WorkspaceFile,
) -> FileResult {
tracing::info_span!("Processes assists", path =? workspace_file.path.display()).in_scope(
move || {
let input = workspace_file.input()?;
let only = Vec::new();
let skip = Vec::new();
let fix_result = workspace_file
.guard()
.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!("assists"),
)?;

ctx.push_message(Message::SkippedFixes {
skipped_suggested_fixes: fix_result.skipped_suggested_fixes,
});

let mut output = fix_result.code;

match workspace_file.as_extension() {
Some("astro") => {
output = AstroFileHandler::output(input.as_str(), output.as_str());
}
Some("vue") => {
output = VueFileHandler::output(input.as_str(), output.as_str());
}
Some("svelte") => {
output = SvelteFileHandler::output(input.as_str(), output.as_str());
}
_ => {}
}
if input != output {
if ctx.execution.as_fix_file_mode().is_none() {
return Ok(FileStatus::Message(Message::Diff {
file_name: workspace_file.path.display().to_string(),
old: input,
new: output,
diff_kind: DiffKind::Assists,
}));
} else {
if output != input && ctx.execution.as_fix_file_mode().is_some() {
workspace_file.update_file(output)?;
}
Ok(FileStatus::Changed)
}
} else {
Ok(FileStatus::Unchanged)
}
},
)
}
Loading
Loading