Skip to content

Commit f27285f

Browse files
authored
Merge pull request jsx-eslint#1854 from jsnajdr/lifecycle-method-node-ident
For deprecated lifecycle methods, report identifier AST node instead of the class node
2 parents 20779ff + 248c2ca commit f27285f

File tree

3 files changed

+113
-36
lines changed

3 files changed

+113
-36
lines changed

lib/rules/no-deprecated.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,19 @@ module.exports = {
106106
);
107107
}
108108

109-
function checkDeprecation(node, method) {
110-
if (!isDeprecated(method)) {
109+
function checkDeprecation(node, methodName, methodNode) {
110+
if (!isDeprecated(methodName)) {
111111
return;
112112
}
113113
const deprecated = getDeprecated();
114-
const version = deprecated[method][0];
115-
const newMethod = deprecated[method][1];
116-
const refs = deprecated[method][2];
114+
const version = deprecated[methodName][0];
115+
const newMethod = deprecated[methodName][1];
116+
const refs = deprecated[methodName][2];
117117
context.report({
118-
node: node,
118+
node: methodNode || node,
119119
message: DEPRECATED_MESSAGE,
120120
data: {
121-
oldMethod: method,
121+
oldMethod: methodName,
122122
version,
123123
newMethod: newMethod ? `, use ${newMethod} instead` : '',
124124
refs: refs ? `, see ${refs}` : ''
@@ -150,7 +150,10 @@ module.exports = {
150150
*/
151151
function getLifeCycleMethods(node) {
152152
const properties = astUtil.getComponentProperties(node);
153-
return properties.map(property => astUtil.getPropertyName(property));
153+
return properties.map(property => ({
154+
name: astUtil.getPropertyName(property),
155+
node: astUtil.getPropertyNameNode(property)
156+
}));
154157
}
155158

156159
/**
@@ -160,7 +163,7 @@ module.exports = {
160163
function checkLifeCycleMethods(node) {
161164
if (utils.isES5Component(node) || utils.isES6Component(node)) {
162165
const methods = getLifeCycleMethods(node);
163-
methods.forEach(method => checkDeprecation(node, method));
166+
methods.forEach(method => checkDeprecation(node, method.name, method.node));
164167
}
165168
}
166169

lib/util/ast.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ function findReturnStatement(node) {
2828
}
2929

3030
/**
31-
* Get properties name
31+
* Get node with property's name
3232
* @param {Object} node - Property.
33-
* @returns {String} Property name.
33+
* @returns {Object} Property name node.
3434
*/
35-
function getPropertyName(node) {
35+
function getPropertyNameNode(node) {
3636
if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
37-
return node.key.name;
37+
return node.key;
3838
} else if (node.type === 'MemberExpression') {
39-
return node.property.name;
39+
return node.property;
4040
}
41-
return '';
41+
return null;
42+
}
43+
44+
/**
45+
* Get properties name
46+
* @param {Object} node - Property.
47+
* @returns {String} Property name.
48+
*/
49+
function getPropertyName(node) {
50+
const nameNode = getPropertyNameNode(node);
51+
return nameNode ? nameNode.name : '';
4252
}
4353

4454
/**
@@ -88,6 +98,7 @@ function isNodeFirstInLine(context, node) {
8898
module.exports = {
8999
findReturnStatement: findReturnStatement,
90100
getPropertyName: getPropertyName,
101+
getPropertyNameNode: getPropertyNameNode,
91102
getComponentProperties: getComponentProperties,
92103
isNodeFirstInLine: isNodeFirstInLine
93104
};

tests/lib/rules/no-deprecated.js

+84-21
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,27 @@ ruleTester.run('no-deprecated', rule, {
223223
message: errorMessage(
224224
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
225225
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
226-
)
226+
),
227+
type: 'Identifier',
228+
line: 3,
229+
column: 11
227230
},
228231
{
229232
message: errorMessage(
230233
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
231234
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
232-
)
235+
),
236+
type: 'Identifier',
237+
line: 4,
238+
column: 11
233239
},
234240
{
235241
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
236242
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
237-
)
243+
),
244+
type: 'Identifier',
245+
line: 5,
246+
column: 11
238247
}
239248
]
240249
},
@@ -253,18 +262,27 @@ ruleTester.run('no-deprecated', rule, {
253262
message: errorMessage(
254263
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
255264
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
256-
)
265+
),
266+
type: 'Identifier',
267+
line: 4,
268+
column: 13
257269
},
258270
{
259271
message: errorMessage(
260272
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
261273
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
262-
)
274+
),
275+
type: 'Identifier',
276+
line: 5,
277+
column: 13
263278
},
264279
{
265280
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
266281
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
267-
)
282+
),
283+
type: 'Identifier',
284+
line: 6,
285+
column: 13
268286
}
269287
]
270288
},
@@ -281,18 +299,27 @@ ruleTester.run('no-deprecated', rule, {
281299
message: errorMessage(
282300
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
283301
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
284-
)
302+
),
303+
type: 'Identifier',
304+
line: 3,
305+
column: 11
285306
},
286307
{
287308
message: errorMessage(
288309
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
289310
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
290-
)
311+
),
312+
type: 'Identifier',
313+
line: 4,
314+
column: 11
291315
},
292316
{
293317
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
294318
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
295-
)
319+
),
320+
type: 'Identifier',
321+
line: 5,
322+
column: 11
296323
}
297324
]
298325
},
@@ -309,18 +336,27 @@ ruleTester.run('no-deprecated', rule, {
309336
message: errorMessage(
310337
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
311338
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
312-
)
339+
),
340+
type: 'Identifier',
341+
line: 3,
342+
column: 11
313343
},
314344
{
315345
message: errorMessage(
316346
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
317347
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
318-
)
348+
),
349+
type: 'Identifier',
350+
line: 4,
351+
column: 11
319352
},
320353
{
321354
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
322355
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
323-
)
356+
),
357+
type: 'Identifier',
358+
line: 5,
359+
column: 11
324360
}
325361
]
326362
},
@@ -337,18 +373,27 @@ ruleTester.run('no-deprecated', rule, {
337373
message: errorMessage(
338374
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
339375
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
340-
)
376+
),
377+
type: 'Identifier',
378+
line: 3,
379+
column: 11
341380
},
342381
{
343382
message: errorMessage(
344383
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
345384
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
346-
)
385+
),
386+
type: 'Identifier',
387+
line: 4,
388+
column: 11
347389
},
348390
{
349391
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
350392
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
351-
)
393+
),
394+
type: 'Identifier',
395+
line: 5,
396+
column: 11
352397
}
353398
]
354399
},
@@ -365,18 +410,27 @@ ruleTester.run('no-deprecated', rule, {
365410
message: errorMessage(
366411
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
367412
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
368-
)
413+
),
414+
type: 'Identifier',
415+
line: 3,
416+
column: 11
369417
},
370418
{
371419
message: errorMessage(
372420
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
373421
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
374-
)
422+
),
423+
type: 'Identifier',
424+
line: 4,
425+
column: 11
375426
},
376427
{
377428
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
378429
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
379-
)
430+
),
431+
type: 'Identifier',
432+
line: 5,
433+
column: 11
380434
}
381435
]
382436
},
@@ -394,18 +448,27 @@ ruleTester.run('no-deprecated', rule, {
394448
message: errorMessage(
395449
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
396450
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
397-
)
451+
),
452+
type: 'Identifier',
453+
line: 4,
454+
column: 11
398455
},
399456
{
400457
message: errorMessage(
401458
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
402459
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
403-
)
460+
),
461+
type: 'Identifier',
462+
line: 5,
463+
column: 11
404464
},
405465
{
406466
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
407467
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
408-
)
468+
),
469+
type: 'Identifier',
470+
line: 6,
471+
column: 11
409472
}
410473
]
411474
}

0 commit comments

Comments
 (0)