Skip to content

Commit

Permalink
Merge pull request #1854 from jsnajdr/lifecycle-method-node-ident
Browse files Browse the repository at this point in the history
For deprecated lifecycle methods, report identifier AST node instead of the class node
  • Loading branch information
ljharb committed Jun 28, 2018
2 parents 20779ff + 248c2ca commit f27285f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 36 deletions.
21 changes: 12 additions & 9 deletions lib/rules/no-deprecated.js
Expand Up @@ -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}` : ''
Expand Down Expand Up @@ -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)
}));
}

/**
Expand All @@ -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));
}
}

Expand Down
23 changes: 17 additions & 6 deletions lib/util/ast.js
Expand Up @@ -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 : '';
}

/**
Expand Down Expand Up @@ -88,6 +98,7 @@ function isNodeFirstInLine(context, node) {
module.exports = {
findReturnStatement: findReturnStatement,
getPropertyName: getPropertyName,
getPropertyNameNode: getPropertyNameNode,
getComponentProperties: getComponentProperties,
isNodeFirstInLine: isNodeFirstInLine
};
105 changes: 84 additions & 21 deletions tests/lib/rules/no-deprecated.js
Expand Up @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
}
Expand Down

0 comments on commit f27285f

Please sign in to comment.