-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Gelio/develop
Release v2.0.0
- Loading branch information
Showing
13 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Node } from 'typescript'; | ||
|
||
import { FunctionNode, isFunctionNode } from './function-node'; | ||
import { findClosestAncestorNode } from './find-closest-ancestor-node'; | ||
|
||
export function findAncestorFunction(node: Node): FunctionNode | null { | ||
return findClosestAncestorNode(node, isFunctionNode); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/react-hooks-nesting-walker/find-closest-ancestor-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Node } from 'typescript'; | ||
|
||
import { TypeGuardPredicate } from './predicate'; | ||
|
||
/** | ||
* Finds a closest ancestor that matches a given predicate. | ||
* | ||
* Ensures type safety | ||
*/ | ||
export function findClosestAncestorNode<ParentNodeType extends Node = Node>( | ||
startingNode: Node, | ||
predicate: TypeGuardPredicate<Node, ParentNodeType>, | ||
): ParentNodeType | null { | ||
if (!startingNode.parent) { | ||
return null; | ||
} | ||
|
||
if (predicate(startingNode.parent)) { | ||
return startingNode.parent; | ||
} | ||
|
||
return findClosestAncestorNode(startingNode.parent, predicate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
FunctionDeclaration, | ||
FunctionExpression, | ||
ArrowFunction, | ||
Node, | ||
isFunctionDeclaration, | ||
isFunctionExpression, | ||
isArrowFunction, | ||
} from 'typescript'; | ||
|
||
export type FunctionNode = | ||
| FunctionDeclaration | ||
| FunctionExpression | ||
| ArrowFunction; | ||
|
||
const matchers = [isFunctionDeclaration, isFunctionExpression, isArrowFunction]; | ||
|
||
export function isFunctionNode(node: Node): node is FunctionNode { | ||
return matchers.some(matcher => matcher(node)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Predicate<T> = (t: T) => boolean; | ||
|
||
export type TypeGuardPredicate<T, U extends T> = (t: T) => t is U; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function MyComponentReturningHookCall() { | ||
// Using a hook inside of a returned value should not be a rule violation, | ||
// even if it looks silly. | ||
|
||
return <div ref={useRef()}>Hello world</div>; | ||
~~~~~~~~ [A hook should not appear after a return statement] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function MyComponent() { | ||
const [counter, setCounter] = useState(0); | ||
|
||
if (counter > 5) { | ||
return <div>Counter is over 5</div>; | ||
} | ||
|
||
useEffect(() => { | ||
~~~~~~~~~~~~~~~~~ | ||
console.log('Counter is', counter); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
}); | ||
~~~~ [A hook should not appear after a return statement] | ||
|
||
return ( | ||
<div> | ||
{counter} - | ||
<button onClick={() => setCounter(counter + 1)}>+1</button> | ||
</div> | ||
); | ||
} | ||
|
||
function useCustomHook(param: number) { | ||
if (param > 5) { | ||
return 'a'; | ||
} | ||
|
||
useEffect(() => { }); | ||
~~~~~~~~~~~~~~~~~~~~ [A hook should not appear after a return statement] | ||
} | ||
|
||
|
||
// ============================= | ||
// Do not report rule violations if the hook is called in the `return` statement | ||
|
||
function useMyHook() { | ||
return useState(true)[0]; | ||
} |