wikimedia / eslint-docgen

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

Monorepo support #141

Open drjonnicholson opened 3 months ago

drjonnicholson commented 3 months ago

Hey all, looks like a fab tool but having issues in a monorepo setup.

In our case ESLint is in the root node_modules folder, so there isn't a local ESLint installed within the package's folder, causing a "Cannot find module" error

image

Have overcome this in my project by creating my own RuleTester extension that's a carbon copy, with modifications to the requires etc.. Here's what it looks like:

const ESLintRuleTester = require('eslint').RuleTester
const inDocMode = !!process.env.DOCGEN

/**
 * Extends ESLint's RuleTester to also build documentation
 * This is a small edit to https://github.com/wikimedia/eslint-docgen/blob/master/src/rule-tester.js, which doesn't work well with monorepos
 */
class RuleTester extends ESLintRuleTester {
  run(name, rule, tests) {
    if (inDocMode) {
      RuleTester.it(name, (done) => {
        const writeDocsFromTests = require('eslint-docgen/src/write-docs-from-tests')
        writeDocsFromTests(name, rule, tests, this.testerConfig, done)
      })
    } else {
      // Filter out invalid property "docgen"
      // (used in documentation building mode).
      tests.valid.forEach((test) => {
        delete test.docgen
      })
      tests.invalid.forEach((test) => {
        delete test.docgen
      })

      // Filter out invalid top level property "docgenConfig"
      // (used in documentation building mode).
      delete tests.docgenConfig

      return super.run.call(this, name, rule, tests)
    }
  }
}

module.exports = RuleTester

Less a bug and more something to document, or provide a version that uses global ESLint when the package one isn't desirable?