Skip to content

Commit

Permalink
fix(react-refresh): check FunctionDeclaration nodes properly (vitejs#…
Browse files Browse the repository at this point in the history
…2903)

Co-authored-by: Shinigami <chrissi92@hotmail.de>
  • Loading branch information
2 people authored and TobiasMelen committed May 3, 2021
1 parent e4633d3 commit 110485a
Showing 1 changed file with 20 additions and 9 deletions.
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) => isComponentLikeIdentifier(variable.id)
)
}
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

0 comments on commit 110485a

Please sign in to comment.