Mayil-AI-Sandbox / eslint-v911

Clone of eslint v9.1.1 for debugging
MIT License
0 stars 0 forks source link

Bug: object-shorthand loses type parameters when auto-fixing #7

Open btmills opened 1 month ago

btmills commented 1 month ago

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:

const testObj = {
  myMethod: <T>(obj: T): T => {
    return obj;
  },
};

What did you expect to happen?

const testObj = {
  myMethod<T>(obj: T): T {
    return obj;
  },
};

What actually happened?

const testObj = {
  myMethod(obj: T): T {
    return obj;
  },
};

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.

mayil-ai[bot] commented 1 month ago

To address the issue with the object-shorthand rule in ESLint incorrectly removing type parameters during autofix, follow these steps:

  1. Identify if an arrow function has type parameters by checking the typeParameters property of the arrow function node.

  2. 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.

Code snippets to check