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

test(format/grit): add tests for grit formatter #3937

Merged
merged 24 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .github/workflows/repository_dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: Build WASM module for the web
run: wasm-pack build --out-dir ../../packages/@biomejs/wasm-web --target web --profiling --scope biomejs crates/biome_wasm --features experimental-html
run: wasm-pack build --out-dir ../../packages/@biomejs/wasm-web --target web --profiling --scope biomejs crates/biome_wasm --features experimental-html, experimental-grit

# https://github.com/actions/cache/issues/342
- name: Clear old wasm-pack cache
Expand Down
11 changes: 11 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 crates/biome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ tokio = { workspace = true, features = ["io-util"] }

[features]
docgen = ["bpaf/docgen"]
experimental-grit = ["biome_service/experimental-grit"]
experimental-html = ["biome_service/experimental-html"]

[lints]
Expand Down
8 changes: 7 additions & 1 deletion crates/biome_formatter/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,15 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f
return;
};

let options = HtmlFormatOptions::default();
let language = language::HtmlTestFormatLanguage::default();

let snapshot = SpecSnapshot::new(test_file, test_directory, language, ());
let snapshot = SpecSnapshot::new(
test_file,
test_directory,
language,
HtmlFormatLanguage::new(options),
);

snapshot.test()
}
Expand Down
13 changes: 10 additions & 3 deletions crates/biome_grit_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ biome_grit_syntax = { workspace = true }
biome_rowan = { workspace = true }

[dev-dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

biome_configuration = { path = "../biome_configuration" }
biome_formatter_test = { path = "../biome_formatter_test" }
biome_grit_factory = { path = "../biome_grit_factory" }
biome_grit_parser = { path = "../biome_grit_parser" }
biome_parser = { path = "../biome_parser" }
biome_service = { path = "../biome_service" }
countme = { workspace = true, features = ["enable"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tests_macros = { path = "../tests_macros" }
# cargo-workspaces metadata
[package.metadata.workspaces]
independent = true
Expand Down
24 changes: 20 additions & 4 deletions crates/biome_grit_formatter/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments};
use biome_formatter::{
CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding,
LineWidth, QuoteStyle, TransformSourceMap,
AttributePosition, CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth,
LineEnding, LineWidth, QuoteStyle, TransformSourceMap,
};
use biome_grit_syntax::GritLanguage;
use std::fmt::Display;
use std::rc::Rc;

#[allow(dead_code)]
Expand Down Expand Up @@ -50,14 +51,14 @@ impl CstFormatContext for GritFormatContext {
}
}

#[derive(Debug, Default, Clone, PartialEq)]

#[derive(Debug, Default, Clone)]
pub struct GritFormatOptions {
indent_style: IndentStyle,
indent_width: IndentWidth,
line_ending: LineEnding,
line_width: LineWidth,
quote_style: QuoteStyle,
attribute_position: AttributePosition,
}

impl GritFormatOptions {
Expand All @@ -68,6 +69,7 @@ impl GritFormatOptions {
line_ending: LineEnding::default(),
line_width: LineWidth::default(),
quote_style: QuoteStyle::default(),
attribute_position: AttributePosition::default(),
}
}
pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self {
Expand Down Expand Up @@ -118,6 +120,20 @@ impl GritFormatOptions {
pub fn quote_style(&self) -> QuoteStyle {
self.quote_style
}

pub fn attribute_position(&self) -> AttributePosition {
self.attribute_position
}
}

impl Display for GritFormatOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Indent style: {}", self.indent_style)?;
writeln!(f, "Indent width: {}", self.indent_width.value())?;
writeln!(f, "Line ending: {}", self.line_ending)?;
writeln!(f, "Line width: {}", self.line_width.value())?;
writeln!(f, "Attribute Position: {}", self.attribute_position)
}
}

impl FormatOptions for GritFormatOptions {
Expand Down
24 changes: 24 additions & 0 deletions crates/biome_grit_formatter/tests/language.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use biome_formatter_test::TestFormatLanguage;
use biome_grit_formatter::{context::GritFormatContext, GritFormatLanguage};
use biome_grit_syntax::GritLanguage;

#[derive(Default)]
pub struct GritTestFormatLanguage;

impl TestFormatLanguage for GritTestFormatLanguage {
type ServiceLanguage = GritLanguage;
type Context = GritFormatContext;
type FormatLanguage = GritFormatLanguage;

fn parse(&self, _text: &str) -> biome_parser::AnyParse {
todo!()
}

fn to_format_language(
&self,
_settings: &biome_service::settings::Settings,
_file_source: &biome_service::workspace::DocumentFileSource,
) -> Self::FormatLanguage {
todo!()
}
}
27 changes: 27 additions & 0 deletions crates/biome_grit_formatter/tests/spec_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile};
use biome_grit_formatter::{context::GritFormatOptions, GritFormatLanguage};
use std::path::Path;

mod language {
include!("language.rs");
}

pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _file_type: &str) {
let root_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/specs/"));

let Some(test_file) = SpecTestFile::try_from_file(spec_input_file, root_path, None) else {
panic!("Failed to set up snapshot test");
};

let options = GritFormatOptions::default();
let language = language::GritTestFormatLanguage;

let snapshot = SpecSnapshot::new(
test_file,
test_directory,
language,
GritFormatLanguage::new(options),
);

snapshot.test()
}
8 changes: 8 additions & 0 deletions crates/biome_grit_formatter/tests/spec_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod spec_test;

mod formatter {

mod grit_module {
tests_macros::gen_tests! {"tests/specs/grit/**/*.grit", crate::spec_test::run, ""}
}
}
4 changes: 4 additions & 0 deletions crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`function $name ($args) { $body }` as $func where {
$func => `const $name = ($args) => { $body }`,
$args <: contains `apple` => `mango`
}
10 changes: 9 additions & 1 deletion crates/biome_grit_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod token_source;

use biome_grit_factory::GritSyntaxFactory;
use biome_grit_syntax::{GritLanguage, GritRoot, GritSyntaxNode};
use biome_parser::diagnostic::ParseDiagnostic;
use biome_parser::tree_sink::LosslessTreeSink;
use biome_parser::{diagnostic::ParseDiagnostic, AnyParse};
use biome_rowan::{AstNode, NodeCache};
use parser::{parse_root, GritParser};

Expand Down Expand Up @@ -100,3 +100,11 @@ impl GritParse {
GritRoot::unwrap_cast(self.syntax())
}
}

impl From<GritParse> for AnyParse {
fn from(parse: GritParse) -> Self {
let root = parse.syntax();
let diagnostics = parse.into_diagnostics();
Self::new(root.as_send().unwrap(), diagnostics)
}
}
57 changes: 57 additions & 0 deletions crates/biome_grit_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use biome_rowan::FileSourceError;
use std::{ffi::OsStr, path::Path};
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(
Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct GritFileSource {
#[allow(unused)]
variant: GritVariant,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(
Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize,
)]
enum GritVariant {
#[default]
Standard,
}

impl GritFileSource {
pub fn grit() -> Self {
Self {
variant: GritVariant::Standard,
}
}

/// Try to return the HTML file source corresponding to this file name from well-known files
pub fn try_from_well_known(_: &Path) -> Result<Self, FileSourceError> {
// TODO: to be implemented
Err(FileSourceError::UnknownFileName)
}

pub fn try_from_extension(extension: &OsStr) -> Result<Self, FileSourceError> {
match extension.as_encoded_bytes() {
b"grit" => Ok(Self::grit()),
_ => Err(FileSourceError::UnknownExtension),
}
}
}

impl TryFrom<&Path> for GritFileSource {
type Error = FileSourceError;

fn try_from(path: &Path) -> Result<Self, Self::Error> {
if let Ok(file_source) = Self::try_from_well_known(path) {
return Ok(file_source);
}

let Some(extension) = path.extension() else {
return Err(FileSourceError::MissingFileExtension);
};
// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Self::try_from_extension(&extension.to_ascii_lowercase())
}
}
1 change: 1 addition & 0 deletions crates/biome_grit_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#[macro_use]
mod generated;
pub mod file_source;
mod syntax_ext;
mod syntax_node;

Expand Down
5 changes: 5 additions & 0 deletions crates/biome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ biome_graphql_analyze = { workspace = true }
biome_graphql_formatter = { workspace = true }
biome_graphql_parser = { workspace = true }
biome_graphql_syntax = { workspace = true }
biome_grit_formatter = { workspace = true }
biome_grit_parser = { workspace = true }
biome_grit_patterns = { workspace = true }
biome_grit_syntax = { workspace = true }
biome_html_formatter = { workspace = true }
biome_html_parser = { workspace = true }
biome_html_syntax = { workspace = true }
Expand Down Expand Up @@ -67,7 +70,9 @@ smallvec = { workspace = true, features = ["serde"] }
tracing = { workspace = true, features = ["attributes", "log"] }

[features]
experimental-grit = []
experimental-html = []

schema = [
"dep:schemars",
"biome_js_analyze/schema",
Expand Down
Loading
Loading