diff --git a/lib/rules/no-deprecated.js b/lib/rules/no-deprecated.js index c5dee54f5a..f5499deb7b 100644 --- a/lib/rules/no-deprecated.js +++ b/lib/rules/no-deprecated.js @@ -106,19 +106,19 @@ module.exports = { ); } - function checkDeprecation(node, method) { - if (!isDeprecated(method)) { + function checkDeprecation(node, methodName, methodNode) { + if (!isDeprecated(methodName)) { return; } const deprecated = getDeprecated(); - const version = deprecated[method][0]; - const newMethod = deprecated[method][1]; - const refs = deprecated[method][2]; + const version = deprecated[methodName][0]; + const newMethod = deprecated[methodName][1]; + const refs = deprecated[methodName][2]; context.report({ - node: node, + node: methodNode || node, message: DEPRECATED_MESSAGE, data: { - oldMethod: method, + oldMethod: methodName, version, newMethod: newMethod ? `, use ${newMethod} instead` : '', refs: refs ? `, see ${refs}` : '' @@ -150,7 +150,10 @@ module.exports = { */ function getLifeCycleMethods(node) { const properties = astUtil.getComponentProperties(node); - return properties.map(property => astUtil.getPropertyName(property)); + return properties.map(property => ({ + name: astUtil.getPropertyName(property), + node: astUtil.getPropertyNameNode(property) + })); } /** @@ -160,7 +163,7 @@ module.exports = { function checkLifeCycleMethods(node) { if (utils.isES5Component(node) || utils.isES6Component(node)) { const methods = getLifeCycleMethods(node); - methods.forEach(method => checkDeprecation(node, method)); + methods.forEach(method => checkDeprecation(node, method.name, method.node)); } } diff --git a/lib/util/ast.js b/lib/util/ast.js index 6161cb4513..a2c7696ea6 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -28,17 +28,27 @@ function findReturnStatement(node) { } /** - * Get properties name + * Get node with property's name * @param {Object} node - Property. - * @returns {String} Property name. + * @returns {Object} Property name node. */ -function getPropertyName(node) { +function getPropertyNameNode(node) { if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) { - return node.key.name; + return node.key; } else if (node.type === 'MemberExpression') { - return node.property.name; + return node.property; } - return ''; + return null; +} + +/** + * Get properties name + * @param {Object} node - Property. + * @returns {String} Property name. + */ +function getPropertyName(node) { + const nameNode = getPropertyNameNode(node); + return nameNode ? nameNode.name : ''; } /** @@ -88,6 +98,7 @@ function isNodeFirstInLine(context, node) { module.exports = { findReturnStatement: findReturnStatement, getPropertyName: getPropertyName, + getPropertyNameNode: getPropertyNameNode, getComponentProperties: getComponentProperties, isNodeFirstInLine: isNodeFirstInLine }; diff --git a/tests/lib/rules/no-deprecated.js b/tests/lib/rules/no-deprecated.js index 2472ce5848..12ab519904 100644 --- a/tests/lib/rules/no-deprecated.js +++ b/tests/lib/rules/no-deprecated.js @@ -223,18 +223,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 3, + column: 11 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 4, + column: 11 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 5, + column: 11 } ] }, @@ -253,18 +262,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 4, + column: 13 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 5, + column: 13 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 6, + column: 13 } ] }, @@ -281,18 +299,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 3, + column: 11 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 4, + column: 11 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 5, + column: 11 } ] }, @@ -309,18 +336,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 3, + column: 11 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 4, + column: 11 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 5, + column: 11 } ] }, @@ -337,18 +373,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 3, + column: 11 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 4, + column: 11 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 5, + column: 11 } ] }, @@ -365,18 +410,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 3, + column: 11 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 4, + column: 11 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 5, + column: 11 } ] }, @@ -394,18 +448,27 @@ ruleTester.run('no-deprecated', rule, { message: errorMessage( 'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount' - ) + ), + type: 'Identifier', + line: 4, + column: 11 }, { message: errorMessage( 'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops' - ) + ), + type: 'Identifier', + line: 5, + column: 11 }, { message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate', 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate' - ) + ), + type: 'Identifier', + line: 6, + column: 11 } ] }