Skip to content

Commit 6f15c64

Browse files
authored
Merge pull request #43 from byondrnd/babel-debug-include-hooks
Babel debug include hooks
2 parents 1a8603b + 8e4742a commit 6f15c64

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.changeset/neat-feet-heal.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@byondxr/babel-plugin": patch
3+
"@byondxr/example-app": patch
4+
---
5+
6+
babel debug include hooks

packages/babel-plugin/src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export type Options = PluginPass & {
2828
export const isComponent = (str: string) => {
2929
return /^[\$_\d]*[A-Z]/.test(str)
3030
}
31+
export const isHookName = (str: string): boolean => {
32+
return /^use[0-9A-Z$_]/.test(str)
33+
}
3134

3235
export const isDomElement = (str: string) => {
3336
return /^[a-z]/.test(str)
@@ -149,6 +152,7 @@ const debugComponentTraverse = (p: NodePath, state: Options, component: string)
149152
if (innerComment) {
150153
if (p.isCallExpression()) {
151154
p.node.arguments.push(t.stringLiteral(component))
155+
p.node.arguments.push(t.stringLiteral(state.filename?.replace(`${state.cwd}/`, '') ?? ''))
152156
innerComment.value = REPLACE_COMMENT
153157
}
154158
} else {
@@ -158,6 +162,9 @@ const debugComponentTraverse = (p: NodePath, state: Options, component: string)
158162
if (trailingComment) {
159163
if (p.parentPath?.isCallExpression()) {
160164
p.parentPath.node.arguments.push(t.stringLiteral(component))
165+
p.parentPath.node.arguments.push(
166+
t.stringLiteral(state.filename?.replace(`${state.cwd}/`, '') ?? '')
167+
)
161168
trailingComment.value = REPLACE_COMMENT
162169
}
163170
}
@@ -240,9 +247,14 @@ const componentVisitor: PluginObj<Options> = {
240247

241248
if (arrowFunction) {
242249
if (t.isIdentifier(declarator.node.id)) {
243-
if (isComponent(declarator.node.id.name)) {
250+
if (isHookName(declarator.node.id.name)) {
251+
const hookName = declarator.node.id.name
252+
debugComponentTraverse(arrowFunction, state, hookName)
253+
} else if (isComponent(declarator.node.id.name)) {
244254
const componentName = declarator.node.id.name
255+
245256
debugComponentTraverse(arrowFunction, state, componentName)
257+
246258
const wrap = ({ importName, importSource }: Import) => {
247259
let actualImport = state.actualImports.get(importName)
248260
if (!actualImport) {

packages/example-app/src/app.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ A.displayName = 'A'
2020

2121
const useTest = (p?: any) => {}
2222

23+
const useA = () => {
24+
useTest(/*component*/)
25+
useTest('' /*component*/)
26+
}
2327
const App = () => {
2428
const [count, setCount] = useState(0)
2529

0 commit comments

Comments
 (0)