Skip to content

Commit 6b5aa3a

Browse files
committed
auto fix
1 parent b9bf156 commit 6b5aa3a

File tree

3 files changed

+274
-37
lines changed

3 files changed

+274
-37
lines changed

lib/rules/no-arrow-function-lifecycle.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,40 @@ module.exports = {
2323
},
2424

2525
create: Components.detect((context, components) => {
26+
function getText(node) {
27+
const params = node.value.params.map((p) => p.name);
28+
29+
if (node.type === 'Property') {
30+
return `: function(${params.join(', ')}) `;
31+
}
32+
33+
if (node.type === 'ClassProperty') {
34+
return `(${params.join(', ')}) `;
35+
}
36+
37+
return null;
38+
}
39+
2640
/**
2741
* @param {Array} properties list of component properties
2842
*/
2943
function reportNoArrowFunctionLifecycle(properties) {
3044
properties.forEach((node) => {
3145
const propertyName = astUtil.getPropertyName(node);
32-
const nodeType = node.value && node.value.type;
46+
const nodeType = node.value.type;
3347
const isLifecycleMethod = lifecycleMethods.includes(propertyName);
3448

3549
if (nodeType === 'ArrowFunctionExpression' && isLifecycleMethod) {
50+
const range = [node.key.range[1], node.value.body.range[0]];
51+
const text = getText(node);
52+
3653
context.report({
3754
node,
3855
message: '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.',
3956
data: {
4057
propertyName
41-
}
58+
},
59+
fix: (fixer) => fixer.replaceTextRange(range, text)
4260
});
4361
}
4462
});

lib/rules/no-typos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module.exports = {
163163
}
164164
});
165165

166-
lifecycleMethods.forEach(method => {
166+
lifecycleMethods.forEach((method) => {
167167
if (method.toLowerCase() === nodeKeyName.toLowerCase() && method !== nodeKeyName) {
168168
context.report({
169169
node,

0 commit comments

Comments
 (0)