Skip to content

Proposal: Let's start using tslint-microsoft-contrib #590

Closed

Description

@niik

In #589 I had forgotten to bind a callback function which caused #587. The change was rather hard to spot:

       <div id='foldout-container' style={this.getFoldoutContainerStyle()}>
-        <div className='overlay' onClick={this.handleOverlayClick}></div>
+        <div className='overlay' onClick={() => this.handleOverlayClick}></div>
         <div className='foldout' style={this.getFoldoutStyle()}>
           {this.props.dropdownContentRenderer()}
         </div>
       </div>

I really wanted some static analysis to have caught this error so I went looking and I found this absolute 💎 called microsoft-tslint-contrib which has a bunch of excellent react related tslint rules and I propose we start working towards using them. I propose we start with the following rules

react-unused-props-and-state

Remove unneeded properties defined in React Props and State interfaces. Any interface named Props or State is defined as a React interface. All fields in these interfaces must be referenced.

@joshaber You and me talked about this a few days back, seems like a no brainer

react-this-binding-issue

Several errors can occur when using React and React.Component subclasses. When using React components you must be careful to correctly bind the 'this' reference on any methods that you pass off to child components as callbacks. For example, it is common to define a private method called 'onClick' and then specify onClick={this.onClick} as a JSX attribute. If you do this then the 'this' reference will be undefined when your private method is invoked.

The React documentation suggests that you bind the 'this' reference on all of your methods within the constructor: this.onClick = this.onClick.bind(this);. This rule will create a violation if

  1. a method reference is passed to a JSX attribute without being bound in the constructor. And
  2. a method is bound multiple times in the constructor.

Another issue that can occur is binding the 'this' reference to a function within the render() method. For example, many people will create an anonymous lambda within the JSX attribute to avoid the 'this' binding issue: onClick={() => { this.onClick(); }}. The problem with this is that a new instance of an anonymous function is created every time render() is invoked. When React compares virutal DOM properties within shouldComponentUpdate() then the onClick property will look like a new property and force a re-render.

You should avoid this pattern because creating function instances within render methods breaks any logic within shouldComponentUpdate() methods. This rule creates violations if

  1. an anonymous function is passed as a JSX attribute. And
  2. if a function instantiated in local scope is passed as a JSX attribute.

This rule can be configured via the "allow-anonymous-listeners" parameter. If you want to suppress violations for the anonymous listener scenarios then configure that rule like this: "react-this-binding-issue": [ true, { 'allow-anonymous-listeners': true } ]

This directly addresses the issue I had. My initial reaction here is that using bind in the constructor seems a little bit sad and akin a little too much to dynamic languages. There's also the concern about .bind` being slow but at least that seems to be changing. Likely the performance difference won't matter to us now if it means we can avoid re-rendering.

no-stateless-class

A stateless class represents a failure in the object oriented design of the system. A class without state is better modeled as a module or given some state. A stateless class is defined as a class with only static members and no parent class.

@joshaber this one is for you. I suspect LocalGitOperations would be the first to go with this and I'm absolutely fine with that

no-suspicious-comment

Do not use suspicious comments, such as BUG, HACK, FIXME, LATER, LATER2, TODO. We recommend that you run this rule before each release as a quality checkpoint.

Maybe on this one. I like the theory that we could run it for PR builds but not fail builds locally while developing.

promise-must-complete

When a Promise instance is created, then either the reject() or resolve() parameter must be called on it within all code branches in the scope.

❤️

insecure-random

Do not use insecure sources for random bytes. Use a secure random number generator instead. Bans all uses of Math.random and crypto.pseudoRandomBytes. Better alternatives are crypto.randomBytes and window.crypto.getRandomValues.
References:
* CWE 330
* MDN Math.random
* Node.js crypto.randomBytes()
* window.crypto.getRandomValues()

I like this in theory although I'm pretty sure there won't be many places in our codebase where a cryptographically strong random number is actually necessary so maybe we'll just end up with tslint-ignore comments in our code but it's a nice safety precaution going forward.


So what do y'all think? Does this seem like a good use of our time? I certainly feel like it would and the longer we wait the more work it'll be to fix. I think I'm gonna PoC this with the react-unused-props rule.

Activity

  1. joshaber commented on Nov 4, 2016

    @joshaber
    Contributor

    Hot damn!

    react-unused-props-and-state

    👍 💯

    react-this-binding-issue

    My initial reaction here is that using bind in the constructor seems a little bit sad and akin a little too much to dynamic languages.

    Yeah... I wonder if it's smart enough to be OK with defining functions in the instance property style:

    const myCallback = () => {
      ...
    }
    

    no-stateless-class

    👍 💯

    no-suspicious-comment

    Maybe on this one. I like the theory that we could run it for PR builds but not fail builds locally while developing.

    Yeah that'd be interesting. Or maybe once we hit 1.0.

    promise-must-complete

    👍 💯

    insecure-random

    I like this in theory although I'm pretty sure there won't be many places in our codebase where a cryptographically strong random number is actually necessary

    Yeah, maybe worth it just to force us to think through the issue.

  2. shiftkey commented on Nov 6, 2016

    @shiftkey
    Member

    Yeah, not sure how I feel about the .bind() but if it catches our mistakes then perhaps the perf improvements in Node v7 will be fine.

    I think no-suspicious-comment is the only one I'm not 👍 💯 on, but I'm willing to experiment with it...

  3. niik commented on Nov 16, 2016

    @niik
    MemberAuthor

    As soon as #637 lands this will be completed 🎉

  4. locked as resolved and limited conversation to collaborators on Jul 23, 2018
  5. unlocked this conversation on Sep 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions