Skip to content

Commit

Permalink
[Refactor] no-arrow-function-lifecycle, `no-unused-class-component-…
Browse files Browse the repository at this point in the history
…methods`: use report/messages convention
  • Loading branch information
ljharb committed Nov 25, 2021
1 parent 94826da commit 32f2e24
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Changed
* [Refactor] [`no-arrow-function-lifecycle`], [`no-unused-class-component-methods`]: use report/messages convention (@ljharb)

## [7.27.1] - 2021.11.18

### Fixed
Expand Down
50 changes: 30 additions & 20 deletions lib/rules/no-arrow-function-lifecycle.js
Expand Up @@ -11,6 +11,7 @@ const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const lifecycleMethods = require('../util/lifecycleMethods');
const report = require('../util/report');

function getText(node) {
const params = node.value.params.map((p) => p.name);
Expand All @@ -26,6 +27,10 @@ function getText(node) {
return null;
}

const messages = {
lifecycle: '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.',
};

module.exports = {
meta: {
docs: {
Expand All @@ -34,6 +39,7 @@ module.exports = {
recommended: false,
url: docsUrl('no-arrow-function-lifecycle'),
},
messages,
schema: [],
fixable: 'code',
},
Expand Down Expand Up @@ -95,26 +101,30 @@ module.exports = {
(previousComment.length > 0 ? previousComment[0] : body).range[0],
];

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) {
if (!sourceCode.getCommentsAfter) {
// eslint 3.x
return isBlockBody && fixer.replaceTextRange(headRange, getText(node));
}
return [].concat(
fixer.replaceTextRange(headRange, getText(node)),
isBlockBody ? [] : fixer.replaceTextRange(
bodyRange,
`{ return ${previousComment.map((x) => sourceCode.getText(x)).join('')}${sourceCode.getText(body)}${nextComment.map((x) => sourceCode.getText(x)).join('')}; }`
)
);
},
});
report(
context,
messages.lifecycle,
'lifecycle',
{
node,
data: {
propertyName,
},
fix(fixer) {
if (!sourceCode.getCommentsAfter) {
// eslint 3.x
return isBlockBody && fixer.replaceTextRange(headRange, getText(node));
}
return [].concat(
fixer.replaceTextRange(headRange, getText(node)),
isBlockBody ? [] : fixer.replaceTextRange(
bodyRange,
`{ return ${previousComment.map((x) => sourceCode.getText(x)).join('')}${sourceCode.getText(body)}${nextComment.map((x) => sourceCode.getText(x)).join('')}; }`
)
);
},
}
);
}
});
}
Expand Down
24 changes: 20 additions & 4 deletions lib/rules/no-unused-class-component-methods.js
Expand Up @@ -7,6 +7,7 @@

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -92,6 +93,11 @@ function getInitialClassInfo(node, isClass) {
};
}

const messages = {
unused: 'Unused method or property "{{name}}"',
unusedWithClass: 'Unused method or property "{{name}}" of class "{{className}}"',
};

module.exports = {
meta: {
docs: {
Expand All @@ -100,6 +106,7 @@ module.exports = {
recommended: false,
url: docsUrl('no-unused-class-component-methods'),
},
messages,
schema: [
{
type: 'object',
Expand Down Expand Up @@ -137,10 +144,19 @@ module.exports = {
) {
const className = (classInfo.classNode.id && classInfo.classNode.id.name) || '';

context.report({
node,
message: `Unused method or property "${name}"${className ? ` of class "${className}"` : ''}`,
});
const messageID = className ? 'unusedWithClass' : 'unused';
report(
context,
messages[messageID],
messageID,
{
node,
data: {
name,
className,
},
}
);
}
}
}
Expand Down

0 comments on commit 32f2e24

Please sign in to comment.