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(react-refresh): check FunctionDeclaration nodes properly #2903

Merged
merged 2 commits into from
Apr 17, 2021
Merged
Changes from 1 commit
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
29 changes: 20 additions & 9 deletions packages/plugin-react-refresh/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,33 @@ function isRefreshBoundary(ast) {
return true
}
const { declaration, specifiers } = node
if (declaration && declaration.type === 'VariableDeclaration') {
return declaration.declarations.every(
({ id }) => id.type === 'Identifier' && isComponentishName(id.name)
)
if (declaration) {
if (declaration.type === 'VariableDeclaration') {
return declaration.declarations.every((variable) => {
return isComponentLikeIdentifier(variable.id)
})
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
}
if (declaration.type === 'FunctionDeclaration') {
return isComponentLikeIdentifier(declaration.id)
}
}
return specifiers.every(
({ exported }) =>
exported.type === 'Identifier' && isComponentishName(exported.name)
)
return specifiers.every((spec) => {
return isComponentLikeIdentifier(spec.exported)
})
})
}

/**
* @param {import('@babel/types').Node} node
*/
function isComponentLikeIdentifier(node) {
return node.type === 'Identifier' && isComponentLikeName(node.name)
}

/**
* @param {string} name
*/
function isComponentishName(name) {
function isComponentLikeName(name) {
return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z'
}

Expand Down