UNPKG

2.47 kBJavaScriptView Raw
1'use strict';
2const createDeprecatedRules = require('./rules/utils/create-deprecated-rules.js');
3const {loadRules} = require('./rules/utils/rule.js');
4const legacyConfigBase = require('./configs/legacy-config-base.js');
5const flatConfigBase = require('./configs/flat-config-base.js');
6const {name, version} = require('./package.json');
7
8const deprecatedRules = createDeprecatedRules({
9 // {ruleId: ReplacementRuleId | ReplacementRuleId[]}, if no replacement, use `{ruleId: []}`
10 'import-index': [],
11 'no-array-instanceof': 'unicorn/no-instanceof-array',
12 'no-fn-reference-in-iterator': 'unicorn/no-array-callback-reference',
13 'no-reduce': 'unicorn/no-array-reduce',
14 'no-unsafe-regex': [],
15 'prefer-dataset': 'unicorn/prefer-dom-node-dataset',
16 'prefer-event-key': 'unicorn/prefer-keyboard-event-key',
17 'prefer-exponentiation-operator': 'prefer-exponentiation-operator',
18 'prefer-flat-map': 'unicorn/prefer-array-flat-map',
19 'prefer-node-append': 'unicorn/prefer-dom-node-append',
20 'prefer-node-remove': 'unicorn/prefer-dom-node-remove',
21 'prefer-object-has-own': 'prefer-object-has-own',
22 'prefer-replace-all': 'unicorn/prefer-string-replace-all',
23 'prefer-starts-ends-with': 'unicorn/prefer-string-starts-ends-with',
24 'prefer-text-content': 'unicorn/prefer-dom-node-text-content',
25 'prefer-trim-start-end': 'unicorn/prefer-string-trim-start-end',
26 'regex-shorthand': 'unicorn/better-regex',
27});
28
29const externalRules = {
30 // Covered by `unicorn/no-negated-condition`
31 'no-negated-condition': 'off',
32 // Covered by `unicorn/no-nested-ternary`
33 'no-nested-ternary': 'off',
34};
35
36const rules = loadRules();
37const recommendedRules = Object.fromEntries(
38 Object.entries(rules).map(([id, rule]) => [
39 `unicorn/${id}`,
40 rule.meta.docs.recommended ? 'error' : 'off',
41 ]),
42);
43const allRules = Object.fromEntries(
44 Object.keys(rules).map(id => [
45 `unicorn/${id}`,
46 'error',
47 ]),
48);
49
50const createConfig = (rules, isLegacyConfig = false) => ({
51 ...(isLegacyConfig ? legacyConfigBase : flatConfigBase),
52 plugins: isLegacyConfig ? ['unicorn'] : {unicorn},
53 rules: {...externalRules, ...rules},
54});
55
56const unicorn = {
57 meta: {
58 name,
59 version,
60 },
61 rules: {
62 ...rules,
63 ...deprecatedRules,
64 },
65};
66
67const configs = {
68 recommended: createConfig(recommendedRules, /* isLegacyConfig */ true),
69 all: createConfig(allRules, /* isLegacyConfig */ true),
70 'flat/recommended': createConfig(recommendedRules),
71 'flat/all': createConfig(allRules),
72};
73
74module.exports = {...unicorn, configs};