Skip to content

Commit 20f0fc1

Browse files
committed
chore: abstract utilities
1 parent 59d18a6 commit 20f0fc1

File tree

2 files changed

+214
-199
lines changed

2 files changed

+214
-199
lines changed

compare/compare.js

Lines changed: 6 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -8,205 +8,12 @@ const {
88
resolve,
99
} = require('path');
1010
const {
11-
ESLint,
12-
} = require('eslint');
13-
const {
14-
builtinRules,
15-
} = require('eslint/use-at-your-own-risk');
16-
17-
/**
18-
* Determines what rules are going to be used for a given ESLint configuration.
19-
*/
20-
const getConfigurationRules = async (configuration) => {
21-
const engine = new ESLint({
22-
baseConfig: configuration,
23-
useEslintrc: false,
24-
});
25-
26-
return (await engine.calculateConfigForFile('./compare')).rules;
27-
};
28-
29-
const getConfigurationPluginNames = async (configuration) => {
30-
const engine = new ESLint({
31-
baseConfig: configuration,
32-
useEslintrc: false,
33-
});
34-
35-
return (await engine.calculateConfigForFile('./compare')).plugins;
36-
};
37-
38-
const getPluginRules = (pluginName) => {
39-
// eslint-disable-next-line import/no-dynamic-require
40-
const {rules} = require(pluginName.startsWith('@') ? pluginName + '/eslint-plugin' : 'eslint-plugin-' + pluginName);
41-
42-
return Object.fromEntries(Object.entries(rules).map(([ruleName, ruleConfiguration]) => {
43-
return [pluginName + '/' + ruleName, ruleConfiguration];
44-
}));
45-
};
46-
47-
const getRuleLink = (ruleName) => {
48-
if (!ruleName.includes('/')) {
49-
return '[`' + ruleName + '`](https://eslint.org/docs/rules/' + ruleName + ')';
50-
}
51-
52-
if (ruleName.startsWith('fp/')) {
53-
return '[`' + ruleName + '`](https://github.com/jfmengels/eslint-plugin-fp/blob/master/docs/rules/' + ruleName.replace(/^fp\//u, '') + '.md)';
54-
}
55-
56-
if (ruleName.startsWith('ava/')) {
57-
return '[`' + ruleName + '`](https://github.com/avajs/eslint-plugin-ava/blob/master/docs/rules/' + ruleName.replace(/^ava\//u, '') + '.md)';
58-
}
59-
60-
if (ruleName.startsWith('canonical/')) {
61-
return '[`' + ruleName + '`](https://github.com/gajus/eslint-plugin-canonical#eslint-plugin-canonical-rules-' + ruleName.replace(/^canonical\//u, '') + ')';
62-
}
63-
64-
if (ruleName.startsWith('eslint-comments/')) {
65-
return '[`' + ruleName + '`](https://github.com/mysticatea/eslint-plugin-eslint-comments/blob/master/docs/rules/' + ruleName.replace(/^eslint-comments\//u, '') + '.md)';
66-
}
67-
68-
if (ruleName.startsWith('unicorn/')) {
69-
return '[`' + ruleName + '`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/' + ruleName.replace(/^unicorn\//u, '') + '.md)';
70-
}
71-
72-
if (ruleName.startsWith('flowtype/')) {
73-
return '[`' + ruleName + '`](https://github.com/gajus/eslint-plugin-flowtype/#eslint-plugin-flowtype-rules-' + ruleName.replace(/^flowtype\//u, '') + ')';
74-
}
75-
76-
if (ruleName.startsWith('jsdoc/')) {
77-
return '[`' + ruleName + '`](https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-' + ruleName.replace(/^jsdoc\//u, '') + ')';
78-
}
79-
80-
if (ruleName.startsWith('import/')) {
81-
return '[`' + ruleName + '`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/' + ruleName.replace(/^import\//u, '') + '.md)';
82-
}
83-
84-
if (ruleName.startsWith('react/')) {
85-
return '[`' + ruleName + '`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/' + ruleName.replace(/^react\//u, '') + '.md)';
86-
}
87-
88-
if (ruleName.startsWith('promise/')) {
89-
return '[`' + ruleName + '`](https://github.com/xjamundx/eslint-plugin-promise/blob/master/docs/rules/' + ruleName.replace(/^promise\//u, '') + '.md)';
90-
}
91-
92-
if (ruleName.startsWith('lodash/')) {
93-
return '[`' + ruleName + '`](https://github.com/wix/eslint-plugin-lodash/blob/master/docs/rules/' + ruleName.replace(/^lodash\//u, '') + '.md)';
94-
}
95-
96-
if (ruleName.startsWith('mocha/')) {
97-
return '[`' + ruleName + '`](https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/' + ruleName.replace(/^mocha\//u, '') + '.md)';
98-
}
99-
100-
if (ruleName.startsWith('node/')) {
101-
return '[`' + ruleName + '`](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/' + ruleName.replace(/^node\//u, '') + '.md)';
102-
}
103-
104-
if (ruleName.startsWith('jsx-a11y/')) {
105-
return '[`' + ruleName + '`](https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/' + ruleName.replace(/^jsx-a11y\//u, '') + '.md)';
106-
}
107-
108-
if (ruleName.startsWith('jest/')) {
109-
return '[`' + ruleName + '`](https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/' + ruleName.replace(/^jest\//u, '') + '.md)';
110-
}
111-
112-
if (ruleName.startsWith('jsonc/')) {
113-
return '[`' + ruleName + '`](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/' + ruleName.replace(/^jsonc\//u, '') + '.html)';
114-
}
115-
116-
if (ruleName.startsWith('@typescript-eslint/')) {
117-
// eslint-disable-next-line max-len
118-
return '[`' + ruleName + '`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/' + ruleName.replace(/^@typescript-eslint\//u, '') + '.md)';
119-
}
120-
121-
if (ruleName.startsWith('yml/')) {
122-
return '[`' + ruleName + '`](https://ota-meshi.github.io/eslint-plugin-yml/rules/' + ruleName.replace(/^yml\//u, '') + '.html)';
123-
}
124-
125-
return '`' + ruleName + '`';
126-
};
127-
128-
const isRuleEnabled = (ruleValue) => {
129-
if (ruleValue === 1 || ruleValue === 'warn') {
130-
return true;
131-
}
132-
133-
if (ruleValue === 2 || ruleValue === 'error') {
134-
return true;
135-
}
136-
137-
return false;
138-
};
139-
140-
const describeRuleValue = (ruleValue) => {
141-
if (ruleValue === undefined) {
142-
return '👻';
143-
}
144-
145-
if (ruleValue === 0 || ruleValue === 'off') {
146-
return '❌';
147-
}
148-
149-
if (ruleValue === 1 || ruleValue === 'warn') {
150-
return '⚠️';
151-
}
152-
153-
if (ruleValue === 2 || ruleValue === 'error') {
154-
return '🚨';
155-
}
156-
157-
return false;
158-
};
159-
160-
const getRuleConfiguration = (ruleset, ruleName) => {
161-
const ruleValueDescription = describeRuleValue(ruleset[ruleName]);
162-
163-
if (ruleValueDescription) {
164-
return ruleValueDescription;
165-
}
166-
167-
return describeRuleValue(ruleset[ruleName][0]);
168-
};
169-
170-
const getLoadedRules = async () => {
171-
const usedPluginNames = await getConfigurationPluginNames({
172-
extends: [
173-
'airbnb',
174-
'google',
175-
'standard',
176-
'canonical',
177-
'canonical/ava',
178-
'canonical/browser',
179-
'canonical/flowtype',
180-
'canonical/jest',
181-
'canonical/json',
182-
'canonical/lodash',
183-
'canonical/mocha',
184-
'canonical/module',
185-
'canonical/node',
186-
'canonical/react',
187-
'canonical/typescript',
188-
'canonical/yaml',
189-
],
190-
});
191-
192-
let loadedRules = {
193-
...Object.fromEntries(builtinRules),
194-
};
195-
196-
for (const usedPluginName of usedPluginNames) {
197-
loadedRules = {
198-
...loadedRules,
199-
...getPluginRules(usedPluginName),
200-
};
201-
}
202-
203-
return Object.fromEntries(
204-
Object.entries(loadedRules)
205-
.sort((a, b) => {
206-
return a[0].localeCompare(b[0]);
207-
}),
208-
);
209-
};
11+
isRuleEnabled,
12+
getRuleLink,
13+
getRuleConfiguration,
14+
getConfigurationRules,
15+
getLoadedRules,
16+
} = require('./utilities');
21017

21118
(async () => {
21219
const loadedRules = await getLoadedRules();

0 commit comments

Comments
 (0)