@@ -15,63 +15,47 @@ import { isOneOf } from "./is";
1515import type { TSESTreeFunction } from "./types" ;
1616
1717export function getFunctionIdentifier ( node : TSESTree . Expression | TSESTreeFunction ) : O . Option < TSESTree . Identifier > {
18- // function MaybeComponent() {}
19- // const whatever = function MaybeComponent() {};
20- if ( "id" in node && node . id ) return O . some ( node . id ) ;
21- // const MaybeComponent = () => {};
22- if (
23- node . parent . type === AST_NODE_TYPES . VariableDeclarator
24- && node . parent . init === node
25- && node . parent . id . type === AST_NODE_TYPES . Identifier
26- ) {
27- return O . some ( node . parent . id ) ;
28- }
29- // MaybeComponent = () => {};
30- if (
31- node . parent . type === AST_NODE_TYPES . AssignmentExpression
32- && node . parent . right === node
33- && node . parent . operator === "="
34- && node . parent . left . type === AST_NODE_TYPES . Identifier
35- ) {
36- return O . some ( node . parent . left ) ;
37- }
38- // {MaybeComponent: () => {}}
39- // {MaybeComponent() {}}
40- if (
41- node . parent . type === AST_NODE_TYPES . Property
42- && node . parent . value === node
43- && ! node . parent . computed
44- && node . parent . key . type === AST_NODE_TYPES . Identifier
45- ) {
46- return O . some ( node . parent . key ) ;
47- }
48-
49- // class {MaybeComponent = () => {}}
50- // class {MaybeComponent() {}}
51- if (
52- isOneOf ( [ AST_NODE_TYPES . MethodDefinition , AST_NODE_TYPES . PropertyDefinition ] ) ( node . parent )
53- && node . parent . value === node
54- && node . parent . key . type === AST_NODE_TYPES . Identifier
55- ) {
56- return O . some ( node . parent . key ) ;
57- }
58- // Follow spec convention for `IsAnonymousFunctionDefinition()` usage.
59- //
60- // const {MaybeComponent = () => {}} = {};
61- // ({MaybeComponent = () => {}} = {});
62- if (
63- node . parent . type === AST_NODE_TYPES . AssignmentPattern
64- && node . parent . right === node
65- && node . parent . left . type === AST_NODE_TYPES . Identifier
66- ) {
67- return O . some ( node . parent . left ) ;
68- }
69-
70- if (
71- isOneOf ( [ AST_NODE_TYPES . TSAsExpression , AST_NODE_TYPES . TSSatisfiesExpression ] ) ( node . parent )
72- && node . parent . expression === node
73- ) {
74- return getFunctionIdentifier ( node . parent ) ;
18+ switch ( true ) {
19+ // function MaybeComponent() {}
20+ case "id" in node
21+ && ! ! node . id :
22+ return O . some ( node . id ) ;
23+ // const whatever = function MaybeComponent() {};
24+ case node . parent . type === AST_NODE_TYPES . VariableDeclarator
25+ && node . parent . init === node
26+ && node . parent . id . type === AST_NODE_TYPES . Identifier :
27+ return O . some ( node . parent . id ) ;
28+ // MaybeComponent = () => {};
29+ case node . parent . type === AST_NODE_TYPES . AssignmentExpression
30+ && node . parent . right === node
31+ && node . parent . operator === "="
32+ && node . parent . left . type === AST_NODE_TYPES . Identifier :
33+ return O . some ( node . parent . left ) ;
34+ // {MaybeComponent: () => {}}
35+ // {MaybeComponent() {}}
36+ case node . parent . type === AST_NODE_TYPES . Property
37+ && node . parent . value === node
38+ && ! node . parent . computed
39+ && node . parent . key . type === AST_NODE_TYPES . Identifier :
40+ return O . some ( node . parent . key ) ;
41+ // class {MaybeComponent = () => {}}
42+ // class {MaybeComponent() {}}
43+ case isOneOf ( [ AST_NODE_TYPES . MethodDefinition , AST_NODE_TYPES . PropertyDefinition ] ) ( node . parent )
44+ && node . parent . value === node
45+ && node . parent . key . type === AST_NODE_TYPES . Identifier :
46+ return O . some ( node . parent . key ) ;
47+ // Follow spec convention for `IsAnonymousFunctionDefinition()` usage.
48+ //
49+ // const {MaybeComponent = () => {}} = {};
50+ // ({MaybeComponent = () => {}} = {});
51+ case node . parent . type === AST_NODE_TYPES . AssignmentPattern
52+ && node . parent . right === node
53+ && node . parent . left . type === AST_NODE_TYPES . Identifier :
54+ return O . some ( node . parent . left ) ;
55+ // const MaybeComponent = (() => {}) as FunctionComponent;
56+ // const MaybeComponent = (() => {}) satisfies FunctionComponent;
57+ case isOneOf ( [ AST_NODE_TYPES . TSAsExpression , AST_NODE_TYPES . TSSatisfiesExpression ] ) ( node . parent ) :
58+ return getFunctionIdentifier ( node . parent ) ;
7559 }
7660 return O . none ( ) ;
7761}
0 commit comments