Skip to content

Commit

Permalink
Normalize snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Sep 23, 2024
1 parent 8c24f8b commit cfd2c99
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
24 changes: 12 additions & 12 deletions packages/base/rules-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"@typescript-eslint/indent": "off",
"@typescript-eslint/key-spacing": "off",
"@typescript-eslint/keyword-spacing": "off",
"@typescript-eslint/lines-around-comment": 0,
"@typescript-eslint/lines-around-comment": "off",
"@typescript-eslint/member-delimiter-style": "off",
"@typescript-eslint/no-extra-parens": "off",
"@typescript-eslint/no-extra-semi": "off",
"@typescript-eslint/object-curly-spacing": "off",
"@typescript-eslint/quotes": 0,
"@typescript-eslint/quotes": "off",
"@typescript-eslint/semi": "off",
"@typescript-eslint/space-before-blocks": "off",
"@typescript-eslint/space-before-function-paren": "off",
Expand All @@ -29,7 +29,7 @@
"arrow-parens": "off",
"arrow-spacing": "off",
"babel/object-curly-spacing": "off",
"babel/quotes": 0,
"babel/quotes": "off",
"babel/semi": "off",
"block-scoped-var": "error",
"block-spacing": "off",
Expand Down Expand Up @@ -201,9 +201,9 @@
"key-spacing": "off",
"keyword-spacing": "off",
"linebreak-style": "off",
"lines-around-comment": 0,
"lines-around-comment": "off",
"lines-between-class-members": "error",
"max-len": 0,
"max-len": "off",
"max-statements-per-line": "off",
"multiline-ternary": "off",
"new-cap": ["error", { "newIsCap": true, "capIsNew": false }],
Expand All @@ -221,7 +221,7 @@
"no-comma-dangle": "off",
"no-compare-neg-zero": "error",
"no-cond-assign": "error",
"no-confusing-arrow": 0,
"no-confusing-arrow": "off",
"no-const-assign": "error",
"no-constant-binary-expression": "error",
"no-constant-condition": "error",
Expand Down Expand Up @@ -270,7 +270,7 @@
"no-loop-func": "error",
"no-loss-of-precision": "error",
"no-misleading-character-class": "error",
"no-mixed-operators": 0,
"no-mixed-operators": "off",
"no-mixed-spaces-and-tabs": "off",
"no-multi-assign": "error",
"no-multi-spaces": "off",
Expand Down Expand Up @@ -3167,7 +3167,7 @@
"no-trailing-spaces": "off",
"no-undef": "error",
"no-undef-init": "error",
"no-unexpected-multiline": 0,
"no-unexpected-multiline": "off",
"no-unmodified-loop-condition": "error",
"no-unneeded-ternary": ["error", { "defaultAssignment": false }],
"no-unreachable": "error",
Expand Down Expand Up @@ -3259,7 +3259,7 @@
"promise/param-names": "error",
"promise/valid-params": "warn",
"quote-props": "off",
"quotes": 0,
"quotes": "off",
"radix": "error",
"react/jsx-child-element-spacing": "off",
"react/jsx-closing-bracket-location": "off",
Expand Down Expand Up @@ -3322,7 +3322,7 @@
"unicorn/empty-brace-spaces": "off",
"unicorn/no-nested-ternary": "off",
"unicorn/number-literal-case": "off",
"unicorn/template-indent": 0,
"unicorn/template-indent": "off",
"use-isnan": "error",
"valid-typeof": "error",
"vue/array-bracket-newline": "off",
Expand All @@ -3342,11 +3342,11 @@
"vue/html-end-tags": "off",
"vue/html-indent": "off",
"vue/html-quotes": "off",
"vue/html-self-closing": 0,
"vue/html-self-closing": "off",
"vue/key-spacing": "off",
"vue/keyword-spacing": "off",
"vue/max-attributes-per-line": "off",
"vue/max-len": 0,
"vue/max-len": "off",
"vue/multiline-html-element-content-newline": "off",
"vue/multiline-ternary": "off",
"vue/mustache-interpolation-spacing": "off",
Expand Down
55 changes: 50 additions & 5 deletions scripts/validate-configs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { format } from 'prettier';

/**
* @typedef {import('eslint').Linter.Config[]} Config
* @typedef {Record<string, string | Array<[string, ...unknown[]]>>} Rules
*/

// The path to the monorepo packages directory
Expand All @@ -23,19 +24,62 @@ const WRITE_MODE =
// For logging
const TAB = ' ';

/**
* Given an ESLint rule config value, convert it from numerical (0, 1, 2) to
* string (off, warn, error) notation, or just returns the given value.
*
* @param {unknown} configValue - The rule config value to normalize.
* @returns {string | unknown} The normalized rule config value.
*/
function normalizeRuleConfigValue(configValue) {
if (typeof configValue !== 'number' && typeof configValue !== 'string') {
return configValue;
}

switch (String(configValue)) {
case '0':
return 'off';
case '1':
return 'warn';
case '2':
return 'error';
default:
return configValue;
}
}

/**
* Normalize a rules object, converting numerical rule values to string rule
* values.
*
* @param {Rules} rules - The rules object to normalize.
* @returns {Rules} The normalized rules object.
*/
function normalizeRules(rules) {
// @ts-expect-error - `Object.fromEntries` doesn't infer the return type.
return Object.fromEntries(
Object.entries(rules).map(([ruleName, ruleConfig]) => [
ruleName,
Array.isArray(ruleConfig)
? [normalizeRuleConfigValue(ruleConfig[0]), ...ruleConfig.slice(1)]
: normalizeRuleConfigValue(ruleConfig),
]),
);
}

/**
* Flatten a {@link ConfigArray} into a record of rule names to rule values.
*
* @param {ConfigArray} configArray - The config array to flatten.
* @returns {Record<string, string | object>} The flattened rule set.
* @returns {Rules} The flattened rule set.
*/
function flattenConfigArray(configArray) {
/**
* @type {Record<string, string | object>}
* @type {Rules}
*/
const ruleSet = configArray.reduce((flatConfig, rule) => {
if (hasProperty(rule, 'rules')) {
Object.assign(flatConfig, rule.rules);
Object.assign(flatConfig, normalizeRules(rule.rules));
}

return flatConfig;
Expand All @@ -53,13 +97,14 @@ function flattenConfigArray(configArray) {
* - Its flattened, complete rule set.
* - The path to the package.
*
* @returns {Promise<Map<string, { packagePath: string; ruleSet: Record<string, string | object> }>>} The config map.
* @returns {Promise<Map<string, { packagePath: string; ruleSet: Rules }>>} The
* config map.
*/
async function getMetaMaskConfigs() {
const packages = await fs.readdir(PACKAGES_DIR_PATH);

/**
* @type {Map<string, { packagePath: string; ruleSet: Record<string, string | object> }>}
* @type {Map<string, { packagePath: string; ruleSet: Rules }>}
*/
const allConfigs = new Map();

Expand Down

0 comments on commit cfd2c99

Please sign in to comment.