Eslint-plugin-react: Rule proposal: disallow component methods with small levenshtein distance from lifecycle methods

Created on 16 Sep 2016  路  2Comments  路  Source: yannickcr/eslint-plugin-react

In #515, @adrianton3 proposed adding a rule that requires _ prefixes on non-lifecycle methods. His goal is:

I've mistyped shouldComponentUpdate and other React class members with long names too many times. This rule is intended to prevent that.

I think this could be accomplished with a rule that enforces that methods cannot have a small levenshtein distance from lifecycle methods. I'm not entirely sure what the cutoff should be, but maybe somewhere in the <= 3 range?

Bad:

var Hello = React.createClass({
  componentDidUpdaet() { 
    // this never executes since there's a typo in the method name
  },
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

Good:

var Hello = React.createClass({
  componentDidUpdate() { 
    // no typo here!
  },
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});
new rule

Most helpful comment

Yes, exactly

All 2 comments

If this is _not_ encouraging underscore prefixes but instead attempting to detect typos in lifecycle methods, that sounds like an _excellent_ idea.

Yes, exactly

Was this page helpful?
0 / 5 - 0 ratings

Related issues

inian picture inian  路  3Comments

budarin picture budarin  路  3Comments

BrodaNoel picture BrodaNoel  路  3Comments

CarlRosell picture CarlRosell  路  3Comments

AaronHarris picture AaronHarris  路  3Comments