Skip to content

Commit

Permalink
fix(lint): allow only/skip filter when --write is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jul 22, 2024
1 parent 6f8eade commit 282fa09
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 125 deletions.
14 changes: 7 additions & 7 deletions crates/biome_cli/src/execute/process_file/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ pub(crate) fn lint_with_guard<'ctx>(
move || {
let mut input = workspace_file.input()?;
let mut changed = false;
let (only, skip) =
if let TraversalMode::Lint { only, skip, .. } = ctx.execution.traversal_mode() {
(only.clone(), skip.clone())
} else {
(Vec::new(), Vec::new())
};
if let Some(fix_mode) = ctx.execution.as_fix_file_mode() {
let fix_result = workspace_file
.guard()
.fix_file(*fix_mode, false)
.fix_file(*fix_mode, false, only.clone(), skip.clone())
.with_file_path_and_code(
workspace_file.path.display().to_string(),
category!("lint"),
Expand Down Expand Up @@ -57,12 +63,6 @@ pub(crate) fn lint_with_guard<'ctx>(
}

let max_diagnostics = ctx.remaining_diagnostics.load(Ordering::Relaxed);
let (only, skip) =
if let TraversalMode::Lint { only, skip, .. } = ctx.execution.traversal_mode() {
(only.clone(), skip.clone())
} else {
(Vec::new(), Vec::new())
};
let pull_diagnostics_result = workspace_file
.guard()
.pull_diagnostics(
Expand Down
13 changes: 8 additions & 5 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,20 @@ pub(crate) fn run<'a>(
return Ok(());
};

let (only, skip) = if let TraversalMode::Lint { only, skip, .. } = mode.traversal_mode() {
(only.clone(), skip.clone())
} else {
(Vec::new(), Vec::new())
};

if let Some(fix_file_mode) = mode.as_fix_file_mode() {
if file_features.supports_lint() {
let fix_file_result = workspace.fix_file(FixFileParams {
fix_file_mode: *fix_file_mode,
path: biome_path.clone(),
should_format: mode.is_check() && file_features.supports_format(),
only: only.clone(),
skip: skip.clone(),
})?;
let code = fix_file_result.code;
let output = match biome_path.extension_as_str() {
Expand Down Expand Up @@ -156,11 +164,6 @@ pub(crate) fn run<'a>(
}
}

let (only, skip) = if let TraversalMode::Lint { only, skip, .. } = mode.traversal_mode() {
(only.clone(), skip.clone())
} else {
(Vec::new(), Vec::new())
};
if !mode.is_check_apply_unsafe() {
let result = workspace.pull_diagnostics(PullDiagnosticsParams {
categories: RuleCategoriesBuilder::default()
Expand Down
80 changes: 80 additions & 0 deletions crates/biome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3806,6 +3806,46 @@ fn lint_only_group_with_disabled_rule() {
));
}

#[test]
fn lint_only_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();
let config = r#"{}"#;
let content = r#"
export const z = function (array) {
array.map((sentence) => sentence.split(" ")).flat();
return 0;
};
"#;

let file_path = Path::new("check.js");
fs.insert(file_path.into(), content.as_bytes());
let config_path = Path::new("biome.json");
fs.insert(config_path.into(), config.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("lint"),
"--write",
"--only=complexity/useArrowFunction",
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"lint_only_write",
fs,
console,
result,
));
}

#[test]
fn lint_skip_rule() {
let mut fs = MemoryFileSystem::default();
Expand Down Expand Up @@ -3943,6 +3983,46 @@ fn lint_skip_rule_and_group() {
));
}

#[test]
fn lint_skip_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();
let config = r#"{}"#;
let content = r#"
export const z = function (array) {
array.map((sentence) => sentence.split(" ")).flat();
return 0;
};
"#;

let file_path = Path::new("check.js");
fs.insert(file_path.into(), content.as_bytes());
let config_path = Path::new("biome.json");
fs.insert(config_path.into(), config.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("lint"),
"--write",
"--skip=complexity/useArrowFunction",
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"lint_skip_write",
fs,
console,
result,
));
}

#[test]
fn lint_only_group_skip_rule() {
let mut fs = MemoryFileSystem::default();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{}
```

## `check.js`

```js

export const z = (array) => {
array.map((sentence) => sentence.split(" ")).flat();
return 0;
};

```

# Emitted Messages

```block
Checked 1 file in <TIME>. Fixed 1 file.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{}
```

## `check.js`

```js

export const z = function (array) {
array.flatMap((sentence) => sentence.split(" "));
return 0;
};

```

# Emitted Messages

```block
Checked 1 file in <TIME>. Fixed 1 file.
```
2 changes: 2 additions & 0 deletions crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ fn fix_all(
path: biome_path,
fix_file_mode: FixFileMode::SafeFixes,
should_format,
only: vec![],
skip: vec![],
})?;

if fixed.actions.is_empty() {
Expand Down
82 changes: 51 additions & 31 deletions crates/biome_service/src/file_handlers/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl ServiceLanguage for CssLanguage {
rules: global
.map(|g| to_analyzer_rules(g, file_path.as_path()))
.unwrap_or_default(),
globals: vec![],
globals: Vec::new(),
preferred_quote,
jsx_runtime: None,
};
Expand Down Expand Up @@ -362,7 +362,7 @@ fn lint(params: LintParams) -> LintResults {
.collect::<Vec<_>>()
}
} else {
vec![]
Vec::new()
};

let mut syntax_visitor = SyntaxVisitor::default();
Expand Down Expand Up @@ -499,7 +499,9 @@ pub(crate) fn code_actions(params: CodeActionsParams) -> PullActionsResult {

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

trace!("CSS runs the analyzer");
Expand All @@ -526,45 +528,60 @@ pub(crate) fn code_actions(params: CodeActionsParams) -> PullActionsResult {

/// If applies all the safe fixes to the given syntax tree.
pub(crate) fn fix_all(params: FixAllParams) -> Result<FixFileResult, WorkspaceError> {
let FixAllParams {
parse,
fix_file_mode,
workspace,
should_format,
biome_path,
manifest: _,
document_file_source,
} = params;

let settings = workspace.settings();
let Some(settings) = settings else {
let tree: CssRoot = parse.tree();

let mut tree: CssRoot = params.parse.tree();
let Some(settings) = params.workspace.settings() else {
return Ok(FixFileResult {
actions: vec![],
actions: Vec::new(),
errors: 0,
skipped_suggested_fixes: 0,
code: tree.syntax().to_string(),
});
};

let mut tree: CssRoot = parse.tree();
let mut actions = Vec::new();

// Compute final rules (taking `overrides` into account)
let rules = settings.as_rules(params.biome_path.as_path());
let rule_filter_list = rules
.as_ref()
.map(|rules| rules.as_enabled_rules())
.unwrap_or_default()

let mut enabled_rules = if !params.only.is_empty() {
params
.only
.into_iter()
.map(|selector| selector.into())
.collect::<Vec<_>>()
} else {
rules
.as_ref()
.map(|rules| rules.as_enabled_rules())
.unwrap_or_default()
.into_iter()
.collect::<Vec<_>>()
};

let mut syntax_visitor = SyntaxVisitor::default();
visit_registry(&mut syntax_visitor);
enabled_rules.extend(syntax_visitor.enabled_rules);

let disabled_rules = params
.skip
.into_iter()
.map(|selector| selector.into())
.collect::<Vec<_>>();
let filter = AnalysisFilter::from_enabled_rules(rule_filter_list.as_slice());

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

let mut actions = Vec::new();
let mut skipped_suggested_fixes = 0;
let mut errors: u16 = 0;
let analyzer_options =
workspace.analyzer_options::<CssLanguage>(biome_path, &document_file_source);
let analyzer_options = params
.workspace
.analyzer_options::<CssLanguage>(params.biome_path, &params.document_file_source);
loop {
let (action, _) = analyze(&tree, filter, &analyzer_options, |signal| {
let current_diagnostic = signal.diagnostic();
Expand All @@ -581,7 +598,7 @@ pub(crate) fn fix_all(params: FixAllParams) -> Result<FixFileResult, WorkspaceEr
continue;
}

match fix_file_mode {
match params.fix_file_mode {
FixFileMode::SafeFixes => {
if action.applicability == Applicability::MaybeIncorrect {
skipped_suggested_fixes += 1;
Expand Down Expand Up @@ -632,9 +649,12 @@ pub(crate) fn fix_all(params: FixAllParams) -> Result<FixFileResult, WorkspaceEr
}
}
None => {
let code = if should_format {
let code = if params.should_format {
format_node(
workspace.format_options::<CssLanguage>(biome_path, &document_file_source),
params.workspace.format_options::<CssLanguage>(
params.biome_path,
&params.document_file_source,
),
tree.syntax(),
)?
.print()?
Expand Down
Loading

0 comments on commit 282fa09

Please sign in to comment.