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

False positive for negative number with allowConstantExport #43

Closed
RandScullard opened this issue Jul 9, 2024 · 1 comment
Closed

Comments

@RandScullard
Copy link

My eslintrc.cjs contains:

  rules: {
    "react-refresh/only-export-components": [
      "error",
      { "allowConstantExport": true },
    ],

I have a .tsx file with the following exported constant:

export const allTestCategories = -1

ESLint produces this error:

ERROR(ESLint) Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components. (react-refresh/only-export-components)

If I simply change the constant value from -1 to 1, the ESLint error goes away. Looking at only-export-components.ts line 105, I see that you are checking for Literal, TemplateLiteral, and BinaryExpression, but the problem is that -1 is a UnaryExpression. I think you need to add UnaryExpression to your logic.

I tried this as a workaround:

export const allTestCategories = -1 as number

This produced a TSAsExpression, which is also not handled by your logic. I'm not sure of all the implications, but it might make sense to allow this as well.

@ArnaudBarre
Copy link
Owner

ArnaudBarre commented Jul 9, 2024

Thanks for investigating and debugging this! This is definitely a edge case I didn't though of.

The fix is published in 0.4.8

I choose to not include TSAsExpression, it would require some extra checks on the expression being cast and I don't see any reason why you will need a type assertion for something that should be a primitive value.

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