binding_rules

0.0.1 • Public • Published

Binding rules

Linux build status This package provides two custom tslint rules to control binding functions: no-unbound-method-decorator and no-arrow-class-property, as described further below.

Installation

Install with npm:

$ npm install -D binding_rules

Register the rules in your tslint.json:

// tslint.json
{
  "rulesDirectory": ["node_modules/binding_rules/dist"],
  "rules": {
    // ...
  }
}

no-unbound-method-decorator

This is the same as the core no-unbound-method rule rule, but also supports an option decorator to specify an the name of a decorator that binds a function (e.g. the boundMethod decorator from autobind-decorator). If a method is decorated with this decorator, it won't be considered unbound.

// tslint.json
{
  "rulesDirectory": ["node_modules/binding_rules/dist"],
  "rules": {
    "no-unbound-method-decorator": [true, { "decorator": "boundMethod" }]
  }
}
import { boundMethod } from "autobind-decorator"
 
class Foo extends React.Component {
  nonDecorated() {}
 
  @boundMethod
  decorated() {}
 
  render() {
    return (
      <div>
        {/* bad */}
        <button onClick={this.nonDecorated}>Non-decorated</button>
 
        {/* good because of the decorator */}
        <button onClick={this.decorated}>Decorated</button>
      </div>
    )
  }
}

no-arrow-class-property

Enforces that there are no arrow function class properties. In conjunction with the no-unbound-method-decorator rule, this can be used to enforce that every method that needs to be bound is done so using a decorator and not an arrow function.

A decorator-bound method provides a few benefits over an arrow function class property: it can be called with super, it has a non-empty name property that contains the function's name, and it is lazily bound (assuming you're using autobind-decorator).

// tslint.json
{
  "rulesDirectory": ["node_modules/binding_rules/dist"],
  "rules": {
    "no-arrow-class-property": [true]
  }
}
class Foo {
  // bad
  foo = () => {}
 
  // good
  bar() {}
}

Contributors

Karthik Viswanathan -- karthik.ksv@gmail.com

License

Binding rules is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i binding_rules

Weekly Downloads

257

Version

0.0.1

License

MIT

Unpacked Size

20.2 kB

Total Files

7

Last publish

Collaborators

  • karthikv