getBuiltinRule.js.flow 1.1 KB

1234567891011121314151617181920212223242526
  1. /**
  2. * This is used to pull the definition of a builtin rule from eslint.
  3. *
  4. * Adopted from https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/rules/utils/get-builtin-rule.js.
  5. */
  6. export const getBuiltinRule = (id) => {
  7. // TODO: Remove this when we drop support for ESLint 7
  8. const eslintVersion = require('eslint/package.json').version;
  9. if (eslintVersion.startsWith('7.')) {
  10. return require(`eslint/lib/rules/${id}`);
  11. }
  12. // In eslint 8 and beyond using https://nodejs.org/api/packages.html#subpath-exports
  13. // eslint has defined public exported paths and has locked the rest of the
  14. // directory as private.
  15. //
  16. // Though there is an issue when run with `jest` apparently where it does not support ESM.
  17. // So we're gonna do it the same old fashion way if it crashes when requiring.
  18. // ref: https://github.com/typescript-eslint/typescript-eslint/issues/4210#issuecomment-981203332
  19. try {
  20. // eslint-disable-next-line import/no-unresolved
  21. return require('eslint/use-at-your-own-risk').builtinRules.get(id);
  22. } catch (e) {
  23. return require(`eslint/lib/rules/${id}`);
  24. }
  25. };