简体   繁体   中英

Forking core eslint rules

I want to do some minor edits to core eslint rules, eg array-bracket-newline , or indent . These rules often depend on utilities inside eslint , most commonly ast-utils . So far, i've used a plugin, added the modified rules there, and did a require('eslint/lib/rules/utils/ast-utils') , as eslint is a peer-dependency anyways.

Since https://github.com/eslint/eslint/commit/24c9f2ac57efcd699ca69695c82e51ce5742df7b this is no longer possible, as an exports directive was added to the package.json . What is the usual method for changing behavior of core eslint rules nowadays?

  • copying out all dependencies would be possible, but both tedious, and duplicating code for no reason (i'd have to trace all the dependencies, and rip out chunks of eslint's code).
  • forking eslint as a whole seems unclean, as there are a lot of other parts, which depend on it (starting from eslint-plugins, over to vscode extensions, yarn sdks, ...). Each one would need to be changed, or some very dirty rename used, in which the fork pretends to be the original (accident waiting to happen).
  • yarn package patching the exports away seems really dirty.

Is there some clean way?


Edit: my current best idea is forking eslint , removing the exports , and then using require('eslint-fork/lib/rules/utils/ast-utils') on the fork. This means i need an extra eslint copy for no real reason, but it's for linting, and a bit of disk space isn't important.

It is possible to require unexported files by their full path, ie:

const { dirname, join } = require('path');
const astUtilsPath = join(dirname(require.resolve('eslint')), 'rules/utils/ast-utils.js');
const astUtils = require(astUtilsPath);

Note that this method relies on the main export being located in a particular package folder ('lib' in the case of eslint).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM