Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For deprecated lifecycle methods, report identifier AST node instead of the class node #1854

Merged
merged 3 commits into from Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
};
65 changes: 43 additions & 22 deletions tests/lib/rules/no-deprecated.js
Expand Up @@ -223,18 +223,21 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
},
Expand All @@ -253,18 +256,21 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
},
Expand All @@ -281,18 +287,21 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
},
Expand All @@ -309,18 +318,21 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
},
Expand All @@ -337,18 +349,21 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
},
Expand All @@ -365,23 +380,26 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
},
{
code: `
code: `un
class Foo extends React.Component {
constructor() {}
componentWillMount() {}
Expand All @@ -394,18 +412,21 @@ ruleTester.run('no-deprecated', rule, {
message: errorMessage(
'componentWillMount', '16.3.0', 'UNSAFE_componentWillMount',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
)
),
type: 'Identifier'
},
{
message: errorMessage(
'componentWillReceiveProps', '16.3.0', 'UNSAFE_componentWillReceiveProps',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
)
),
type: 'Identifier'
},
{
message: errorMessage('componentWillUpdate', '16.3.0', 'UNSAFE_componentWillUpdate',
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
)
),
type: 'Identifier'
}
]
}
Expand Down