-
-
Notifications
You must be signed in to change notification settings - Fork 515
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
feat(js_semantic): support jsxFactory
and jsxFragmentFactory
#3761
base: main
Are you sure you want to change the base?
Changes from all commits
09097db
9165264
cdb5016
bc0dcff
dab5f0d
c90102d
7e5cf47
4c9831b
0fde490
f5f7ab0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,16 @@ mod formatter; | |
|
||
use std::str::FromStr; | ||
|
||
use biome_console::markup; | ||
use biome_deserialize::StringSet; | ||
use biome_deserialize_macros::{Deserializable, Merge, Partial}; | ||
use biome_js_parser::{parse_module, JsParserOptions}; | ||
use biome_rowan::AstNode; | ||
use bpaf::Bpaf; | ||
pub use formatter::{ | ||
partial_javascript_formatter, JavascriptFormatter, PartialJavascriptFormatter, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde::{Deserialize, Deserializer, Serialize}; | ||
|
||
/// A set of options applied to the JavaScript files | ||
#[derive(Clone, Debug, Default, Deserialize, Eq, Partial, PartialEq, Serialize)] | ||
|
@@ -42,6 +45,20 @@ pub struct JavascriptConfiguration { | |
#[partial(bpaf(hide))] | ||
pub jsx_runtime: JsxRuntime, | ||
|
||
/// Indicates the name of the factory function used to create JSX elements. | ||
#[partial( | ||
bpaf(hide), | ||
serde(deserialize_with = "deserialize_optional_jsx_factory_from_string") | ||
)] | ||
pub jsx_factory: JsxFactory, | ||
|
||
/// Indicates the name of the factory function used to create JSX fragments. | ||
#[partial( | ||
bpaf(hide), | ||
serde(deserialize_with = "deserialize_optional_jsx_factory_from_string") | ||
)] | ||
pub jsx_fragment_factory: JsxFactory, | ||
|
||
#[partial(type, bpaf(external(partial_javascript_organize_imports), optional))] | ||
pub organize_imports: JavascriptOrganizeImports, | ||
} | ||
|
@@ -100,6 +117,93 @@ impl FromStr for JsxRuntime { | |
} | ||
} | ||
|
||
fn deserialize_optional_jsx_factory_from_string<'de, D>( | ||
deserializer: D, | ||
) -> Result<Option<JsxFactory>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
match parse_jsx_factory(&s) { | ||
Some(factory) => Ok(Some(factory)), | ||
None => Err(serde::de::Error::custom(format!( | ||
"expected valid identifier or qualified name, but received {s}" | ||
))), | ||
} | ||
} | ||
|
||
fn parse_jsx_factory(value: &str) -> Option<JsxFactory> { | ||
use biome_js_syntax::*; | ||
let syntax = parse_module(value, JsParserOptions::default()); | ||
let item = syntax.try_tree()?.items().into_iter().next()?; | ||
if let AnyJsModuleItem::AnyJsStatement(stmt) = item { | ||
let expr = JsExpressionStatement::cast_ref(stmt.syntax())? | ||
.expression() | ||
.ok()?; | ||
if let AnyJsExpression::JsStaticMemberExpression(member) = expr { | ||
let mut expr = member.object().ok(); | ||
while let Some(e) = expr { | ||
if let Some(ident) = JsIdentifierExpression::cast_ref(e.syntax()) { | ||
return Some(JsxFactory(ident.text().clone())); | ||
} else if let Some(member) = JsStaticMemberExpression::cast_ref(e.syntax()) { | ||
expr = member.object().ok(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} else if let AnyJsExpression::JsIdentifierExpression(ident) = expr { | ||
return Some(JsxFactory(ident.text().clone())); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
#[derive(Bpaf, Clone, Debug, Deserialize, Eq, Merge, PartialEq, Serialize)] | ||
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct JsxFactory(pub String); | ||
|
||
impl Default for JsxFactory { | ||
fn default() -> Self { | ||
Self("React".to_string()) | ||
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.
|
||
} | ||
} | ||
|
||
impl JsxFactory { | ||
pub fn into_string(self) -> String { | ||
self.0 | ||
} | ||
} | ||
|
||
impl biome_deserialize::Deserializable for JsxFactory { | ||
fn deserialize( | ||
value: &impl biome_deserialize::DeserializableValue, | ||
name: &str, | ||
diagnostics: &mut Vec<biome_deserialize::DeserializationDiagnostic>, | ||
) -> Option<Self> { | ||
let factory = biome_deserialize::Text::deserialize(value, name, diagnostics)?; | ||
parse_jsx_factory(factory.text()).or_else(|| { | ||
diagnostics.push(biome_deserialize::DeserializationDiagnostic::new( | ||
markup!( | ||
"Incorrect value, expected "<Emphasis>{"identifier"}</Emphasis>" or "<Emphasis>{"qualified name"}</Emphasis>", but received "<Emphasis>{format_args!("{}", factory.text())}</Emphasis>"." | ||
), | ||
).with_range(value.range())); | ||
None | ||
}) | ||
} | ||
} | ||
|
||
impl FromStr for JsxFactory { | ||
type Err = String; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let factory = parse_jsx_factory(s).ok_or_else(|| { | ||
format!("expected valid identifier or qualified name, but received {s}") | ||
})?; | ||
Ok(factory) | ||
} | ||
} | ||
|
||
/// Linter options specific to the JavaScript linter | ||
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)] | ||
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))] | ||
|
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.
I don't know if this is the right place to put, thinking of this is a quite general utility.