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
Implement loading biome.config.js
#1825
Changes from 4 commits
236544f
cfea26e
1aceec9
8368907
b94a3a5
87a961c
ae08477
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
use crate::configs::{ | ||
CONFIG_DISABLED_FORMATTER, CONFIG_FILE_SIZE_LIMIT, CONFIG_FORMAT, | ||
CONFIG_FORMATTER_AND_FILES_IGNORE, CONFIG_FORMATTER_IGNORED_DIRECTORIES, | ||
CONFIG_FORMATTER_IGNORED_FILES, CONFIG_FORMAT_JSONC, CONFIG_ISSUE_3175_1, CONFIG_ISSUE_3175_2, | ||
CONFIG_FORMATTER_IGNORED_FILES, CONFIG_FORMAT_JS, CONFIG_FORMAT_JSONC, CONFIG_ISSUE_3175_1, | ||
CONFIG_ISSUE_3175_2, | ||
}; | ||
use crate::snap_test::{assert_file_contents, markup_to_string, SnapshotPayload}; | ||
use crate::{ | ||
|
@@ -1177,6 +1178,37 @@ fn format_with_configuration() { | |
)); | ||
} | ||
|
||
#[test] | ||
fn format_with_js_configuration() { | ||
let mut console = BufferConsole::default(); | ||
let mut fs = MemoryFileSystem::default(); | ||
fs.set_working_directory("/"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boa's default module loader didn't like it if there's no working directory, so I had to add one to the memory filesystem. Note the main module is currently loaded from a string that is read from disk by our config finder, so I expect this might need a little bit more before loading external modules will work through the memory file system. |
||
|
||
let file_path = Path::new("/biome.config.js"); | ||
fs.insert(file_path.into(), CONFIG_FORMAT_JS.as_bytes()); | ||
|
||
let file_path = Path::new("/file.js"); | ||
fs.insert(file_path.into(), CUSTOM_FORMAT_BEFORE.as_bytes()); | ||
|
||
let result = run_cli( | ||
DynRef::Borrowed(&mut fs), | ||
&mut console, | ||
Args::from(&["format", "/file.js", "--write"]), | ||
); | ||
|
||
assert!(result.is_ok(), "run_cli returned {result:?}"); | ||
|
||
assert_file_contents(&fs, file_path, CUSTOM_FORMAT_AFTER); | ||
|
||
assert_cli_snapshot(SnapshotPayload::new( | ||
module_path!(), | ||
"format_with_js_configuration", | ||
fs, | ||
console, | ||
result, | ||
)); | ||
} | ||
|
||
#[test] | ||
fn format_is_disabled() { | ||
let mut fs = MemoryFileSystem::default(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
source: crates/biome_cli/tests/snap_test.rs | ||
expression: content | ||
--- | ||
## `/biome.config.js` | ||
|
||
```js | ||
export const config = { | ||
formatter: { | ||
enabled: true, | ||
lineWidth: 160, | ||
indentStyle: "space", | ||
indentSize: 6, | ||
} | ||
}; | ||
|
||
``` | ||
|
||
## `/file.js` | ||
|
||
```js | ||
function f() { | ||
return { something }; | ||
} | ||
|
||
``` | ||
|
||
# Emitted Messages | ||
|
||
```block | ||
Formatted 1 file(s) in <TIME> | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! Implementation of [DeserializableValue] for the JS engine. | ||
use crate::{DeserializationDiagnostic, Deserialized}; | ||
use biome_console::markup; | ||
use biome_diagnostics::Error; | ||
use boa_engine::{Context, JsValue}; | ||
use serde::de::DeserializeOwned; | ||
|
||
/// Attempts to interpret a value from the JS runtime and deserialize it following the semantics | ||
/// defined by JSON. As such, JS types that cannot be serialized to JSON, such as functions or | ||
/// Promises are not supported by this deserializer. | ||
/// | ||
/// The data structures that need to be deserialized have to implement the [Deserializable] trait. | ||
/// For most data structures, this can be achieved using the | ||
/// [biome_deserialize_macros::Deserializable] derive. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note this comment is not entirely correct, since the function uses Serde traits for now, instead of our |
||
/// | ||
/// `name` corresponds to the name used in a diagnostic to designate the deserialized value. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ``` | ||
/// use biome_deserialize::js::deserialize_from_js_value; | ||
/// use biome_deserialize_macros::Deserializable; | ||
/// use boa_engine::{Context, Source}; | ||
/// | ||
/// #[derive(Debug, Default, Deserializable, Eq, PartialEq)] | ||
/// struct NewConfiguration { | ||
/// lorem: String | ||
/// } | ||
/// | ||
/// let source = r#"{ lorem: "ipsum" }"#; | ||
/// | ||
/// let mut context = Context::default(); | ||
/// let value = context.eval(Source::from_bytes(source)).unwrap(); | ||
/// | ||
/// let deserialized = deserialize_from_js_value::<NewConfiguration>(&mut context, value); | ||
/// assert!(!deserialized.has_errors()); | ||
/// assert_eq!(deserialized.into_deserialized().unwrap(), NewConfiguration { lorem: "ipsum".to_string() }); | ||
/// ``` | ||
pub fn deserialize_from_js_value<Output: DeserializeOwned>( | ||
context: &mut Context, | ||
value: JsValue, | ||
) -> Deserialized<Output> { | ||
let mut diagnostics: Vec<DeserializationDiagnostic> = Vec::new(); | ||
|
||
// TODO: We probably want to improve our diagnostics somewhat. | ||
let deserialized = match value.to_json(context) { | ||
Ok(json) => match serde_json::from_value(json) { | ||
Ok(value) => Some(value), | ||
Err(err) => { | ||
diagnostics.push(DeserializationDiagnostic::new( | ||
markup!("Value could not be exported to JSON: "{err.to_string()}), | ||
)); | ||
None | ||
} | ||
}, | ||
Err(err) => { | ||
diagnostics.push(DeserializationDiagnostic::new( | ||
markup!("Value could not be parsed as JSON: "{err.to_string()}), | ||
)); | ||
None | ||
} | ||
}; | ||
|
||
Deserialized { | ||
diagnostics: diagnostics.into_iter().map(Error::from).collect::<Vec<_>>(), | ||
deserialized, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
//! | ||
mod diagnostics; | ||
mod impls; | ||
pub mod js; | ||
pub mod json; | ||
mod merge; | ||
pub mod string_set; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the distraction, just renamed this to follow Rust's naming conventions: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv