From 622474ea5bce54955d155b5ac9b00434a7cd3cb0 Mon Sep 17 00:00:00 2001 From: Rel1cx Date: Mon, 3 Mar 2025 11:41:35 +0800 Subject: [PATCH] fix: 'naming-convention/use-state' fails with multiple words, closes #960 --- .../src/rules/use-state.spec.ts | 5 +++++ .../src/rules/use-state.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.spec.ts b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.spec.ts index 49030ac2a..0ec84f8b2 100644 --- a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.spec.ts +++ b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.spec.ts @@ -134,5 +134,10 @@ ruleTester.run(RULE_NAME, rule, { return [count, setCount]; } `, + /* tsx */ `const [myCount, setMyCount] = useState(0);`, + /* tsx */ `const [fooBarBaz, setFooBarBaz] = useState({foo: "bbb", bar: "aaa", baz: "qqq"});`, + /* tsx */ `const [fooBarBaz, set_foo_bar_baz] = useState({foo: "bbb", bar: "aaa", baz: "qqq"});`, + /* tsx */ `const [foo_bar_baz, set_foo_bar_baz] = useState({foo: "bbb", bar: "aaa", baz: "qqq"});`, + /* tsx */ `const [FooBarBaz, setFooBarBaz] = useState({foo: "bbb", bar: "aaa", baz: "qqq"});`, ], }); diff --git a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.ts b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.ts index f5efa9b82..6db2ac4e0 100644 --- a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.ts +++ b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.ts @@ -53,7 +53,7 @@ export default createRule<[], MessageID>({ return; } const valueName = match(value) - .with({ type: T.Identifier }, (id) => id.name) + .with({ type: T.Identifier }, ({ name }) => snakeCase(name)) .with({ type: T.ObjectPattern }, ({ properties }) => { const values = properties.reduce((acc, prop) => { if (prop.type === T.Property && prop.key.type === T.Identifier) {