Skip to content

Commit 5b08a1e

Browse files
Adjusted no-unsafe rule
1 parent b5e86bf commit 5b08a1e

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

docs/rules/no-unsafe.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Prevent usage of `UNSAFE_` methods (react/no-unsafe)
22

3-
Certain legacy lifecycle methods are unsafe for use in async React applications and cause warnings in [_strict mode_][strict_mode]. These also happen to be the lifecycles that cause the most [confusion within the React community][component_lifecycle_changes].
3+
Certain legacy lifecycle methods are [unsafe for use in async React applications][async_rendering] and cause warnings in [_strict mode_][strict_mode]. These also happen to be the lifecycles that cause the most [confusion within the React community][component_lifecycle_changes].
44

5+
[async_rendering]: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html
56
[strict_mode]: https://reactjs.org/docs/strict-mode.html#identifying-unsafe-lifecycles
67
[component_lifecycle_changes]: https://reactjs.org/blog/2018/03/29/react-v-16-3.html#component-lifecycle-changes
78

lib/rules/no-unsafe.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const Components = require('../util/Components');
99
const astUtil = require('../util/ast');
1010
const docsUrl = require('../util/docsUrl');
11+
const versionUtil = require('../util/version');
1112

1213
// ------------------------------------------------------------------------------
1314
// Rule Definition
@@ -44,7 +45,8 @@ module.exports = {
4445
*/
4546
function isUnsafe(method) {
4647
const unsafeMethods = getUnsafeMethods();
47-
return unsafeMethods.indexOf(method) !== -1;
48+
const isApplicable = versionUtil.testReactVersion(context, '16.3.0');
49+
return unsafeMethods.indexOf(method) !== -1 && isApplicable;
4850
}
4951

5052
/**
@@ -59,7 +61,7 @@ module.exports = {
5961

6062
context.report({
6163
node: node,
62-
message: `Do not use ${method}`
64+
message: `${method} is unsafe for use in async rendering, see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html`
6365
});
6466
}
6567

tests/lib/rules/no-unsafe.js

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const parserOptions = {
1919
}
2020
};
2121

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+
2226
// ------------------------------------------------------------------------------
2327
// Tests
2428
// ------------------------------------------------------------------------------
@@ -32,15 +36,17 @@ ruleTester.run('no-unsafe', rule, {
3236
componentDidUpdate() {}
3337
render() {}
3438
}
35-
`
39+
`,
40+
settings: {react: {version: '16.4.0'}}
3641
},
3742
{
3843
code: `
3944
const Foo = createReactClass({
4045
componentDidUpdate: function() {},
4146
render: function() {}
4247
});
43-
`
48+
`,
49+
settings: {react: {version: '16.4.0'}}
4450
},
4551
{
4652
code: `
@@ -49,7 +55,8 @@ ruleTester.run('no-unsafe', rule, {
4955
UNSAFE_componentWillReceiveProps() {}
5056
UNSAFE_componentWillUpdate() {}
5157
}
52-
`
58+
`,
59+
settings: {react: {version: '16.4.0'}}
5360
},
5461
{
5562
code: `
@@ -58,7 +65,28 @@ ruleTester.run('no-unsafe', rule, {
5865
UNSAFE_componentWillReceiveProps: function() {},
5966
UNSAFE_componentWillUpdate: function() {},
6067
});
61-
`
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'}}
6290
}
6391
],
6492

@@ -71,15 +99,28 @@ ruleTester.run('no-unsafe', rule, {
7199
UNSAFE_componentWillUpdate() {}
72100
}
73101
`,
102+
settings: {react: {version: '16.3.0'}},
74103
errors: [
75104
{
76-
message: 'Do not use UNSAFE_componentWillMount'
105+
message: errorMessage('UNSAFE_componentWillMount'),
106+
line: 2,
107+
column: 7,
108+
endLine: 6,
109+
endColumn: 8
77110
},
78111
{
79-
message: 'Do not use UNSAFE_componentWillReceiveProps'
112+
message: errorMessage('UNSAFE_componentWillReceiveProps'),
113+
line: 2,
114+
column: 7,
115+
endLine: 6,
116+
endColumn: 8
80117
},
81118
{
82-
message: 'Do not use UNSAFE_componentWillUpdate'
119+
message: errorMessage('UNSAFE_componentWillUpdate'),
120+
line: 2,
121+
column: 7,
122+
endLine: 6,
123+
endColumn: 8
83124
}
84125
]
85126
},
@@ -91,15 +132,20 @@ ruleTester.run('no-unsafe', rule, {
91132
UNSAFE_componentWillUpdate: function() {},
92133
});
93134
`,
135+
settings: {react: {version: '16.3.0'}},
94136
errors: [
95137
{
96-
message: 'Do not use UNSAFE_componentWillMount'
138+
message: errorMessage('UNSAFE_componentWillMount'),
139+
line: 2,
140+
column: 38,
141+
endLine: 6,
142+
endColumn: 10
97143
},
98144
{
99-
message: 'Do not use UNSAFE_componentWillReceiveProps'
145+
message: errorMessage('UNSAFE_componentWillReceiveProps')
100146
},
101147
{
102-
message: 'Do not use UNSAFE_componentWillUpdate'
148+
message: errorMessage('UNSAFE_componentWillUpdate')
103149
}
104150
]
105151
}

0 commit comments

Comments
 (0)