adding "fix:" to my custom eslint rule causes an error when trying to run "npx eslint <file_path>"

96 Views Asked by At

I created a custom eslint rule that is flagging in a ton of spots in the codebase so I want to add my own fix for the rule so that the change can be automated but it wont work.

The following code examples work fine without the "fix:" portion.

I am running eslint 8.33

        'no-restricted-syntax': [
            'error',
            {
                selector: "NewExpression[callee.name='BadObj']",
                message: "Creating a new instance of BadObj is not allowed, use newFunc() instead.",
                fix: fixer => {
                    return fixer.replaceText(
                      node,
                      "newFunc"
                    );
                },
            },
        ],
'no-restricted-syntax': [
      'error',
      {
        selector: "NewExpression[callee.name='BadObj']",
        message: "Creating a new instance of 'BadObj' is not allowed, use newFunc() instead.",
        // Add the fix function
        fix: function (fixer, node) {
          // Replace 'new Error' with 'newFunc()'
          const replacement = "newFunc()";

          // Apply the fix
          return fixer.replaceTextRange(node, replacement);
        },
      },
    ],

ERROR THROWN:

Oops! Something went wrong! :(

ESLint: 8.33.0

Error: .eslintrc.js:
        Configuration for rule "no-restricted-syntax" is invalid:
        Value {"selector":"NewExpression[callee.name='Error']","message":"Creating a new instance of 'Error' is not allowed, use TestingError.getErrorModel() instead."} should be string.
        Value {"selector":"NewExpression[callee.name='Error']","message":"Creating a new instance of 'Error' is not allowed, use TestingError.getErrorModel() instead."} should NOT have additional properties.
        Value {"selector":"NewExpression[callee.name='Error']","message":"Creating a new instance of 'Error' is not allowed, use TestingError.getErrorModel() instead."} should match exactly one schema in oneOf.

    at ConfigValidator.validateRuleOptions (<filePath>eslintrc\dist\eslintrc.cjs:2027:23)
    at <filePath>\eslintrc\dist\eslintrc.cjs:2082:18
    at Array.forEach (<anonymous>)
    at ConfigValidator.validateRules (<filePath>eslintrc\dist\eslintrc.cjs:2079:34)
    at ConfigValidator.validateConfigArray (<filePath>eslintrc\dist\eslintrc.cjs:2205:18)
    at CascadingConfigArrayFactory._finalizeConfigArray (<filePath></filePath>eslintrc\dist\eslintrc.cjs:3962:23)
    at CascadingConfigArrayFactory.getConfigArrayForFile (<filePath>\eslintrc\dist\eslintrc.cjs:3768:21)
    at FileEnumerator._iterateFilesWithFile (<filePath>eslint\lib\cli-engine\file-enumerator.js:368:43)
    at FileEnumerator._iterateFiles <filePath>\eslint\lib\cli-engine\file-enumerator.js:349:25)
    at FileEnumerator.iterateFiles (<filePath>\eslint\lib\cli-engine\file-enumerator.js:299:59)
1

There are 1 best solutions below

0
On

The ESLint no-restricted-syntax rule does not allow a fix in its configuration options. If you want to add an auto-fixer, you'll need to write a custom ESLint rule. Adding a fix property is done in custom rules.