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

Fix invalid hook usage for exports #4385

Merged
merged 3 commits into from
Aug 23, 2022
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
5 changes: 5 additions & 0 deletions .changeset/flat-ads-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix warning when using hooks inside the react components not exported as a function declaration
45 changes: 31 additions & 14 deletions packages/astro/src/vite-plugin-jsx/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,38 @@ export default function tagExportsWithRenderer({
if (node.exportKind === 'type') return;
if (node.type === 'ExportAllDeclaration') return;

if (node.type === 'ExportNamedDeclaration') {
if (t.isFunctionDeclaration(node.declaration)) {
if (node.declaration.id?.name) {
const id = node.declaration.id.name;
const tags = state.get('astro:tags') ?? [];
state.set('astro:tags', [...tags, id]);
}
const addTag = (id: string) => {
const tags = state.get('astro:tags') ?? [];
state.set('astro:tags', [...tags, id]);
}

if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration') {
if (t.isIdentifier(node.declaration)) {
addTag(node.declaration.name);
}
} else if (node.type === 'ExportDefaultDeclaration') {
if (t.isFunctionDeclaration(node.declaration)) {
if (node.declaration.id?.name) {
const id = node.declaration.id.name;
const tags = state.get('astro:tags') ?? [];
state.set('astro:tags', [...tags, id]);
}
else if (t.isFunctionDeclaration(node.declaration) && node.declaration.id?.name) {
addTag(node.declaration.id.name);
}
else if (t.isVariableDeclaration(node.declaration)) {
node.declaration.declarations?.forEach(declaration => {
if (t.isArrowFunctionExpression(declaration.init) && t.isIdentifier(declaration.id)) {
addTag(declaration.id.name);
}
});
}
else if (t.isObjectExpression(node.declaration)) {
node.declaration.properties?.forEach(property => {
if (t.isProperty(property) && t.isIdentifier(property.key)) {
addTag(property.key.name);
}
});
}
else if (t.isExportNamedDeclaration(node)) {
node.specifiers.forEach(specifier => {
if (t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported)) {
addTag(specifier.local.name);
}
});
}
}
},
Expand Down