Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[eslint-plugin-react-hooks] update naming rules to disallow _component
  • Loading branch information
kassens committed Aug 31, 2022
commit f95ad034ac82cb26615951155650d4b97eb0e5fc
6 changes: 6 additions & 0 deletions packages/eslint-plugin-react-hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Next Release

* **New Violations:** Component names now need to start with an uppercase letter. `_Button` or `_component` are no longer valid. ([@kassens](https://github.com/kassens)) in [#25162](https://github.com/facebook/react/pull/25162)

## 4.6.0

## 4.5.0

* Fix false positive error with large number of branches. ([@scyron6](https://github.com/scyron6) in [#24287](https://github.com/facebook/react/pull/24287))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,6 @@ const tests = {
const [myState, setMyState] = useState(null);
}
`,
`
// Valid, but should be invalid. '_useHook' is currently recognized as a component.
function Component(props) {
if (cond) {
_useHook();
}
}
function _useHook() {
useState(null);
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -652,6 +641,21 @@ const tests = {
functionError('useHookInsideNormalFunction', 'normalFunctionWithHook'),
],
},
{
code: `
// These are neither functions nor hooks.
function _normalFunctionWithHook() {
useHookInsideNormalFunction();
}
function _useNotAHook() {
useHookInsideNormalFunction();
}
`,
errors: [
functionError('useHookInsideNormalFunction', '_normalFunctionWithHook'),
functionError('useHookInsideNormalFunction', '_useNotAHook'),
],
},
{
code: `
// Invalid because it's dangerous and might not warn otherwise.
Expand Down
11 changes: 3 additions & 8 deletions packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

function isHookName(s) {
return /^use[A-Z0-9].*$/.test(s);
return /^use[A-Z0-9]/.test(s);
}

/**
Expand All @@ -42,16 +42,11 @@ function isHook(node) {

/**
* Checks if the node is a React component name. React component names must
* always start with a non-lowercase letter. So `MyComponent` or `_MyComponent`
* are valid component names for instance.
* always start with an uppercase letter.
*/

function isComponentName(node) {
if (node.type === 'Identifier') {
return !/^[a-z]/.test(node.name);
} else {
return false;
}
return node.type === 'Identifier' && /^[A-Z]/.test(node.name);
}

function isReactFunction(node, functionName) {
Expand Down