Skip to content

Commit

Permalink
refactor: expose ProjectLayout as service (#4851)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Jan 8, 2025
1 parent 01044aa commit 2ae40a7
Show file tree
Hide file tree
Showing 53 changed files with 476 additions and 313 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ biome_markdown_factory = { version = "0.0.1", path = "./crates/biome_markd
biome_markdown_parser = { version = "0.0.1", path = "./crates/biome_markdown_parser" }
biome_markdown_syntax = { version = "0.0.1", path = "./crates/biome_markdown_syntax" }
biome_plugin_loader = { version = "0.0.1", path = "./crates/biome_plugin_loader" }
biome_project_layout = { version = "0.0.1", path = "./crates/biome_project_layout" }
biome_ungrammar = { version = "0.3.1", path = "./crates/biome_ungrammar" }
biome_yaml_factory = { version = "0.0.1", path = "./crates/biome_yaml_factory" }
biome_yaml_parser = { version = "0.0.1", path = "./crates/biome_yaml_parser" }
Expand Down
1 change: 1 addition & 0 deletions benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "8.18.1",
"@typescript-eslint/parser": "8.3.0",
"dprint": "0.48.0",
"eslint": "9.17.0",
"prettier": "3.3.3"
Expand Down
3 changes: 3 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noUndeclaredDependencies": "error"
},
"style": {
"noNonNullAssertion": "off",
"useNodejsImportProtocol": "error"
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ biome_diagnostics = { workspace = true }
biome_parser = { workspace = true }
biome_rowan = { workspace = true }
biome_suppression = { workspace = true }
camino = { workspace = true }
enumflags2 = { workspace = true }
indexmap = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
tracing = { workspace = true }


[features]
schema = ["dep:schemars", "biome_console/schema", "serde"]
serde = ["dep:serde", "dep:biome_deserialize", "dep:biome_deserialize_macros"]
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_analyze/src/analyzer_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::RuleDiagnostic;
use biome_parser::AnyParse;
use std::{fmt::Debug, path::PathBuf};
use camino::Utf8PathBuf;
use std::fmt::Debug;

/// Definition of an analyzer plugin.
pub trait AnalyzerPlugin: Debug {
fn evaluate(&self, root: AnyParse, path: PathBuf) -> Vec<RuleDiagnostic>;
fn evaluate(&self, root: AnyParse, path: Utf8PathBuf) -> Vec<RuleDiagnostic>;

fn supports_css(&self) -> bool;

Expand Down
8 changes: 4 additions & 4 deletions crates/biome_analyze/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::options::{JsxRuntime, PreferredQuote};
use crate::{registry::RuleRoot, FromServices, Queryable, Rule, RuleKey, ServiceBag};
use crate::{GroupCategory, RuleCategory, RuleGroup, RuleMetadata};
use biome_diagnostics::{Error, Result};
use camino::Utf8Path;
use std::ops::Deref;
use std::path::Path;

type RuleQueryResult<R> = <<R as Rule>::Query as Queryable>::Output;
type RuleServiceBag<R> = <<R as Rule>::Query as Queryable>::Services;
Expand All @@ -14,7 +14,7 @@ pub struct RuleContext<'a, R: Rule> {
bag: &'a ServiceBag,
services: RuleServiceBag<R>,
globals: &'a [&'a str],
file_path: &'a Path,
file_path: &'a Utf8Path,
options: &'a R::Options,
preferred_quote: &'a PreferredQuote,
jsx_runtime: Option<JsxRuntime>,
Expand All @@ -30,7 +30,7 @@ where
root: &'a RuleRoot<R>,
services: &'a ServiceBag,
globals: &'a [&'a str],
file_path: &'a Path,
file_path: &'a Utf8Path,
options: &'a R::Options,
preferred_quote: &'a PreferredQuote,
jsx_runtime: Option<JsxRuntime>,
Expand Down Expand Up @@ -159,7 +159,7 @@ where
}

/// The file path of the current file
pub fn file_path(&self) -> &Path {
pub fn file_path(&self) -> &Utf8Path {
self.file_path
}

Expand Down
6 changes: 3 additions & 3 deletions crates/biome_analyze/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use camino::Utf8PathBuf;
use rustc_hash::FxHashMap;

use crate::{FixKind, Rule, RuleKey};
use std::any::{Any, TypeId};
use std::fmt::Debug;
use std::path::PathBuf;

/// A convenient new type data structure to store the options that belong to a rule
#[derive(Debug)]
Expand Down Expand Up @@ -98,14 +98,14 @@ pub struct AnalyzerOptions {
pub(crate) configuration: AnalyzerConfiguration,

/// The file that is being analyzed
pub(crate) file_path: PathBuf,
pub file_path: Utf8PathBuf,

/// Suppression reason used when applying a suppression code action
pub(crate) suppression_reason: Option<String>,
}

impl AnalyzerOptions {
pub fn with_file_path(mut self, file_path: impl Into<PathBuf>) -> Self {
pub fn with_file_path(mut self, file_path: impl Into<Utf8PathBuf>) -> Self {
self.file_path = file_path.into();
self
}
Expand Down
38 changes: 23 additions & 15 deletions crates/biome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::configs::{
CONFIG_LINTER_SUPPRESSED_GROUP, CONFIG_LINTER_SUPPRESSED_RULE,
CONFIG_LINTER_UPGRADE_DIAGNOSTIC, CONFIG_RECOMMENDED_GROUP,
};
use crate::snap_test::{assert_file_contents, markup_to_string, SnapshotPayload};
use crate::snap_test::{
assert_cli_snapshot_with_redactor, assert_file_contents, markup_to_string, SnapshotPayload,
};
use crate::{
assert_cli_snapshot, run_cli, run_cli_with_dyn_fs, run_cli_with_server_workspace, FORMATTED,
LINT_ERROR, PARSE_ERROR,
Expand Down Expand Up @@ -3879,13 +3881,16 @@ fn linter_finds_package_json_for_no_undeclared_dependencies() {
&mut console,
Args::from(["lint", file.as_str()].as_slice()),
);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"linter_finds_package_json_for_no_undeclared_dependencies",
fs,
console,
result,
));
assert_cli_snapshot_with_redactor(
SnapshotPayload::new(
module_path!(),
"linter_finds_package_json_for_no_undeclared_dependencies",
fs,
console,
result,
),
|content| content.replace("frontend\\", "frontend/"),
);
}

#[test]
Expand Down Expand Up @@ -3934,13 +3939,16 @@ fn linter_finds_nested_package_json_for_no_undeclared_dependencies() {
&mut console,
Args::from(["lint", file.as_str()].as_slice()),
);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"linter_finds_nested_package_json_for_no_undeclared_dependencies",
fs,
console,
result,
));
assert_cli_snapshot_with_redactor(
SnapshotPayload::new(
module_path!(),
"linter_finds_nested_package_json_for_no_undeclared_dependencies",
fs,
console,
result,
),
|content| content.replace("frontend\\", "frontend/"),
);
}

#[test]
Expand Down
16 changes: 14 additions & 2 deletions crates/biome_cli/tests/snap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use camino::{Utf8Path, Utf8PathBuf};
use regex::Regex;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::convert::identity;
use std::env::{current_exe, temp_dir};
use std::fmt::Write as _;
use std::path::MAIN_SEPARATOR;
Expand Down Expand Up @@ -405,6 +406,17 @@ impl<'a> SnapshotPayload<'a> {

/// Function used to snapshot a session test of the a CLI run.
pub fn assert_cli_snapshot(payload: SnapshotPayload<'_>) {
assert_cli_snapshot_with_redactor(payload, identity)
}

/// Used to snapshot a session test of the a CLI run.
///
/// Takes a custom `redactor` that allows the snapshotted content to be
/// normalized so it remains stable across test runs.
pub fn assert_cli_snapshot_with_redactor(
payload: SnapshotPayload<'_>,
redactor: impl FnOnce(String) -> String,
) {
let module_path = payload.module_path.to_owned();
let test_name = payload.test_name;
let cli_snapshot = CliSnapshot::from(payload);
Expand All @@ -416,9 +428,9 @@ pub fn assert_cli_snapshot(payload: SnapshotPayload<'_>) {

insta::with_settings!({
prepend_module_to_snapshot => false,
snapshot_path => snapshot_path
snapshot_path => snapshot_path,
}, {
insta::assert_snapshot!(test_name, content);
insta::assert_snapshot!(test_name, redactor(content));

});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import 'react-dom'
```block
frontend/file1.js:1:8 lint/correctness/noUndeclaredDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i The current dependency isn't specified in your package.json.
i Dependency react-dom isn't specified in frontend/package.json.
> 1 │ import 'react-dom'
│ ^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import 'react-dom'
```block
frontend/file1.js:1:8 lint/correctness/noUndeclaredDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i The current dependency isn't specified in your package.json.
i Dependency react-dom isn't specified in frontend/package.json.
> 1 │ import 'react-dom'
│ ^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions crates/biome_css_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ biome_diagnostics = { workspace = true }
biome_rowan = { workspace = true }
biome_string_case = { workspace = true }
biome_suppression = { workspace = true }
camino = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
Expand Down
7 changes: 2 additions & 5 deletions crates/biome_grit_patterns/src/grit_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn new_file_owner(
/// that can use the Biome workspace.
#[derive(Clone, Debug)]
pub struct GritTargetFile {
pub path: PathBuf,
pub path: Utf8PathBuf,
pub parse: AnyParse,
}

Expand All @@ -323,9 +323,6 @@ impl GritTargetFile {
let parser = target_language.get_parser();
let parse = parser.parse_with_path(source, &path);

Self {
parse,
path: path.into(),
}
Self { parse, path }
}
}
8 changes: 6 additions & 2 deletions crates/biome_grit_patterns/src/grit_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ impl GritQuery {

let var_registry = VarRegistry::from_locations(&self.variable_locations);

let file_registry =
FileRegistry::new_from_paths(files.iter().map(|file| &file.path).collect());
// FIXME: Can be simplified when https://github.com/getgrit/gritql/pull/594/files is released.
let paths: Vec<PathBuf> = files
.iter()
.map(|file| file.path.clone().into_std_path_buf())
.collect();
let file_registry = FileRegistry::new_from_paths(paths.iter().collect());
let binding = FilePattern::Single(file_ptr);

let mut state = State::new(var_registry.into(), file_registry);
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ biome_js_factory = { workspace = true }
biome_js_semantic = { workspace = true }
biome_js_syntax = { workspace = true }
biome_package = { workspace = true }
biome_project_layout = { workspace = true }
biome_rowan = { workspace = true }
biome_string_case = { workspace = true }
biome_suppression = { workspace = true }
Expand Down
Loading

0 comments on commit 2ae40a7

Please sign in to comment.