eslint / eslint

Find and fix problems in your JavaScript code.
https://eslint.org
MIT License
24.39k stars 4.4k forks source link

Docs: custom rule tutorial doesn't work with v9 #18382

Closed Keavon closed 3 weeks ago

Keavon commented 3 weeks ago

Docs page(s)

https://eslint.org/docs/latest/extend/custom-rule-tutorial

What documentation issue do you want to solve?

Following the documentation, this error occurs seemingly hinting at the new ESLint v9 flat config format change. Things work when downgrading to ESLint v8 with npm install eslint@8 --save-dev.

$ npm test

> eslint-custom-rule-example@1.0.0 test
> node enforce-foo-bar.test.js

C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:382
        throw err;
        ^

ConfigError: ESLint configuration in rule-tester is invalid: Config (unnamed): Key "parserOptions": This appears to be in eslintrc format rather than flat config format.
    at rethrowConfigError (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\config-array\api.js:230:8)
    at C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\config-array\api.js:1032:5
    at Array.reduce (<anonymous>)
    at FlatConfigArray.getConfig (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\config-array\api.js:1028:39)
    ... 2 lines matching cause stack trace ...
    at RuleTester.<anonymous> (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:1258:33)
    at RuleTester.itDefaultHandler (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:377:23)
    at C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:1255:71
    at Array.forEach (<anonymous>) {
  messageTemplate: 'eslintrc-incompat',
  messageData: { key: 'parserOptions' },
  index: 6,
  [cause]: WrapperError: Key "parserOptions": This appears to be in eslintrc format rather than flat config format.
      at ObjectSchema.validate (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\object-schema\src\object-schema.js:287:23)
      at C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\object-schema\src\object-schema.js:239:18
      ... 6 lines matching cause stack trace ...
      at testValidTemplate (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:908:28)
      at RuleTester.<anonymous> (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:1258:33) {
    messageTemplate: 'eslintrc-incompat',
    messageData: { key: 'parserOptions' },
    [cause]: IncompatibleKeyError: This appears to be in eslintrc format rather than flat config format.
        at Object.validate (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\config\flat-config-schema.js:536:19)
        at ObjectSchema.validate (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\object-schema\src\object-schema.js:285:35)
        at C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\object-schema\src\object-schema.js:239:18
        at Array.reduce (<anonymous>)
        at ObjectSchema.merge (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\object-schema\src\object-schema.js:237:24)
        at C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\config-array\api.js:1030:43
        at Array.reduce (<anonymous>)
        at FlatConfigArray.getConfig (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\@humanwhocodes\config-array\api.js:1028:39)
        at runRuleForItem (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:793:25)
        at testValidTemplate (C:\Users\Keavon\Projects\Graphite\eslint-custom-rule-example\node_modules\eslint\lib\rule-tester\rule-tester.js:908:28) {
      messageTemplate: 'eslintrc-incompat',
      messageData: { key: 'parserOptions' }
    }
  }
}

Node.js v18.12.1

What do you think is the correct solution?

I'm not familiar with how the flat format change affects custom rules, and I'm stuck here. Someone knowledgeable should probably make the necessary changes to publish the correct way of making custom rules in this tutorial.

Participation

Additional comments

No response

aladdin-add commented 3 weeks ago

@Keavon thanks for the report! I've made a fix: https://github.com/eslint/eslint/pull/18383