-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
53 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/forgetti/src/core/utils/get-hoc-definition-from-callee.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,57 @@ | ||
import type * as babel from '@babel/core'; | ||
import * as t from '@babel/types'; | ||
import type { ImportDefinition } from '../presets'; | ||
import type { StateContext } from '../types'; | ||
import unwrapNode from './unwrap-node'; | ||
|
||
function getHOCDefinitionFromPropName( | ||
definitions: ImportDefinition[], | ||
propName: string, | ||
): ImportDefinition | undefined { | ||
for (let i = 0, len = definitions.length; i < len; i++) { | ||
const def = definitions[i]; | ||
if (def.kind === 'default' && propName === 'default') { | ||
return def; | ||
} | ||
if (def.kind === 'named' && propName === def.name) { | ||
return def; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
export function getHOCDefinitionFromCallee( | ||
ctx: StateContext, | ||
path: babel.NodePath<t.CallExpression>, | ||
): ImportDefinition | undefined { | ||
const callee = path.node.callee; | ||
const id = unwrapNode(callee, t.isIdentifier); | ||
if (id) { | ||
const binding = path.scope.getBindingIdentifier(id.name); | ||
if (binding) { | ||
return ctx.registrations.hooks.identifiers.get(binding); | ||
} | ||
return undefined; | ||
} | ||
const memberExpr = unwrapNode(callee, t.isMemberExpression); | ||
if ( | ||
memberExpr && | ||
!memberExpr.computed && | ||
t.isIdentifier(memberExpr.property) | ||
) { | ||
const object = unwrapNode(memberExpr.object, t.isIdentifier); | ||
if (object) { | ||
const binding = path.scope.getBindingIdentifier(object.name); | ||
if (binding) { | ||
const definitions = ctx.registrations.hocs.namespaces.get(binding); | ||
if (definitions) { | ||
return getHOCDefinitionFromPropName( | ||
definitions, | ||
memberExpr.property.name, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
return undefined; | ||
} |
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