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 all 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", features = ["experimental-grit"] }
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
49 changes: 34 additions & 15 deletions crates/biome_grit_formatter/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments};
use biome_formatter::printer::PrinterOptions;
use biome_formatter::{
CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding,
LineWidth, QuoteStyle, TransformSourceMap,
AttributePosition, BracketSpacing, 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)]
#[derive(Debug, Clone)]
pub struct GritFormatContext {
options: GritFormatOptions,
comments: Rc<GritComments>,
source_map: Option<TransformSourceMap>,
}

impl GritFormatContext {
pub fn new(comments: GritComments) -> Self {
pub fn new(options: GritFormatOptions, comments: GritComments) -> Self {
Self {
options,
comments: Rc::new(comments),
source_map: None,
}
Expand All @@ -31,11 +35,11 @@ impl FormatContext for GritFormatContext {
type Options = GritFormatOptions;

fn options(&self) -> &Self::Options {
todo!()
&self.options
}

fn source_map(&self) -> Option<&TransformSourceMap> {
todo!()
self.source_map.as_ref()
}
}
impl CstFormatContext for GritFormatContext {
Expand All @@ -46,18 +50,18 @@ impl CstFormatContext for GritFormatContext {
type CommentRule = FormatGritLeadingComment;

fn comments(&self) -> &biome_formatter::comments::Comments<Self::Language> {
todo!()
&self.comments
}
}

#[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 +72,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,34 +123,48 @@ 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 {
fn indent_style(&self) -> IndentStyle {
todo!()
self.indent_style
}

fn indent_width(&self) -> IndentWidth {
todo!()
self.indent_width
}

fn line_width(&self) -> LineWidth {
todo!()
self.line_width
}

fn line_ending(&self) -> LineEnding {
todo!()
self.line_ending
}

fn attribute_position(&self) -> biome_formatter::AttributePosition {
todo!()
self.attribute_position
}

fn bracket_spacing(&self) -> biome_formatter::BracketSpacing {
todo!()
BracketSpacing::default()
}

fn as_print_options(&self) -> biome_formatter::prelude::PrinterOptions {
todo!()
PrinterOptions::from(self)
}
}
13 changes: 9 additions & 4 deletions crates/biome_grit_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ mod grit;
mod prelude;

use biome_formatter::{
comments::Comments,
prelude::*,
trivia::{format_dangling_comments, format_leading_comments, format_trailing_comments},
write, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted,
};
use biome_grit_syntax::{GritLanguage, GritSyntaxNode};
use comments::GritCommentStyle;

pub(crate) use crate::context::GritFormatContext;

Expand Down Expand Up @@ -120,15 +122,18 @@ impl FormatLanguage for GritFormatLanguage {
}

fn options(&self) -> &<Self::Context as biome_formatter::FormatContext>::Options {
todo!()
&self.options
}

fn create_context(
self,
_root: &biome_rowan::SyntaxNode<Self::SyntaxLanguage>,
_source_map: Option<biome_formatter::TransformSourceMap>,
root: &biome_rowan::SyntaxNode<Self::SyntaxLanguage>,
source_map: Option<biome_formatter::TransformSourceMap>,
) -> Self::Context {
todo!()
let comments: Comments<GritLanguage> =
Comments::from_node(root, &GritCommentStyle, source_map.as_ref());

GritFormatContext::new(self.options, comments).with_source_map(source_map)
}
}

Expand Down
25 changes: 25 additions & 0 deletions crates/biome_grit_formatter/tests/language.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use biome_formatter_test::TestFormatLanguage;
use biome_grit_formatter::{context::GritFormatContext, GritFormatLanguage};
use biome_grit_parser::parse_grit;
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 {
parse_grit(text).into()
}

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, ""}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file(body = contains `console.$method` => `println`)
33 changes: 33 additions & 0 deletions crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: grit/file_node.grit
---
# Input

```grit
file(body = contains `console.$method` => `println`)
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
-----

```grit
file(body = contains `console.$method` => `println`)```



## Unimplemented nodes/tokens

"file(body = contains `console.$method` => `println`)" => 0..52
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)
}
}
Loading
Loading