Skip to content

Commit 8d2c7a9

Browse files
Add aliases option to check aliases of unsafe methods
Avoid breaking changes by adding the non-prefixed methods behind the option
1 parent 65116d6 commit 8d2c7a9

File tree

3 files changed

+87
-42
lines changed

3 files changed

+87
-42
lines changed

docs/rules/no-unsafe.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ The rule checks the following methods:
1616
The following patterns are considered warnings:
1717

1818
```jsx
19-
class Foo extends React.Component {
20-
componentWillMount() {}
21-
componentWillReceiveProps() {}
22-
componentWillUpdate() {}
23-
}
24-
// or
2519
class Foo extends React.Component {
2620
UNSAFE_componentWillMount() {}
2721
UNSAFE_componentWillReceiveProps() {}
@@ -30,12 +24,6 @@ class Foo extends React.Component {
3024
```
3125

3226
```jsx
33-
const Foo = createReactClass({
34-
componentWillMount: function() {},
35-
componentWillReceiveProps: function() {},
36-
componentWillUpdate: function() {}
37-
});
38-
// or
3927
const Foo = createReactClass({
4028
UNSAFE_componentWillMount: function() {},
4129
UNSAFE_componentWillReceiveProps: function() {},
@@ -46,12 +34,6 @@ const Foo = createReactClass({
4634
The following patterns are **not** considered warnings:
4735

4836
```jsx
49-
class Foo extends Bar {
50-
componentWillMount() {}
51-
componentWillReceiveProps() {}
52-
componentWillUpdate() {}
53-
}
54-
// or
5537
class Foo extends Bar {
5638
UNSAFE_componentWillMount() {}
5739
UNSAFE_componentWillReceiveProps() {}
@@ -61,14 +43,55 @@ class Foo extends Bar {
6143

6244
```jsx
6345
const Foo = bar({
46+
UNSAFE_componentWillMount: function() {},
47+
UNSAFE_componentWillReceiveProps: function() {},
48+
UNSAFE_componentWillUpdate: function() {}
49+
});
50+
```
51+
52+
## Rule Options
53+
```json
54+
...
55+
"react/no-unsafe": [<enabled>, { "checkAliases": <boolean> }]
56+
...
57+
```
58+
59+
### `checkAliases` (default: `false`)
60+
61+
When `true` the rule will also check aliases of unsafe methods: `componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`.
62+
63+
The following patterns are considered warnings:
64+
65+
```jsx
66+
class Foo extends React.Component {
67+
componentWillMount() {}
68+
componentWillReceiveProps() {}
69+
componentWillUpdate() {}
70+
}
71+
```
72+
73+
```jsx
74+
const Foo = createReactClass({
6475
componentWillMount: function() {},
6576
componentWillReceiveProps: function() {},
6677
componentWillUpdate: function() {}
6778
});
68-
// or
79+
```
80+
81+
The following patterns are **not** considered warnings:
82+
83+
```jsx
84+
class Foo extends Bar {
85+
componentWillMount() {}
86+
componentWillReceiveProps() {}
87+
componentWillUpdate() {}
88+
}
89+
```
90+
91+
```jsx
6992
const Foo = bar({
70-
UNSAFE_componentWillMount: function() {},
71-
UNSAFE_componentWillReceiveProps: function() {},
72-
UNSAFE_componentWillUpdate: function() {}
93+
componentWillMount: function() {},
94+
componentWillReceiveProps: function() {},
95+
componentWillUpdate: function() {}
7396
});
74-
```
97+
```

lib/rules/no-unsafe.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,51 @@ module.exports = {
2222
recommended: false,
2323
url: docsUrl('no-unsafe')
2424
},
25-
schema: []
25+
schema: [
26+
{
27+
type: 'object',
28+
properties: {
29+
checkAliases: {
30+
default: false,
31+
type: 'boolean'
32+
}
33+
},
34+
additionalProperties: false
35+
}
36+
]
2637
},
2738

2839
create: Components.detect((context, components, utils) => {
40+
const config = context.options[0] || {};
41+
const checkAliases = config.checkAliases || false;
42+
2943
const isApplicable = versionUtil.testReactVersion(context, '16.3.0');
3044
if (!isApplicable) {
3145
return {};
3246
}
3347

34-
const unsafe = {};
35-
unsafe.componentWillMount = unsafe.UNSAFE_componentWillMount = {
36-
newMethod: 'componentDidMount',
37-
details:
38-
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
39-
};
40-
unsafe.componentWillReceiveProps = unsafe.UNSAFE_componentWillReceiveProps = {
41-
newMethod: 'getDerivedStateFromProps',
42-
details:
43-
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
44-
};
45-
unsafe.componentWillUpdate = unsafe.UNSAFE_componentWillUpdate = {
46-
newMethod: 'componentDidUpdate',
47-
details:
48-
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
48+
const unsafe = {
49+
UNSAFE_componentWillMount: {
50+
newMethod: 'componentDidMount',
51+
details:
52+
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
53+
},
54+
UNSAFE_componentWillReceiveProps: {
55+
newMethod: 'getDerivedStateFromProps',
56+
details:
57+
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
58+
},
59+
UNSAFE_componentWillUpdate: {
60+
newMethod: 'componentDidUpdate',
61+
details:
62+
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
63+
}
4964
};
65+
if (checkAliases) {
66+
unsafe.componentWillMount = unsafe.UNSAFE_componentWillMount;
67+
unsafe.componentWillReceiveProps = unsafe.UNSAFE_componentWillReceiveProps;
68+
unsafe.componentWillUpdate = unsafe.UNSAFE_componentWillUpdate;
69+
}
5070

5171
/**
5272
* Returns a list of unsafe methods

tests/lib/rules/no-unsafe.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ruleTester.run('no-unsafe', rule, {
9797
componentWillUpdate() {}
9898
}
9999
`,
100-
settings: {react: {version: '16.2.0'}}
100+
settings: {react: {version: '16.4.0'}}
101101
},
102102
{
103103
code: `
@@ -118,7 +118,7 @@ ruleTester.run('no-unsafe', rule, {
118118
componentWillUpdate: function() {},
119119
});
120120
`,
121-
settings: {react: {version: '16.2.0'}}
121+
settings: {react: {version: '16.4.0'}}
122122
},
123123
{
124124
code: `
@@ -142,7 +142,8 @@ ruleTester.run('no-unsafe', rule, {
142142
componentWillUpdate() {}
143143
}
144144
`,
145-
settings: {react: {version: '16.3.0'}},
145+
options: [{checkAliases: true}],
146+
settings: {react: {version: '16.4.0'}},
146147
errors: [
147148
{
148149
message: errorMessage(
@@ -227,6 +228,7 @@ ruleTester.run('no-unsafe', rule, {
227228
componentWillUpdate: function() {},
228229
});
229230
`,
231+
options: [{checkAliases: true}],
230232
settings: {react: {version: '16.3.0'}},
231233
errors: [
232234
{

0 commit comments

Comments
 (0)