Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Excluded native JSX elements from no-unsafe-any #3699

Merged
merged 8 commits into from
May 3, 2018
Merged

Excluded native JSX elements from no-unsafe-any #3699

merged 8 commits into from
May 3, 2018

Conversation

JoshuaKGoldberg
Copy link
Contributor

PR checklist

Overview of change:

Excludes identifiers from the rule if:

  • Their parent is a JSX element (opening or closing element, as full or self-closing)
  • The name's text starts with a lower-case letter.

Is there anything you'd like reviewers to focus on?

I couldn't find a better way to specify these native elements, since even with @types/react in the type system it still comes up as any. This lower-case name checking is pretty voodoo-ish (though that is in the JSX behavior spec!).

CHANGELOG.md entry:

[bugfix] no-unsafe-any no longer marks native JSX elements as unsafe

Joshua Goldberg and others added 2 commits February 6, 2018 10:36
Interesting how TypeScript auto-added single-apostrophes...
@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Feb 6, 2018

Agh, sent the PR early. Will fix failures :)

I've verified the fixes work locally, but can't get the test case of adding a failure to work in the harness... Ha, file name!

@JoshuaKGoldberg
Copy link
Contributor Author

Deprecation rule tests broken on testNext; otherwise passing...

@JoshuaKGoldberg
Copy link
Contributor Author

Hey @suchanlee just bringing this to your attention - IMO it's one of the higher priority PRs. #3080 blocks using the no-unsafe-any rule with React.

return isIdentifier(node)
&& node.parent !== undefined
&& jsxElementTypes.has(node.parent.kind)
&& isLowerCase(node.text[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of isLowerCase (which can allow false positives), it might be better to just get a set of all native jsx elements since they're exhaustive and well defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

set of all native jsx elements

I think that's no longer the case: facebook/react#2746

What kind of false positives should I be looking out for?

Off the top of my head, I'm thinking this might need to be changed to isFirstLetterLowerCase - trying to find a reference for what happens if you do something like <myElement />

Choose a reason for hiding this comment

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

@JoshuaKGoldberg any update here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the reminder - behaviorally, it looks like it's purely based on the first letter of the element name.

<span>lowercase span</span>
<sPan>camelCase sPan</sPan>

becomes in create-react-app:

<span>lowercase span</span>
<span>camelCase sPan</span>

Copy link
Contributor

Choose a reason for hiding this comment

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

oh i see cool. Yeah I think it should be first letter lower case instead of lower case.

]);

function isJsxNativeElement(node: ts.Node): boolean {
return isIdentifier(node)
Copy link
Contributor

Choose a reason for hiding this comment

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

how are fragments handled with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passes in TS > 2.1, fixing now...

return isIdentifier(node)
&& node.parent !== undefined
&& jsxElementTypes.has(node.parent.kind)
&& isLowerCase(node.text[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

oh i see cool. Yeah I think it should be first letter lower case instead of lower case.

@giladgray giladgray merged commit 93fd0ac into palantir:master May 3, 2018
@JoshuaKGoldberg JoshuaKGoldberg deleted the no-unsafe-any-tsx branch May 3, 2018 23:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants