Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin does not inject the __collectModule hook into ArrowFunction declarations. #3

Open
DanDroryAu opened this issue Jan 7, 2025 · 1 comment

Comments

@DanDroryAu
Copy link

If a component is defined like so:

// MyComponent.tsx
const MyComponent = () => {
  return (<div>hello, world!</div>);
}

export default MyComponent;

The plugin doesn't inject the __collectModule hook.

This is because the ExportDefaultDeclaration ast traverse has a small missing step for arrowFunctionExpressions.

I've provided some code that I think can resolve the issue. Let me know if you want me to make a forkPR.

ExportDefaultDeclaration(path) {
    const declaration = path.node.declaration;

    // Insert hook in `export default function() { ... }`
    if (isReactFunctionComponent(declaration)) {
        injectImport(
            ast,
            __internal_importHelperModuleName
        );
        injectHook(path.get('declaration'), relative);
        injected = true;
    } else if (t.isIdentifier(declaration)) {
        // Insert hook in `function Component() { ... }; export default Component;`
        const binding = path.scope.getBinding(
            declaration.name
        );
        if (binding) {
            // Right here we need to check if the binding is a declarator for the ArrowFunctionExpression. 
            // This code creates the correct NodePath for the if statement and the injectHook function.
            const expressionPath = t.isVariableDeclarator(binding.path.node) ? binding.path.get('init') : binding.path;
            if (isReactFunctionComponent(expressionPath.node)) {
                injectImport(
                    ast,
                    __internal_importHelperModuleName
                );
                injectHook(expressionPath, relative);
                injected = true;
            }
        }
    }
},

Let me know if I can help resolve this or if more information is required.

@wille
Copy link
Owner

wille commented Jan 7, 2025

Awesome, please make a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants