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] destructuring-assignment: Handle destructuring of useContext in SFC #2797

Merged
merged 1 commit into from Dec 28, 2022

Conversation

ed-jinyoung-park
Copy link
Contributor

@ed-jinyoung-park ed-jinyoung-park commented Sep 16, 2020

i reopen PR on resolve issue - #2309 cause i did a lot of commits while editing.

  • add rule of handling destructuring of useContext in SFC
  • delete detecting variable named 'context' in SFC

Fixes #2309. Closes #2787.

…in SFC

Co-authored-by: Jin Young Park <jy2045@sogang.ac.kr>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
lib/rules/destructuring-assignment.js Outdated Show resolved Hide resolved
Comment on lines 143 to 146
const destructuringUseContext = destructuring && node.init.callee && node.init.callee.name === 'useContext';
// let foo = useContext(aContext);
const assignUseContext = identifier && node.init.callee && node.init.callee.name === 'useContext';
Copy link
Member

Choose a reason for hiding this comment

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

are we sure useContext is the react hook? I think we should make sure that nobody's using a custom hook or function named useContext, and that this useContext is imported/required from react.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://reactjs.org/docs/hooks-reference.html#usecontext
here in official docs of react says useContext is one of basic hooks with useState, useEffect.
and we import useContext like import {useContext} from 'react';

I think if somebody's using function named useContext which overwrite basic hooks , I think we should make another rule about custom hooks which overwrite basic hooks.

Copy link
Member

Choose a reason for hiding this comment

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

Someone might be using a version of React that doesn't have hooks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

then is there any possible way to check version of react? or we should not include this part.

Copy link
Member

Choose a reason for hiding this comment

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

Yes! Check out lib/util/version.js's testReactVersion method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do I have to import testReactVersion method in lib/rules/destructuring-assignment' to check react version in rules? I have no idea.. could you help me with this?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's right

@ljharb
Copy link
Member

ljharb commented Sep 20, 2021

@Zinyon are you still interested in completing this PR?

@ljharb
Copy link
Member

ljharb commented Sep 20, 2021

The isDestructuredFromPragmaImport helper might be useful here.

@ed-jinyoung-park
Copy link
Contributor Author

@ljharb sure, i will look around isDestructuredFromPragmaImport helper and complete PR soon!

@codecov
Copy link

codecov bot commented Dec 27, 2022

Codecov Report

Merging #2797 (a60f020) into master (a60f020) will not change coverage.
The diff coverage is n/a.

❗ Current head a60f020 differs from pull request most recent head 523db20. Consider uploading reports for the commit 523db20 to get more accurate results

@@           Coverage Diff           @@
##           master    #2797   +/-   ##
=======================================
  Coverage   97.58%   97.58%           
=======================================
  Files         130      130           
  Lines        9223     9223           
  Branches     3349     3349           
=======================================
  Hits         9000     9000           
  Misses        223      223           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@ljharb
Copy link
Member

ljharb commented Dec 27, 2022

I went ahead and added the react version dependence; i'll leave "make sure it's destructured from the pragma" as a followup.

@ljharb ljharb marked this pull request as ready for review December 27, 2022 23:39
@ljharb ljharb merged commit 523db20 into jsx-eslint:master Dec 28, 2022
ljharb pushed a commit to 102/eslint-plugin-react that referenced this pull request Jun 8, 2023
…Context in SFC"

 - [Tests] `destructuring-assignment`: Add more modern context cases

This reverts commit 523db20 / jsx-eslint#2797

Fixes jsx-eslint#3536.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[react/destructuring-assignment] Shows weird behaviour when I declare a variable with the name context
2 participants