Skip to content

Commit 15d69dc

Browse files
committed
feat(linter): implement react/display-name rule (#18426)
This is a fixed up version of #12084 with merge conflicts resolved.
1 parent c3036f7 commit 15d69dc

File tree

13 files changed

+3107
-333
lines changed

13 files changed

+3107
-333
lines changed

apps/oxlint/src/snapshots/_-c fixtures__print_config__ban_rules__eslintrc.json -A all -D eqeqeq --print-config@oxlint.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ working directory:
3232
"react": {
3333
"formComponents": [],
3434
"linkComponents": [],
35-
"version": null
35+
"version": null,
36+
"componentWrapperFunctions": []
3637
},
3738
"jsdoc": {
3839
"ignorePrivate": false,

apps/oxlint/src/snapshots/fixtures_-A all --print-config@oxlint.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ working directory: fixtures
2525
"react": {
2626
"formComponents": [],
2727
"linkComponents": [],
28-
"version": null
28+
"version": null,
29+
"componentWrapperFunctions": []
2930
},
3031
"jsdoc": {
3132
"ignorePrivate": false,

crates/oxc_linter/src/config/settings/react.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ pub struct ReactPluginSettings {
8080
#[validate(regex = "REACT_VERSION_REGEX")]
8181
#[schemars(with = "Option<String>")]
8282
pub version: Option<ReactVersion>,
83+
84+
/// Functions that wrap React components and should be treated as HOCs.
85+
///
86+
/// Example:
87+
///
88+
/// ```jsonc
89+
/// {
90+
/// "settings": {
91+
/// "react": {
92+
/// "componentWrapperFunctions": ["observer", "withRouter"]
93+
/// }
94+
/// }
95+
/// }
96+
/// ```
97+
#[serde(default)]
98+
#[serde(rename = "componentWrapperFunctions")]
99+
component_wrapper_functions: Vec<CompactStr>,
83100
// TODO: More properties should be added
84101
}
85102

@@ -92,6 +109,10 @@ impl ReactPluginSettings {
92109
pub fn get_link_component_attrs(&self, name: &str) -> Option<ComponentAttrs<'_>> {
93110
get_component_attrs_by_name(&self.link_components, name)
94111
}
112+
113+
pub fn is_component_wrapper_function(&self, name: &str) -> bool {
114+
self.component_wrapper_functions.iter().any(|func| func == name)
115+
}
95116
}
96117

97118
// Deserialize helper types

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,11 @@ impl RuleRunner for crate::rules::react::checked_requires_onchange_or_readonly::
21742174
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
21752175
}
21762176

2177+
impl RuleRunner for crate::rules::react::display_name::DisplayName {
2178+
const NODE_TYPES: Option<&AstTypesBitset> = None;
2179+
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::RunOnce;
2180+
}
2181+
21772182
impl RuleRunner for crate::rules::react::exhaustive_deps::ExhaustiveDeps {
21782183
const NODE_TYPES: Option<&AstTypesBitset> =
21792184
Some(&AstTypesBitset::from_types(&[AstType::CallExpression]));

crates/oxc_linter/src/generated/rules_enum.rs

Lines changed: 343 additions & 321 deletions
Large diffs are not rendered by default.

crates/oxc_linter/src/rules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ pub(crate) mod jest {
356356
pub(crate) mod react {
357357
pub mod button_has_type;
358358
pub mod checked_requires_onchange_or_readonly;
359+
pub mod display_name;
359360
pub mod exhaustive_deps;
360361
pub mod forbid_dom_props;
361362
pub mod forbid_elements;

0 commit comments

Comments
 (0)