forked from sarbbottam/eslint-find-rules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormalize-plugin-name.js
72 lines (55 loc) · 1.74 KB
/
normalize-plugin-name.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* istanbul ignore file: ignore from coverage because it varies per eslint version */
let eslintNaming;
function _getNormalizer() {
if (eslintNaming) {
return eslintNaming;
}
const eslintVersionFunctions = [
// eslint >= 7.8.0
function () {
const ESLintExports = require('eslint');
const version = ESLintExports.ESLint.version;
if (!version) {
throw new Error('Unable to find eslint version');
}
const ESLintRC = require('@eslint/eslintrc');
const naming = ESLintRC.Legacy.naming;
return {
normalizePackageName: naming.normalizePackageName,
getShorthandName: naming.getShorthandName
}
},
// eslint >= 6.1.0
function () {
const normalizer = require('eslint/lib/shared/naming');
return {
normalizePackageName: normalizer.normalizePackageName,
getShorthandName: normalizer.getShorthandName
};
},
];
for (const tryEslintVersion of eslintVersionFunctions) {
try {
const normalizer = tryEslintVersion();
if (
typeof normalizer.normalizePackageName === 'function' &&
typeof normalizer.getShorthandName === 'function'
) {
eslintNaming = {
normalizePackageName: normalizer.normalizePackageName,
getShorthandName: normalizer.getShorthandName
};
return eslintNaming;
}
} catch (err) {
}
}
throw new Error('eslint naming functions not found');
}
function normalizePluginName(name) {
const normalizer = _getNormalizer();
const module = normalizer.normalizePackageName(name, 'eslint-plugin');
const prefix = normalizer.getShorthandName(name, 'eslint-plugin');
return {module, prefix};
}
module.exports = normalizePluginName;