Skip to content

fix(eslint): don't read ref.current on render#541

Open
lismith2-cisco wants to merge 1 commit intomomentum-design:masterfrom
lismith2-cisco:SPARK-543350-UNSAFE-REF-ESLINT
Open

fix(eslint): don't read ref.current on render#541
lismith2-cisco wants to merge 1 commit intomomentum-design:masterfrom
lismith2-cisco:SPARK-543350-UNSAFE-REF-ESLINT

Conversation

@lismith2-cisco
Copy link
Collaborator

@lismith2-cisco lismith2-cisco added the validated If the pull request is validated automation. label Jul 18, 2024
'no-restricted-syntax': [
'warn',
{
'selector': "JSXAttribute > JSXExpressionContainer > MemberExpression[property.name='current']",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This selector does not work in every case. For example in the src/components/TextInput/TextInput.tsx there are 3 useRefs:

  const messageId = useRef(uuidV4());
  const labelId = useRef(uuidV4());
  const clearButtonId = useRef(uuidV4());

When the clearButtonId.current used directly ESLint give me the warning, but when messageId.cirrent is in an expression then there is no warning

image

Copy link
Collaborator Author

@lismith2-cisco lismith2-cisco Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but I don't think it is possible to get eslint to validate this. If you can work out a way, please let me know :) It looks like you can maybe do it better in flow ( https://flow.org/en/docs/react/hook-syntax/#preventing-unsafe-mutation ) but I've not seen anything for TypeScript.

Copy link
Collaborator

@maxinteger maxinteger Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works with the * wildcard selector, but (for some reason) does not select root level expressions, so I combined the two:

"JSXAttribute > JSXExpressionContainer > MemberExpression[property.name='current'], JSXAttribute > JSXExpressionContainer * MemberExpression[property.name='current']",

I tried to simplify it in based on ESQuery Selectors, without any luck. But I spent only 5 minutes on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

validated If the pull request is validated automation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants