Skip to content

Commit 3be1d19

Browse files
committed
[patch] no-unsafe: report on the method instead of the entire component
This also makes error ordering more consistent since certain eslint/node combinations reverse the ordering
1 parent d5178be commit 3be1d19

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

88
### Fixed
99
* [`no-deprecated`]: prevent false positive on commonjs import ([#3614][] @akulsr0)
10+
* [`no-unsafe`]: report on the method instead of the entire component (@ljharb)
1011

1112
[#3614]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3614
1213

lib/rules/no-unsafe.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ module.exports = {
108108
const newMethod = meta.newMethod;
109109
const details = meta.details;
110110

111+
const propertyNode = astUtil.getComponentProperties(node)
112+
.find((property) => astUtil.getPropertyName(property) === method);
113+
111114
report(context, messages.unsafeMethod, 'unsafeMethod', {
112-
node,
115+
node: propertyNode,
113116
data: {
114117
method,
115118
newMethod,
@@ -135,7 +138,9 @@ module.exports = {
135138
function checkLifeCycleMethods(node) {
136139
if (componentUtil.isES5Component(node, context) || componentUtil.isES6Component(node, context)) {
137140
const methods = getLifeCycleMethods(node);
138-
methods.forEach((method) => checkUnsafe(node, method));
141+
methods
142+
.sort((a, b) => a.localeCompare(b))
143+
.forEach((method) => checkUnsafe(node, method));
139144
}
140145
}
141146

tests/lib/rules/no-unsafe.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ ruleTester.run('no-unsafe', rule, {
151151
newMethod: 'componentDidMount',
152152
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
153153
},
154-
line: 2,
155-
column: 9,
156-
type: 'ClassDeclaration',
154+
line: 3,
155+
column: 11,
156+
type: 'MethodDefinition',
157157
},
158158
{
159159
messageId: 'unsafeMethod',
@@ -162,9 +162,9 @@ ruleTester.run('no-unsafe', rule, {
162162
newMethod: 'getDerivedStateFromProps',
163163
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
164164
},
165-
line: 2,
166-
column: 9,
167-
type: 'ClassDeclaration',
165+
line: 4,
166+
column: 11,
167+
type: 'MethodDefinition',
168168
},
169169
{
170170
messageId: 'unsafeMethod',
@@ -173,9 +173,9 @@ ruleTester.run('no-unsafe', rule, {
173173
newMethod: 'componentDidUpdate',
174174
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
175175
},
176-
line: 2,
177-
column: 9,
178-
type: 'ClassDeclaration',
176+
line: 5,
177+
column: 11,
178+
type: 'MethodDefinition',
179179
},
180180
],
181181
},
@@ -196,9 +196,9 @@ ruleTester.run('no-unsafe', rule, {
196196
newMethod: 'componentDidMount',
197197
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
198198
},
199-
line: 2,
200-
column: 9,
201-
type: 'ClassDeclaration',
199+
line: 3,
200+
column: 11,
201+
type: 'MethodDefinition',
202202
},
203203
{
204204
messageId: 'unsafeMethod',
@@ -207,9 +207,9 @@ ruleTester.run('no-unsafe', rule, {
207207
newMethod: 'getDerivedStateFromProps',
208208
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
209209
},
210-
line: 2,
211-
column: 9,
212-
type: 'ClassDeclaration',
210+
line: 4,
211+
column: 11,
212+
type: 'MethodDefinition',
213213
},
214214
{
215215
messageId: 'unsafeMethod',
@@ -218,9 +218,9 @@ ruleTester.run('no-unsafe', rule, {
218218
newMethod: 'componentDidUpdate',
219219
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
220220
},
221-
line: 2,
222-
column: 9,
223-
type: 'ClassDeclaration',
221+
line: 5,
222+
column: 11,
223+
type: 'MethodDefinition',
224224
},
225225
],
226226
},
@@ -243,9 +243,9 @@ ruleTester.run('no-unsafe', rule, {
243243
newMethod: 'componentDidMount',
244244
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
245245
},
246-
line: 2,
247-
column: 38,
248-
type: 'ObjectExpression',
246+
line: 3,
247+
column: 11,
248+
type: 'Property',
249249
},
250250
{
251251
messageId: 'unsafeMethod',
@@ -254,9 +254,9 @@ ruleTester.run('no-unsafe', rule, {
254254
newMethod: 'getDerivedStateFromProps',
255255
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
256256
},
257-
line: 2,
258-
column: 38,
259-
type: 'ObjectExpression',
257+
line: 4,
258+
column: 11,
259+
type: 'Property',
260260
},
261261
{
262262
messageId: 'unsafeMethod',
@@ -265,9 +265,9 @@ ruleTester.run('no-unsafe', rule, {
265265
newMethod: 'componentDidUpdate',
266266
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
267267
},
268-
line: 2,
269-
column: 38,
270-
type: 'ObjectExpression',
268+
line: 5,
269+
column: 11,
270+
type: 'Property',
271271
},
272272
],
273273
},
@@ -288,9 +288,9 @@ ruleTester.run('no-unsafe', rule, {
288288
newMethod: 'componentDidMount',
289289
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
290290
},
291-
line: 2,
292-
column: 38,
293-
type: 'ObjectExpression',
291+
line: 3,
292+
column: 11,
293+
type: 'Property',
294294
},
295295
{
296296
messageId: 'unsafeMethod',
@@ -299,9 +299,9 @@ ruleTester.run('no-unsafe', rule, {
299299
newMethod: 'getDerivedStateFromProps',
300300
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
301301
},
302-
line: 2,
303-
column: 38,
304-
type: 'ObjectExpression',
302+
line: 4,
303+
column: 11,
304+
type: 'Property',
305305
},
306306
{
307307
messageId: 'unsafeMethod',
@@ -310,9 +310,9 @@ ruleTester.run('no-unsafe', rule, {
310310
newMethod: 'componentDidUpdate',
311311
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
312312
},
313-
line: 2,
314-
column: 38,
315-
type: 'ObjectExpression',
313+
line: 5,
314+
column: 11,
315+
type: 'Property',
316316
},
317317
],
318318
},

0 commit comments

Comments
 (0)