|
| 1 | +/** |
| 2 | + * @fileoverview Prevent string definitions for references and prevent referencing this.refs |
| 3 | + * @author Tom Hastjarjanto |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/no-string-refs'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +require('babel-eslint'); |
| 15 | + |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | +// Tests |
| 18 | +// ------------------------------------------------------------------------------ |
| 19 | + |
| 20 | +var ruleTester = new RuleTester(); |
| 21 | +ruleTester.run('no-refs', rule, { |
| 22 | + |
| 23 | + valid: [{ |
| 24 | + code: [ |
| 25 | + 'var Hello = React.createClass({', |
| 26 | + ' componentDidMount: function() {', |
| 27 | + ' var component = this.hello;', |
| 28 | + ' },', |
| 29 | + ' render: function() {', |
| 30 | + ' return <div ref={c => this.hello = c}>Hello {this.props.name}</div>;', |
| 31 | + ' }', |
| 32 | + '});' |
| 33 | + ].join('\n'), |
| 34 | + parser: 'babel-eslint', |
| 35 | + ecmaFeatures: { |
| 36 | + jsx: true |
| 37 | + } |
| 38 | + } |
| 39 | + ], |
| 40 | + |
| 41 | + invalid: [{ |
| 42 | + code: [ |
| 43 | + 'var Hello = React.createClass({', |
| 44 | + ' componentDidMount: function() {', |
| 45 | + ' var component = this.refs.hello;', |
| 46 | + ' },', |
| 47 | + ' render: function() {', |
| 48 | + ' return <div>Hello {this.props.name}</div>;', |
| 49 | + ' }', |
| 50 | + '});' |
| 51 | + ].join('\n'), |
| 52 | + parser: 'babel-eslint', |
| 53 | + ecmaFeatures: { |
| 54 | + classes: true, |
| 55 | + jsx: true |
| 56 | + }, |
| 57 | + errors: [{ |
| 58 | + message: 'Using this.refs is deprecated.' |
| 59 | + }] |
| 60 | + }, { |
| 61 | + code: [ |
| 62 | + 'var Hello = React.createClass({', |
| 63 | + ' render: function() {', |
| 64 | + ' return <div ref="hello">Hello {this.props.name}</div>;', |
| 65 | + ' }', |
| 66 | + '});' |
| 67 | + ].join('\n'), |
| 68 | + parser: 'babel-eslint', |
| 69 | + ecmaFeatures: { |
| 70 | + classes: true, |
| 71 | + jsx: true |
| 72 | + }, |
| 73 | + errors: [{ |
| 74 | + message: 'Using string literals in ref attributes is deprecated.' |
| 75 | + }] |
| 76 | + }, { |
| 77 | + code: [ |
| 78 | + 'var Hello = React.createClass({', |
| 79 | + ' render: function() {', |
| 80 | + ' return <div ref={\'hello\'}>Hello {this.props.name}</div>;', |
| 81 | + ' }', |
| 82 | + '});' |
| 83 | + ].join('\n'), |
| 84 | + parser: 'babel-eslint', |
| 85 | + ecmaFeatures: { |
| 86 | + classes: true, |
| 87 | + jsx: true |
| 88 | + }, |
| 89 | + errors: [{ |
| 90 | + message: 'Using string literals in ref attributes is deprecated.' |
| 91 | + }] |
| 92 | + }, { |
| 93 | + code: [ |
| 94 | + 'var Hello = React.createClass({', |
| 95 | + ' componentDidMount: function() {', |
| 96 | + ' var component = this.refs.hello;', |
| 97 | + ' },', |
| 98 | + ' render: function() {', |
| 99 | + ' return <div ref="hello">Hello {this.props.name}</div>;', |
| 100 | + ' }', |
| 101 | + '});' |
| 102 | + ].join('\n'), |
| 103 | + parser: 'babel-eslint', |
| 104 | + ecmaFeatures: { |
| 105 | + classes: true, |
| 106 | + jsx: true |
| 107 | + }, |
| 108 | + errors: [{ |
| 109 | + message: 'Using this.refs is deprecated.' |
| 110 | + }, { |
| 111 | + message: 'Using string literals in ref attributes is deprecated.' |
| 112 | + }] |
| 113 | + } |
| 114 | +]}); |
0 commit comments