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

Improve readability of isValidElementType #19251

Conversation

behnammodi
Copy link
Contributor

@behnammodi behnammodi commented Jul 3, 2020

isValidElementType is hard to understand and maintenance, so I tried to improve it

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 3, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6696afc:

Sandbox Source
React Configuration

@sizebot
Copy link

sizebot commented Jul 3, 2020

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against 6696afc

@sizebot
Copy link

sizebot commented Jul 3, 2020

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against 6696afc

@behnammodi behnammodi changed the title improve readability improved readability Jul 3, 2020
Copy link
Contributor Author

@behnammodi behnammodi left a comment

Choose a reason for hiding this comment

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

I changed to switch/case

packages/shared/isValidElementType.js Outdated Show resolved Hide resolved
@rickhanlonii rickhanlonii changed the title improved readability Improve readability of isValidElementType Jul 4, 2020
Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

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

Not to nitpick, but since this is exclusively about readability, the switch statements are not as readable to me because they rely on fall throughs and no default cases, which I'm not used to when I read something like this.

What about just breaking the original into multiple ifs instead?

export default function isValidElementType(type: mixed) {
  if (typeof type === 'string' || typeof type === 'function') return true;

  // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
  if (
    type === REACT_FRAGMENT_TYPE ||
    type === REACT_PROFILER_TYPE ||
    type === REACT_DEBUG_TRACING_MODE_TYPE ||
    type === REACT_STRICT_MODE_TYPE ||
    type === REACT_SUSPENSE_TYPE ||
    type === REACT_SUSPENSE_LIST_TYPE ||
    type === REACT_LEGACY_HIDDEN_TYPE
  ) {
    return true;
  }

  if (typeof type === 'object' && type !== null) {
    if (
      type.$$typeof === REACT_LAZY_TYPE ||
      type.$$typeof === REACT_MEMO_TYPE ||
      type.$$typeof === REACT_PROVIDER_TYPE ||
      type.$$typeof === REACT_CONTEXT_TYPE ||
      type.$$typeof === REACT_FORWARD_REF_TYPE ||
      type.$$typeof === REACT_FUNDAMENTAL_TYPE ||
      type.$$typeof === REACT_RESPONDER_TYPE ||
      type.$$typeof === REACT_SCOPE_TYPE ||
      type.$$typeof === REACT_BLOCK_TYPE ||
      type[(0: any)] === REACT_SERVER_BLOCK_TYPE
    ) {
      return true;
    }
  }

  return false;
}

packages/shared/isValidElementType.js Show resolved Hide resolved
@behnammodi
Copy link
Contributor Author

@rickhanlonii Nice, I will update it

@behnammodi
Copy link
Contributor Author

@rickhanlonii I updated it.

Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

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

I don't oppose merging this but for future reference — we don't normally take stylistic PRs. Style and "readability" are extremely subjective and these changes often introduce subtle mistakes.

isValidElementType is hard to understand and maintenance

As maintainers, we don't find this particular function hard to maintain. We appreciate your effort but I would suggest to focus on changes that aren't purely stylistic as those are unlikely to be merged in general.

@behnammodi
Copy link
Contributor Author

@gaearon Thanks, sure

@gaearon gaearon merged commit 77e8722 into facebook:master Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants