Skip to content

Commit

Permalink
auto fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtan committed Sep 19, 2021
1 parent b9bf156 commit 6b5aa3a
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 37 deletions.
22 changes: 20 additions & 2 deletions lib/rules/no-arrow-function-lifecycle.js
Expand Up @@ -23,22 +23,40 @@ module.exports = {
},

create: Components.detect((context, components) => {
function getText(node) {
const params = node.value.params.map((p) => p.name);

if (node.type === 'Property') {
return `: function(${params.join(', ')}) `;
}

if (node.type === 'ClassProperty') {
return `(${params.join(', ')}) `;
}

return null;
}

/**
* @param {Array} properties list of component properties
*/
function reportNoArrowFunctionLifecycle(properties) {
properties.forEach((node) => {
const propertyName = astUtil.getPropertyName(node);
const nodeType = node.value && node.value.type;
const nodeType = node.value.type;
const isLifecycleMethod = lifecycleMethods.includes(propertyName);

if (nodeType === 'ArrowFunctionExpression' && isLifecycleMethod) {
const range = [node.key.range[1], node.value.body.range[0]];
const text = getText(node);

context.report({
node,
message: '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.',
data: {
propertyName
}
},
fix: (fixer) => fixer.replaceTextRange(range, text)
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-typos.js
Expand Up @@ -163,7 +163,7 @@ module.exports = {
}
});

lifecycleMethods.forEach(method => {
lifecycleMethods.forEach((method) => {
if (method.toLowerCase() === nodeKeyName.toLowerCase() && method !== nodeKeyName) {
context.report({
node,
Expand Down

0 comments on commit 6b5aa3a

Please sign in to comment.