|
| 1 | +/** |
| 2 | + * @fileoverview Prevents usage of Function.prototype.bind and arrow functions |
| 3 | + * in React component definition. |
| 4 | + * @author Daniel Lo Nigro <dan.cx> |
| 5 | + */ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Requirements |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +var rule = require('../../../lib/rules/jsx-no-bind'); |
| 13 | +var RuleTester = require('eslint').RuleTester; |
| 14 | + |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | +// Tests |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +var ruleTester = new RuleTester(); |
| 20 | +ruleTester.run('jsx-no-bind', rule, { |
| 21 | + |
| 22 | + valid: [ |
| 23 | + // Not covered by the rule |
| 24 | + { |
| 25 | + code: '<div onClick={this._handleClick}></div>', |
| 26 | + parser: 'babel-eslint' |
| 27 | + }, |
| 28 | + { |
| 29 | + code: '<div meaningOfLife={42}></div>', |
| 30 | + parser: 'babel-eslint' |
| 31 | + }, |
| 32 | + |
| 33 | + // bind() explicitly allowed |
| 34 | + { |
| 35 | + code: '<div onClick={this._handleClick.bind(this)}></div>', |
| 36 | + options: [{allowBind: true}], |
| 37 | + parser: 'babel-eslint' |
| 38 | + }, |
| 39 | + |
| 40 | + // Arrow functions explicitly allowed |
| 41 | + { |
| 42 | + code: '<div onClick={() => alert("1337")}></div>', |
| 43 | + options: [{allowArrowFunctions: true}], |
| 44 | + parser: 'babel-eslint' |
| 45 | + } |
| 46 | + ], |
| 47 | + |
| 48 | + invalid: [ |
| 49 | + // .bind() |
| 50 | + { |
| 51 | + code: '<div onClick={this._handleClick.bind(this)}></div>', |
| 52 | + errors: [{message: 'JSX props should not use .bind()'}], |
| 53 | + parser: 'babel-eslint' |
| 54 | + }, |
| 55 | + { |
| 56 | + code: '<div onClick={someGlobalFunction.bind(this)}></div>', |
| 57 | + errors: [{message: 'JSX props should not use .bind()'}], |
| 58 | + parser: 'babel-eslint' |
| 59 | + }, |
| 60 | + { |
| 61 | + code: '<div onClick={window.lol.bind(this)}></div>', |
| 62 | + errors: [{message: 'JSX props should not use .bind()'}], |
| 63 | + parser: 'babel-eslint' |
| 64 | + }, |
| 65 | + |
| 66 | + // Arrow functions |
| 67 | + { |
| 68 | + code: '<div onClick={() => alert("1337")}></div>', |
| 69 | + errors: [{message: 'JSX props should not use arrow functions'}], |
| 70 | + parser: 'babel-eslint' |
| 71 | + }, |
| 72 | + { |
| 73 | + code: '<div onClick={() => 42}></div>', |
| 74 | + errors: [{message: 'JSX props should not use arrow functions'}], |
| 75 | + parser: 'babel-eslint' |
| 76 | + }, |
| 77 | + { |
| 78 | + code: '<div onClick={param => { first(); second(); }}></div>', |
| 79 | + errors: [{message: 'JSX props should not use arrow functions'}], |
| 80 | + parser: 'babel-eslint' |
| 81 | + } |
| 82 | + ] |
| 83 | +}); |
0 commit comments