Open btmills opened 1 month ago
To address the issue with the object-shorthand
rule in ESLint incorrectly removing type parameters during autofix, follow these steps:
Identify if an arrow function has type parameters by checking the typeParameters
property of the arrow function node.
Preserve the type parameters in the transformed method shorthand syntax by including them in the autofix output.
Make the following changes to the lib/rules/object-shorthand.js
file:
Here is the modified code snippet for the lib/rules/object-shorthand.js
file:
// lib/rules/object-shorthand.js
// ... (other code)
if (node.value.type === "ArrowFunctionExpression" &&
node.value.body.type === "BlockStatement" &&
AVOID_EXPLICIT_RETURN_ARROWS &&
!arrowsWithLexicalIdentifiers.has(node.value)
) {
// Check if the arrow function has type parameters
const typeParameters = node.value.typeParameters;
const typeParametersText = typeParameters ? sourceCode.getText(typeParameters) : '';
context.report({
node,
messageId: "expectedMethodShorthand",
fix: fixer => {
const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);
// Include type parameters in the method shorthand
const methodPrefix = keyPrefix + keyText + typeParametersText;
return fixer.replaceTextRange(
fixRange,
methodPrefix + fnBody
);
}
});
}
// ... (other code)
By implementing these changes, the autofix functionality should now correctly handle TypeScript type parameters and maintain them when converting arrow functions to method shorthand.
lib/rules/object-shorthand.js:463-516 | Snippet contains logic for shorthand method syntax fixing, relevant to the TypeScript generic method issue.
lib/rules/arrow-body-style.js:149-293 | Related to object literal return fix, may need adjustment for TypeScript generics.
lib/rules/arrow-parens.js:133-185 | Handles parentheses in arrow functions, could be relevant for generic type parameter preservation.
lib/rules/prefer-numeric-literals.js:87-144 | Not directly related to the issue, but showcases autofix logic which might provide insights for the fix.
Environment
Node version: v18.19.0 npm version: v10.7.0 Local ESLint version: v8.57.0 (Currently used) Global ESLint version: Not found Operating System: darwin 23.4.0
What parser are you using?
@typescript-eslint/parser
What did you do?
Configuration
``` module.exports = { parser: "@typescript-eslint/parser", rules: { "object-shorthand": [ "error", "always", { avoidExplicitReturnArrows: true }, ], }, }; ```Run auto-fix on this:
What did you expect to happen?
What actually happened?
Link to Minimal Reproducible Example
https://typescript-eslint.io/play/#ts=5.4.5&fileType=.ts&code=MYewdgzgLgBFCm0DyAjAVjAvDA3gKBhgFsBPAWXigAsQATALhgB4AVAPgAoR1GWBKXlja4ChGACdKAV3FgY3NAG5RAXwA0eFcqA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQPYCMArRAYwBcBaZAC12jOoEMA7AE3TAG1xsdFpodSABoevSI3gB3RgE9Uo3hCxKcjAG64AlqwCiADwAO8LSS1kASojKxozAIIDcU1BjJxEY7AF8vAXR5fbyA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Participation
Additional comments
First reported here.