wikimedia / eslint-docgen

Automatically generate ESLint plugin documentation from rule metadata and test cases.
MIT License
10 stars 7 forks source link

eslint-docgen's RuleTester fails, but eslint version works fine #131

Closed monkpit closed 2 years ago

monkpit commented 2 years ago

I really wanted to use this package and I tried for a few days on and off to get it to work, but I keep running into this error:

$ DOCGEN=1 yarn test --detectOpenHandles
< ... >
 /path/to/my/rule.md
      error  Configured parser '/path/to/my/project/node_modules/@babel/eslint-parser/lib/index.cjs' was not found.:
             undefined

      at writeDocsFromTests (node_modules/eslint-docgen/src/write-docs-from-tests.js:86:15)

@babel/eslint-parser is used by one of the dependencies of my eslint plugin (an internal work thing) so it's reasonable that it would show up as the parser here.

The path that is given in the error message is valid and leads to @babel/eslint-parser as expected if I try to open it (meaning it's certainly able to be found :) ).

I opened up the source of write-docs-from-tests.js and added some more logging around the error - the full stack trace is like this:

Error: Configured parser '/path/to/my/project/node_modules/@babel/eslint-parser/lib/index.cjs' was not found.:
        undefined
            at lintFix (/path/to/my/project/node_modules/eslint-docgen/src/fix.js:55:15)
            at Object.batchLintFix (/path/to/my/project/node_modules/eslint-docgen/src/fix.js:74:19)
            at buildRuleDetails (/path/to/my/project/node_modules/eslint-docgen/src/build-docs-from-tests.js:94:16)
            at buildDocsFromTests (/path/to/my/project/node_modules/eslint-docgen/src/build-docs-from-tests.js:271:18)
            at writeDocsFromTests (/path/to/my/project/node_modules/eslint-docgen/src/write-docs-from-tests.js:73:37)

If I change RuleTester to the eslint version, like const {RuleTester} = require('eslint'); then the tests run fine and there's no issue. But then there's no docs :) unless I write them or copy/paste rule descriptions into markdown myself... which is why I would love to be able to use this package!

edg2s commented 2 years ago

Can you link to a work in progress branch so I can have a look?

monkpit commented 2 years ago

@edg2s Hey, thanks for the quick reply! I actually can't since it's for work. I'll try to create a minimal repo where it can be reproduced.

monkpit commented 2 years ago

@edg2s

Here you go: https://github.com/monkpit/docgen-issue-repro

From my experimenting, it seems it's triggered by having an .eslintrc.json in my project root, that is extending another config inside node_modules.

I added node_modules/fake-module to demonstrate it.

Clone the repo, install the deps (which should also install deps inside of fake-module automatically), and then do npm run docs.

In my original repo that I was having this issue in, I tried removing the reference to extends: 'node_modules/...' and the error from my OP goes away.

I'm not sure why eslint-docgen is even seeing my local project's .eslintrc.json anyways? I am passing in the rule directly, it shouldn't need to check my local config for my project.

edg2s commented 2 years ago

Hmm, reaching into node_modules to get an ELint config seems like undocumented behaviour. I would guess that ESLint recommends you expose the shared config properly?

edg2s commented 2 years ago

You should be able to test with: npm i --save-dev wikimedia/eslint-docgen#custom-parser

If this fixes your issue I will merge and do a minor release.

monkpit commented 2 years ago

It works flawlessly! 🎉 Thanks!