Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changeset/neat-feet-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@byondxr/babel-plugin": patch
"@byondxr/example-app": patch
---

babel debug include hooks
14 changes: 13 additions & 1 deletion packages/babel-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export type Options = PluginPass & {
export const isComponent = (str: string) => {
return /^[\$_\d]*[A-Z]/.test(str)
}
export const isHookName = (str: string): boolean => {
return /^use[0-9A-Z$_]/.test(str)
}

export const isDomElement = (str: string) => {
return /^[a-z]/.test(str)
Expand Down Expand Up @@ -149,6 +152,7 @@ const debugComponentTraverse = (p: NodePath, state: Options, component: string)
if (innerComment) {
if (p.isCallExpression()) {
p.node.arguments.push(t.stringLiteral(component))
p.node.arguments.push(t.stringLiteral(state.filename?.replace(`${state.cwd}/`, '') ?? ''))
innerComment.value = REPLACE_COMMENT
}
} else {
Expand All @@ -158,6 +162,9 @@ const debugComponentTraverse = (p: NodePath, state: Options, component: string)
if (trailingComment) {
if (p.parentPath?.isCallExpression()) {
p.parentPath.node.arguments.push(t.stringLiteral(component))
p.parentPath.node.arguments.push(
t.stringLiteral(state.filename?.replace(`${state.cwd}/`, '') ?? '')
)
trailingComment.value = REPLACE_COMMENT
}
}
Expand Down Expand Up @@ -240,9 +247,14 @@ const componentVisitor: PluginObj<Options> = {

if (arrowFunction) {
if (t.isIdentifier(declarator.node.id)) {
if (isComponent(declarator.node.id.name)) {
if (isHookName(declarator.node.id.name)) {
const hookName = declarator.node.id.name
debugComponentTraverse(arrowFunction, state, hookName)
} else if (isComponent(declarator.node.id.name)) {
const componentName = declarator.node.id.name

debugComponentTraverse(arrowFunction, state, componentName)

const wrap = ({ importName, importSource }: Import) => {
let actualImport = state.actualImports.get(importName)
if (!actualImport) {
Expand Down
4 changes: 4 additions & 0 deletions packages/example-app/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ A.displayName = 'A'

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

const useA = () => {
useTest(/*component*/)
useTest('' /*component*/)
}
const App = () => {
const [count, setCount] = useState(0)

Expand Down