-
-
Notifications
You must be signed in to change notification settings - Fork 724
fix(linter): Fix the react/jsx-fragments rule config to take a string argument
#16175
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
base: main
Are you sure you want to change the base?
Conversation
The original rule has a config option in the format of `["error", "element"]` or `["error", "syntax"]`. We required a config object, which is technically incorrect. This fixes that in the docs, but we still need to fix the code to accept a string argument instead of an object. I guess we have to make this backward-compatible and accept either?
And add tests accordingly.
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
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.
Pull request overview
This PR fixes the react/jsx-fragments rule configuration to accept string arguments (e.g., ["syntax"] or ["element"]) for compatibility with the original ESLint rule, while maintaining backward compatibility with the previously-implemented object syntax (e.g., [{"mode": "element"}]).
Key Changes
- Modified configuration parsing to support both string-based and object-based argument formats
- Updated diagnostic messages and help text with improved formatting (backticks and periods)
- Added comprehensive test cases for both configuration formats
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/oxc_linter/src/rules/react/jsx_fragments.rs | Refactored configuration parsing to accept both string and object arguments; improved diagnostic message formatting; added test cases for both configuration formats |
| crates/oxc_linter/src/snapshots/react_jsx_fragments.snap | Updated test snapshots to reflect improved diagnostic message formatting with backticks and periods |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
CodSpeed Performance ReportMerging #16175 will not alter performanceComparing Summary
Footnotes
|
| pub struct JsxFragments { | ||
| /// `syntax` mode: | ||
| /// | ||
| mode: FragmentMode, | ||
| } |
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.
you can support both by doing an enum, something like this i think
#[schemars(untagged)]
pub enum JsxFragments {
StringFragmentMode(FragmentMode);
ObjectFragmentMode({ mode: FragmentMode });
}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.
This almost works, but not quite, as it breaks the self.mode throughout the file. I can do it like this and it compiles, but unwrapping in from_configuration apparently panics in some cases.
#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(untagged)]
pub enum JsxFragments {
Mode(FragmentMode),
Object { mode: FragmentMode },
}
impl Default for JsxFragments {
fn default() -> Self {
JsxFragments::Mode(FragmentMode::Syntax)
}
}
impl JsxFragments {
fn mode(&self) -> FragmentMode {
match self {
JsxFragments::Mode(m) => *m,
JsxFragments::Object { mode } => *mode,
}
}
}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.
unwrap_or_default in from_configuration fixes the panic! Although I have apparently broken the behavior in the tests somehow. 🤔
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.
ah hm didn't consider that it makes the code weird, might need a helper function to convert the mode from the enum to the fragment mode
edit: oh i see that's what you did
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.
hm unwrap would be preferable i think to enforce that the config they pass in is correct. panicking is a bit harsh but that's a separate problem
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've pushed up a commit to use this pattern, although I'm not really sure it's particularly better than the previous version. I'll probably let cam decide on whatever is preferred.
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.
imo it's better because it codifies the config in the type rather than some adhoc deserialization code
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.
also you can get config doc generation hopefully that describes these two options if you put this enum in the config value
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.
Unfortunately we'll need to make it possible to serialize a value of this type for the website to generate it, right now it can't handle an enum that is more than just simple types.
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.
DefaultRuleConfig definitely makes it better: 65f8344
The original rule has a config option in the format of
["error", "element"]or["error", "syntax"]. We required a config object like["error", { "mode": "element" }], which is technically incompatible for existing configs that get migrated to oxlint.This fixes that in the generated docs, and also makes it work correctly in the oxlint config. We retain compatibility with the object syntax in case anyone has set up oxlint with it already, and I've added tests to ensure both formats of the config option work.
Generated docs now: