Skip to content

Commit f9f431d

Browse files
committed
add test cases for non-function values
1 parent 116260b commit f9f431d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

crates/oxc_linter/src/rules/react/jsx_handler_names.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ impl Rule for JsxHandlerNames {
304304
if !self.check_local_variables && !value_expr.is_member_expression() {
305305
return;
306306
}
307+
// For other expressions, use the whole content inside the braces as the handler name,
308+
// which will be marked as a bad handler name if the prop key is an event handler prop.
307309
let span = expression_container.span.shrink(1);
308310
(Some(normalize_handler_name(ctx.source_range(span))), span, false)
309311
}
@@ -613,6 +615,8 @@ fn test() {
613615
serde_json::json!([{ "checkLocalVariables": true, "ignoreComponentNames": ["MyLib*"] }]),
614616
),
615617
),
618+
("<TestComponent onChange={true} />", None), // ok if not checking local variables (the same behavior as eslint version)
619+
("<TestComponent onChange={'value'} />", None), // ok if not checking local variables (the same behavior as eslint version)
616620
];
617621

618622
let fail = vec![
@@ -689,6 +693,14 @@ fn test() {
689693
serde_json::json!([{ "checkLocalVariables": true, "ignoreComponentNames": ["MyLibrary*"] }]),
690694
),
691695
),
696+
(
697+
"<TestComponent onChange={true} />",
698+
Some(serde_json::json!([{ "checkLocalVariables": true }])),
699+
),
700+
(
701+
"<TestComponent onChange={'value'} />",
702+
Some(serde_json::json!([{ "checkLocalVariables": true }])),
703+
),
692704
];
693705

694706
Tester::new(JsxHandlerNames::NAME, JsxHandlerNames::PLUGIN, pass, fail).test_and_snapshot();

crates/oxc_linter/src/snapshots/react_jsx_handler_names.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,17 @@ source: crates/oxc_linter/src/tester.rs
181181
8</div>
182182
╰────
183183
help: Prop key for handleButton must begin with 'on'
184+
185+
eslint-plugin-react(jsx-handler-names): Invalid handler name: true
186+
╭─[jsx_handler_names.tsx:1:26]
187+
1<TestComponent onChange={true} />
188+
· ────
189+
╰────
190+
help: Handler function for onChange prop key must be a camelCase name beginning with 'handle' only
191+
192+
⚠ eslint-plugin-react(jsx-handler-names): Invalid handler name: 'value'
193+
╭─[jsx_handler_names.tsx:1:26]
194+
1 │ <TestComponent onChange={'value'} />
195+
· ───────
196+
╰────
197+
help: Handler function for onChange prop key must be a camelCase name beginning with 'handle' only

0 commit comments

Comments
 (0)