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