|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of UNSAFE_ methods |
| 3 | + * @author Sergei Startsev |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const rule = require('../../../lib/rules/no-unsafe'); |
| 12 | +const RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +const parserOptions = { |
| 15 | + ecmaVersion: 2018, |
| 16 | + sourceType: 'module', |
| 17 | + ecmaFeatures: { |
| 18 | + jsx: true |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +function errorMessage(method) { |
| 23 | + return `${method} is unsafe for use in async rendering, see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html`; |
| 24 | +} |
| 25 | + |
| 26 | +// ------------------------------------------------------------------------------ |
| 27 | +// Tests |
| 28 | +// ------------------------------------------------------------------------------ |
| 29 | + |
| 30 | +const ruleTester = new RuleTester({parserOptions}); |
| 31 | +ruleTester.run('no-unsafe', rule, { |
| 32 | + valid: [ |
| 33 | + { |
| 34 | + code: ` |
| 35 | + class Foo extends React.Component { |
| 36 | + componentDidUpdate() {} |
| 37 | + render() {} |
| 38 | + } |
| 39 | + `, |
| 40 | + settings: {react: {version: '16.4.0'}} |
| 41 | + }, |
| 42 | + { |
| 43 | + code: ` |
| 44 | + const Foo = createReactClass({ |
| 45 | + componentDidUpdate: function() {}, |
| 46 | + render: function() {} |
| 47 | + }); |
| 48 | + `, |
| 49 | + settings: {react: {version: '16.4.0'}} |
| 50 | + }, |
| 51 | + { |
| 52 | + code: ` |
| 53 | + class Foo extends Bar { |
| 54 | + UNSAFE_componentWillMount() {} |
| 55 | + UNSAFE_componentWillReceiveProps() {} |
| 56 | + UNSAFE_componentWillUpdate() {} |
| 57 | + } |
| 58 | + `, |
| 59 | + settings: {react: {version: '16.4.0'}} |
| 60 | + }, |
| 61 | + { |
| 62 | + code: ` |
| 63 | + const Foo = bar({ |
| 64 | + UNSAFE_componentWillMount: function() {}, |
| 65 | + UNSAFE_componentWillReceiveProps: function() {}, |
| 66 | + UNSAFE_componentWillUpdate: function() {}, |
| 67 | + }); |
| 68 | + `, |
| 69 | + settings: {react: {version: '16.4.0'}} |
| 70 | + }, |
| 71 | + { |
| 72 | + code: ` |
| 73 | + class Foo extends React.Component { |
| 74 | + UNSAFE_componentWillMount() {} |
| 75 | + UNSAFE_componentWillReceiveProps() {} |
| 76 | + UNSAFE_componentWillUpdate() {} |
| 77 | + } |
| 78 | + `, |
| 79 | + settings: {react: {version: '16.2.0'}} |
| 80 | + }, |
| 81 | + { |
| 82 | + code: ` |
| 83 | + const Foo = createReactClass({ |
| 84 | + UNSAFE_componentWillMount: function() {}, |
| 85 | + UNSAFE_componentWillReceiveProps: function() {}, |
| 86 | + UNSAFE_componentWillUpdate: function() {}, |
| 87 | + }); |
| 88 | + `, |
| 89 | + settings: {react: {version: '16.2.0'}} |
| 90 | + } |
| 91 | + ], |
| 92 | + |
| 93 | + invalid: [ |
| 94 | + { |
| 95 | + code: ` |
| 96 | + class Foo extends React.Component { |
| 97 | + UNSAFE_componentWillMount() {} |
| 98 | + UNSAFE_componentWillReceiveProps() {} |
| 99 | + UNSAFE_componentWillUpdate() {} |
| 100 | + } |
| 101 | + `, |
| 102 | + settings: {react: {version: '16.3.0'}}, |
| 103 | + errors: [ |
| 104 | + { |
| 105 | + message: errorMessage('UNSAFE_componentWillMount'), |
| 106 | + line: 2, |
| 107 | + column: 7, |
| 108 | + type: 'ClassDeclaration' |
| 109 | + }, |
| 110 | + { |
| 111 | + message: errorMessage('UNSAFE_componentWillReceiveProps'), |
| 112 | + line: 2, |
| 113 | + column: 7, |
| 114 | + type: 'ClassDeclaration' |
| 115 | + }, |
| 116 | + { |
| 117 | + message: errorMessage('UNSAFE_componentWillUpdate'), |
| 118 | + line: 2, |
| 119 | + column: 7, |
| 120 | + type: 'ClassDeclaration' |
| 121 | + } |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + code: ` |
| 126 | + const Foo = createReactClass({ |
| 127 | + UNSAFE_componentWillMount: function() {}, |
| 128 | + UNSAFE_componentWillReceiveProps: function() {}, |
| 129 | + UNSAFE_componentWillUpdate: function() {}, |
| 130 | + }); |
| 131 | + `, |
| 132 | + settings: {react: {version: '16.3.0'}}, |
| 133 | + errors: [ |
| 134 | + { |
| 135 | + message: errorMessage('UNSAFE_componentWillMount'), |
| 136 | + line: 2, |
| 137 | + column: 38, |
| 138 | + type: 'ObjectExpression' |
| 139 | + }, |
| 140 | + { |
| 141 | + message: errorMessage('UNSAFE_componentWillReceiveProps'), |
| 142 | + line: 2, |
| 143 | + column: 38, |
| 144 | + type: 'ObjectExpression' |
| 145 | + }, |
| 146 | + { |
| 147 | + message: errorMessage('UNSAFE_componentWillUpdate'), |
| 148 | + line: 2, |
| 149 | + column: 38, |
| 150 | + type: 'ObjectExpression' |
| 151 | + } |
| 152 | + ] |
| 153 | + } |
| 154 | + ] |
| 155 | +}); |
0 commit comments