diff --git a/configs/all.js b/configs/all.js deleted file mode 100644 index 743845a378..0000000000 --- a/configs/all.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; -const recommended = require('./recommended.js'); - -module.exports = Object.fromEntries(Object.entries(recommended).map( - ([ruleId, severity]) => [ruleId, ruleId.startsWith('unicorn/') ? 'error' : severity], -)); diff --git a/configs/recommended.js b/configs/recommended.js deleted file mode 100644 index 86fd567670..0000000000 --- a/configs/recommended.js +++ /dev/null @@ -1,122 +0,0 @@ -'use strict'; -module.exports = { - 'unicorn/better-regex': 'error', - 'unicorn/catch-error-name': 'error', - 'unicorn/consistent-destructuring': 'off', - 'unicorn/consistent-function-scoping': 'error', - 'unicorn/custom-error-definition': 'off', - 'unicorn/empty-brace-spaces': 'error', - 'unicorn/error-message': 'error', - 'unicorn/escape-case': 'error', - 'unicorn/expiring-todo-comments': 'error', - 'unicorn/explicit-length-check': 'error', - 'unicorn/filename-case': 'error', - 'unicorn/import-style': 'error', - 'unicorn/new-for-builtins': 'error', - 'unicorn/no-abusive-eslint-disable': 'error', - 'unicorn/no-anonymous-default-export': 'error', - 'unicorn/no-array-callback-reference': 'error', - 'unicorn/no-array-for-each': 'error', - 'unicorn/no-array-method-this-argument': 'error', - 'unicorn/no-array-push-push': 'error', - 'unicorn/no-array-reduce': 'error', - 'unicorn/no-await-expression-member': 'error', - 'unicorn/no-await-in-promise-methods': 'error', - 'unicorn/no-console-spaces': 'error', - 'unicorn/no-document-cookie': 'error', - 'unicorn/no-empty-file': 'error', - 'unicorn/no-for-loop': 'error', - 'unicorn/no-hex-escape': 'error', - 'unicorn/no-instanceof-array': 'error', - 'unicorn/no-invalid-remove-event-listener': 'error', - 'unicorn/no-keyword-prefix': 'off', - 'unicorn/no-lonely-if': 'error', - 'unicorn/no-magic-array-flat-depth': 'error', - 'no-negated-condition': 'off', - 'unicorn/no-negated-condition': 'error', - 'no-nested-ternary': 'off', - 'unicorn/no-nested-ternary': 'error', - 'unicorn/no-new-array': 'error', - 'unicorn/no-new-buffer': 'error', - 'unicorn/no-null': 'error', - 'unicorn/no-object-as-default-parameter': 'error', - 'unicorn/no-process-exit': 'error', - 'unicorn/no-single-promise-in-promise-methods': 'error', - 'unicorn/no-static-only-class': 'error', - 'unicorn/no-thenable': 'error', - 'unicorn/no-this-assignment': 'error', - 'unicorn/no-typeof-undefined': 'error', - 'unicorn/no-unnecessary-await': 'error', - 'unicorn/no-unnecessary-polyfills': 'error', - 'unicorn/no-unreadable-array-destructuring': 'error', - 'unicorn/no-unreadable-iife': 'error', - 'unicorn/no-unused-properties': 'off', - 'unicorn/no-useless-fallback-in-spread': 'error', - 'unicorn/no-useless-length-check': 'error', - 'unicorn/no-useless-promise-resolve-reject': 'error', - 'unicorn/no-useless-spread': 'error', - 'unicorn/no-useless-switch-case': 'error', - 'unicorn/no-useless-undefined': 'error', - 'unicorn/no-zero-fractions': 'error', - 'unicorn/number-literal-case': 'error', - 'unicorn/numeric-separators-style': 'error', - 'unicorn/prefer-add-event-listener': 'error', - 'unicorn/prefer-array-find': 'error', - 'unicorn/prefer-array-flat': 'error', - 'unicorn/prefer-array-flat-map': 'error', - 'unicorn/prefer-array-index-of': 'error', - 'unicorn/prefer-array-some': 'error', - 'unicorn/prefer-at': 'error', - 'unicorn/prefer-blob-reading-methods': 'error', - 'unicorn/prefer-code-point': 'error', - 'unicorn/prefer-date-now': 'error', - 'unicorn/prefer-default-parameters': 'error', - 'unicorn/prefer-dom-node-append': 'error', - 'unicorn/prefer-dom-node-dataset': 'error', - 'unicorn/prefer-dom-node-remove': 'error', - 'unicorn/prefer-dom-node-text-content': 'error', - 'unicorn/prefer-event-target': 'error', - 'unicorn/prefer-export-from': 'error', - 'unicorn/prefer-includes': 'error', - 'unicorn/prefer-json-parse-buffer': 'off', - 'unicorn/prefer-keyboard-event-key': 'error', - 'unicorn/prefer-logical-operator-over-ternary': 'error', - 'unicorn/prefer-math-trunc': 'error', - 'unicorn/prefer-modern-dom-apis': 'error', - 'unicorn/prefer-modern-math-apis': 'error', - 'unicorn/prefer-module': 'error', - 'unicorn/prefer-native-coercion-functions': 'error', - 'unicorn/prefer-negative-index': 'error', - 'unicorn/prefer-node-protocol': 'error', - 'unicorn/prefer-number-properties': 'error', - 'unicorn/prefer-object-from-entries': 'error', - 'unicorn/prefer-optional-catch-binding': 'error', - 'unicorn/prefer-prototype-methods': 'error', - 'unicorn/prefer-query-selector': 'error', - 'unicorn/prefer-reflect-apply': 'error', - 'unicorn/prefer-regexp-test': 'error', - 'unicorn/prefer-set-has': 'error', - 'unicorn/prefer-set-size': 'error', - 'unicorn/prefer-spread': 'error', - 'unicorn/prefer-string-replace-all': 'error', - 'unicorn/prefer-string-slice': 'error', - 'unicorn/prefer-string-starts-ends-with': 'error', - 'unicorn/prefer-string-trim-start-end': 'error', - 'unicorn/prefer-structured-clone': 'error', - 'unicorn/prefer-switch': 'error', - 'unicorn/prefer-ternary': 'error', - 'unicorn/prefer-top-level-await': 'error', - 'unicorn/prefer-type-error': 'error', - 'unicorn/prevent-abbreviations': 'error', - 'unicorn/relative-url-style': 'error', - 'unicorn/require-array-join-separator': 'error', - 'unicorn/require-number-to-fixed-digits-argument': 'error', - // Turned off because we can't distinguish `widow.postMessage` and `{Worker,MessagePort,Client,BroadcastChannel}#postMessage()` - // See #1396 - 'unicorn/require-post-message-target-origin': 'off', - 'unicorn/string-content': 'off', - 'unicorn/switch-case-braces': 'error', - 'unicorn/template-indent': 'error', - 'unicorn/text-encoding-identifier-case': 'error', - 'unicorn/throw-new-error': 'error', -}; diff --git a/index.js b/index.js index ed98665e25..0f754a2077 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,6 @@ const createDeprecatedRules = require('./rules/utils/create-deprecated-rules.js' const {loadRules} = require('./rules/utils/rule.js'); const legacyConfigBase = require('./configs/legacy-config-base.js'); const flatConfigBase = require('./configs/flat-config-base.js'); -const recommendedRules = require('./configs/recommended.js'); -const allRules = require('./configs/all.js'); const {name, version} = require('./package.json'); const deprecatedRules = createDeprecatedRules({ @@ -28,10 +26,31 @@ const deprecatedRules = createDeprecatedRules({ 'regex-shorthand': 'unicorn/better-regex', }); +const externalRules = { + // Covered by `unicorn/no-negated-condition` + 'no-negated-condition': 'off', + // Covered by `unicorn/no-nested-ternary` + 'no-nested-ternary': 'off', +}; + +const rules = loadRules(); +const recommendedRules = Object.fromEntries( + Object.entries(rules).map(([id, rule]) => [ + `unicorn/${id}`, + rule.meta.docs.recommended ? 'error' : 'off', + ]), +); +const allRules = Object.fromEntries( + Object.keys(rules).map(id => [ + `unicorn/${id}`, + 'error', + ]), +); + const createConfig = (rules, isLegacyConfig = false) => ({ ...(isLegacyConfig ? legacyConfigBase : flatConfigBase), plugins: isLegacyConfig ? ['unicorn'] : {unicorn}, - rules, + rules: {...externalRules, ...rules}, }); const unicorn = { @@ -40,7 +59,7 @@ const unicorn = { version, }, rules: { - ...loadRules(), + ...rules, ...deprecatedRules, }, }; diff --git a/rules/require-post-message-target-origin.js b/rules/require-post-message-target-origin.js index 1a6734e70b..d43159d66e 100644 --- a/rules/require-post-message-target-origin.js +++ b/rules/require-post-message-target-origin.js @@ -64,6 +64,8 @@ module.exports = { type: 'problem', docs: { description: 'Enforce using the `targetOrigin` argument with `window.postMessage()`.', + // Turned off because we can't distinguish `window.postMessage` and `{Worker,MessagePort,Client,BroadcastChannel}#postMessage()` + // See #1396 recommended: false, }, hasSuggestions: true, diff --git a/scripts/create-rule.mjs b/scripts/create-rule.mjs index e04b8e590c..5758272050 100644 --- a/scripts/create-rule.mjs +++ b/scripts/create-rule.mjs @@ -35,44 +35,6 @@ function renderTemplate({source, target, data}) { return fs.writeFileSync(targetFile, content); } -function updateRecommended(id) { - const RULE_INDENT = '\t'; - const file = path.join(ROOT, 'configs/recommended.js'); - const content = fs.readFileSync(file, 'utf8'); - const {before, rules, after} = content.match(/(?.*?{\n)(?.*?)(?\n};\s*)/s)?.groups ?? {}; - if (!rules) { - throw new Error('Unexpected content in “configs/recommended.js”.'); - } - - const lines = rules.split('\n'); - - if (!lines.every(line => line.startsWith(RULE_INDENT))) { - throw new Error('Unexpected content in “configs/recommended.js”.'); - } - - let ruleContent = `${RULE_INDENT}'unicorn/${id}': 'error',`; - - const unicornRuleLines = lines.filter(line => line.startsWith(`${RULE_INDENT}'unicorn/`)); - let insertIndex; - if (ruleContent.localeCompare(unicornRuleLines[0]) === -1) { - insertIndex = 0; - } else if (ruleContent.localeCompare(unicornRuleLines.at(-1)) === 1) { - insertIndex = lines.length; - lines[lines.length - 1] += ','; - ruleContent = ruleContent.slice(0, -1); - } else { - const lineBefore = unicornRuleLines[ - unicornRuleLines.findIndex(line => line.localeCompare(ruleContent) === 1) - 1 - ]; - insertIndex = lines.indexOf(lineBefore) + 1; - } - - lines.splice(insertIndex, 0, ruleContent); - - const updated = `${before}${lines.join('\n')}${after}`; - fs.writeFileSync(file, updated); -} - async function getData() { const questions = [ { @@ -153,7 +115,6 @@ renderTemplate({ target: `test/${id}.mjs`, data, }); -updateRecommended(id); try { await execa('code', [ diff --git a/test/package.mjs b/test/package.mjs index 2c27a25ba4..2dad8894fd 100644 --- a/test/package.mjs +++ b/test/package.mjs @@ -24,17 +24,6 @@ const deprecatedRules = Object.entries(eslintPluginUnicorn.rules) .filter(([, {meta: {deprecated}}]) => deprecated) .map(([ruleId]) => ruleId); -const testSorted = (t, actualOrder, sourceName) => { - actualOrder = actualOrder.filter(x => !ignoredRules.includes(x)); - const sortedOrder = [...actualOrder].sort(); - - for (const [wantedIndex, name] of sortedOrder.entries()) { - const actualIndex = actualOrder.indexOf(name); - const whereMessage = (wantedIndex === 0) ? '' : `, after '${sortedOrder[wantedIndex - 1]}'`; - t.is(actualIndex, wantedIndex, `${sourceName} should be alphabetically sorted, '${name}' should be placed at index ${wantedIndex}${whereMessage}`); - } -}; - const RULES_WITHOUT_PASS_FAIL_SECTIONS = new Set([ // Doesn't show code samples since it's just focused on filenames. 'filename-case', @@ -72,8 +61,6 @@ test('Every rule is defined in index file in alphabetical order', t => { ruleFiles.length - deprecatedRules.length, 'There are more rules than those exported in the all config.', ); - - testSorted(t, Object.keys(eslintPluginUnicorn.configs.recommended.rules), 'configs.recommended.rules'); }); test('validate configuration', async t => {