Skip to content

Commit 2575bba

Browse files
committed
[Fix] no-arrow-function-lifecycle: when converting from an arrow, remove the semi and wrapping parens
Fixes jsx-eslint#3337
1 parent b52e0ca commit 2575bba

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

88
### Fixed
99
* [`no-unknown-property`]: add `dialog` attributes ([#3436][] @ljharb)
10+
* [`no-arrow-function-lifecycle`]: when converting from an arrow, remove the semi and wrapping parens ([#3337][] @ljharb)
1011

1112
[#3436]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3436
13+
[#3337]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3337
1214

1315
## [7.31.8] - 2022.09.08
1416

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ module.exports = {
9494
}
9595
bodyRange = [
9696
(previousComment.length > 0 ? previousComment[0] : body).range[0],
97-
(nextComment.length > 0 ? nextComment[nextComment.length - 1] : body).range[1],
97+
(nextComment.length > 0 ? nextComment[nextComment.length - 1] : body).range[1]
98+
+ (node.value.body.type === 'ObjectExpression' ? 1 : 0), // to account for a wrapped end paren
9899
];
99100
}
100101
const headRange = [
101102
node.key.range[1],
102103
(previousComment.length > 0 ? previousComment[0] : body).range[0],
103104
];
105+
const hasSemi = node.value.expression && sourceCode.getText(node).slice(node.value.range[1] - node.range[0]) === ';';
104106

105107
report(
106108
context,
@@ -119,7 +121,7 @@ module.exports = {
119121
return [].concat(
120122
fixer.replaceTextRange(headRange, getText(node)),
121123
isBlockBody ? [] : fixer.replaceTextRange(
122-
bodyRange,
124+
[bodyRange[0], bodyRange[1] + (hasSemi ? 1 : 0)],
123125
`{ return ${previousComment.map((x) => sourceCode.getText(x)).join('')}${sourceCode.getText(body)}${nextComment.map((x) => sourceCode.getText(x)).join('')}; }`
124126
)
125127
);

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

+54
Original file line numberDiff line numberDiff line change
@@ -987,5 +987,59 @@ ruleTester.run('no-arrow-function-lifecycle', rule, {
987987
}
988988
`,
989989
},
990+
{
991+
code: `
992+
export default class Root extends Component {
993+
getInitialState = () => ({
994+
errorImporting: null,
995+
errorParsing: null,
996+
errorUploading: null,
997+
file: null,
998+
fromExtension: false,
999+
importSuccess: false,
1000+
isImporting: false,
1001+
isParsing: false,
1002+
isUploading: false,
1003+
parsedResults: null,
1004+
showLongRunningMessage: false,
1005+
});
1006+
}
1007+
`,
1008+
features: ['class fields'],
1009+
errors: [{ message: 'getInitialState is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' }],
1010+
output: semver.satisfies(eslintPkg.version, '> 3') ? `
1011+
export default class Root extends Component {
1012+
getInitialState() { return {
1013+
errorImporting: null,
1014+
errorParsing: null,
1015+
errorUploading: null,
1016+
file: null,
1017+
fromExtension: false,
1018+
importSuccess: false,
1019+
isImporting: false,
1020+
isParsing: false,
1021+
isUploading: false,
1022+
parsedResults: null,
1023+
showLongRunningMessage: false,
1024+
}; }
1025+
}
1026+
` : `
1027+
export default class Root extends Component {
1028+
getInitialState = () => ({
1029+
errorImporting: null,
1030+
errorParsing: null,
1031+
errorUploading: null,
1032+
file: null,
1033+
fromExtension: false,
1034+
importSuccess: false,
1035+
isImporting: false,
1036+
isParsing: false,
1037+
isUploading: false,
1038+
parsedResults: null,
1039+
showLongRunningMessage: false,
1040+
});
1041+
}
1042+
`,
1043+
},
9901044
]),
9911045
});

0 commit comments

Comments
 (0)