Skip to content

Commit 32f2e24

Browse files
committed
[Refactor] no-arrow-function-lifecycle, no-unused-class-component-methods: use report/messages convention
1 parent 94826da commit 32f2e24

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Changed
9+
* [Refactor] [`no-arrow-function-lifecycle`], [`no-unused-class-component-methods`]: use report/messages convention (@ljharb)
10+
811
## [7.27.1] - 2021.11.18
912

1013
### Fixed

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Components = require('../util/Components');
1111
const astUtil = require('../util/ast');
1212
const docsUrl = require('../util/docsUrl');
1313
const lifecycleMethods = require('../util/lifecycleMethods');
14+
const report = require('../util/report');
1415

1516
function getText(node) {
1617
const params = node.value.params.map((p) => p.name);
@@ -26,6 +27,10 @@ function getText(node) {
2627
return null;
2728
}
2829

30+
const messages = {
31+
lifecycle: '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.',
32+
};
33+
2934
module.exports = {
3035
meta: {
3136
docs: {
@@ -34,6 +39,7 @@ module.exports = {
3439
recommended: false,
3540
url: docsUrl('no-arrow-function-lifecycle'),
3641
},
42+
messages,
3743
schema: [],
3844
fixable: 'code',
3945
},
@@ -95,26 +101,30 @@ module.exports = {
95101
(previousComment.length > 0 ? previousComment[0] : body).range[0],
96102
];
97103

98-
context.report({
99-
node,
100-
message: '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.',
101-
data: {
102-
propertyName,
103-
},
104-
fix(fixer) {
105-
if (!sourceCode.getCommentsAfter) {
106-
// eslint 3.x
107-
return isBlockBody && fixer.replaceTextRange(headRange, getText(node));
108-
}
109-
return [].concat(
110-
fixer.replaceTextRange(headRange, getText(node)),
111-
isBlockBody ? [] : fixer.replaceTextRange(
112-
bodyRange,
113-
`{ return ${previousComment.map((x) => sourceCode.getText(x)).join('')}${sourceCode.getText(body)}${nextComment.map((x) => sourceCode.getText(x)).join('')}; }`
114-
)
115-
);
116-
},
117-
});
104+
report(
105+
context,
106+
messages.lifecycle,
107+
'lifecycle',
108+
{
109+
node,
110+
data: {
111+
propertyName,
112+
},
113+
fix(fixer) {
114+
if (!sourceCode.getCommentsAfter) {
115+
// eslint 3.x
116+
return isBlockBody && fixer.replaceTextRange(headRange, getText(node));
117+
}
118+
return [].concat(
119+
fixer.replaceTextRange(headRange, getText(node)),
120+
isBlockBody ? [] : fixer.replaceTextRange(
121+
bodyRange,
122+
`{ return ${previousComment.map((x) => sourceCode.getText(x)).join('')}${sourceCode.getText(body)}${nextComment.map((x) => sourceCode.getText(x)).join('')}; }`
123+
)
124+
);
125+
},
126+
}
127+
);
118128
}
119129
});
120130
}

lib/rules/no-unused-class-component-methods.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const Components = require('../util/Components');
99
const docsUrl = require('../util/docsUrl');
10+
const report = require('../util/report');
1011

1112
// ------------------------------------------------------------------------------
1213
// Rule Definition
@@ -92,6 +93,11 @@ function getInitialClassInfo(node, isClass) {
9293
};
9394
}
9495

96+
const messages = {
97+
unused: 'Unused method or property "{{name}}"',
98+
unusedWithClass: 'Unused method or property "{{name}}" of class "{{className}}"',
99+
};
100+
95101
module.exports = {
96102
meta: {
97103
docs: {
@@ -100,6 +106,7 @@ module.exports = {
100106
recommended: false,
101107
url: docsUrl('no-unused-class-component-methods'),
102108
},
109+
messages,
103110
schema: [
104111
{
105112
type: 'object',
@@ -137,10 +144,19 @@ module.exports = {
137144
) {
138145
const className = (classInfo.classNode.id && classInfo.classNode.id.name) || '';
139146

140-
context.report({
141-
node,
142-
message: `Unused method or property "${name}"${className ? ` of class "${className}"` : ''}`,
143-
});
147+
const messageID = className ? 'unusedWithClass' : 'unused';
148+
report(
149+
context,
150+
messages[messageID],
151+
messageID,
152+
{
153+
node,
154+
data: {
155+
name,
156+
className,
157+
},
158+
}
159+
);
144160
}
145161
}
146162
}

0 commit comments

Comments
 (0)