Skip to content

Commit

Permalink
feat(eslint-plugin): add config all.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ldrick committed Feb 24, 2019
1 parent d362c4a commit b6d7590
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Expand Up @@ -28,7 +28,7 @@
"docs": "eslint-docs",
"docs:check": "eslint-docs check",
"test": "jest --coverage",
"recommended:update": "ts-node tools/update-recommended.ts",
"create:config": "ts-node --files tools/create-config.ts",
"prebuild": "npm run clean",
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf dist/",
Expand Down
57 changes: 57 additions & 0 deletions packages/eslint-plugin/src/configs/all.json
@@ -0,0 +1,57 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/ban-ts-ignore": "error",
"@typescript-eslint/ban-types": "error",
"camelcase": "off",
"@typescript-eslint/camelcase": "error",
"@typescript-eslint/class-name-casing": "error",
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/generic-type-naming": "warn",
"indent": "off",
"@typescript-eslint/indent": "error",
"@typescript-eslint/interface-name-prefix": "error",
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/member-naming": "warn",
"@typescript-eslint/member-ordering": "warn",
"@typescript-eslint/no-angle-bracket-type-assertion": "error",
"no-array-constructor": "off",
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-extraneous-class": "warn",
"@typescript-eslint/no-for-in-array": "warn",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-object-literal-type-assertion": "error",
"@typescript-eslint/no-parameter-properties": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/no-this-alias": "warn",
"@typescript-eslint/no-triple-slash-reference": "error",
"@typescript-eslint/no-type-alias": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "warn",
"@typescript-eslint/no-unnecessary-type-assertion": "warn",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-use-before-define": "error",
"@typescript-eslint/no-useless-constructor": "warn",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-interface": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/prefer-regexp-exec": "warn",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "warn",
"@typescript-eslint/restrict-plus-operands": "warn",
"@typescript-eslint/type-annotation-spacing": "error"
}
}
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/configs/recommended.json
Expand Up @@ -7,6 +7,7 @@
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/ban-ts-ignore": "error",
"@typescript-eslint/ban-types": "error",
"camelcase": "off",
"@typescript-eslint/camelcase": "error",
Expand All @@ -28,13 +29,15 @@
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-object-literal-type-assertion": "error",
"@typescript-eslint/no-parameter-properties": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/no-triple-slash-reference": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-use-before-define": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-interface": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/type-annotation-spacing": "error"
}
}
102 changes: 102 additions & 0 deletions packages/eslint-plugin/tools/create-config.ts
@@ -0,0 +1,102 @@
/* eslint-disable no-console */

import path from 'path';
import fs from 'fs';
import requireIndex from 'requireindex';

const MAX_RULE_NAME_LENGTH = 33 + 'typescript-eslint/'.length;
const RULE_NAME_PREFIX = '@typescript-eslint';

const all = Object.entries(
requireIndex(path.resolve(__dirname, '../dist/rules')),
);

const baseConfig = {
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
};

const bannedRules = new Set([
'camelcase',
'indent',
'no-array-constructor',
'no-unused-vars',
]);

/**
* Helper function reduces records to key - value pairs.
* @param config
* @param entry
*/
const reducer = (
config: Record<string, string>,
entry: Record<string, any>,
): Record<string, string> => {
const key = entry[0];
const value = entry[1];
const ruleName = `${RULE_NAME_PREFIX}/${key}`;
const setting = value.default.meta.docs.recommended;
const usedSetting = !setting ? 'warn' : setting;

// having this here is just for output niceness (the keys will be ordered)
if (bannedRules.has(key)) {
console.log(key.padEnd(MAX_RULE_NAME_LENGTH), '= off');
config[key] = 'off';
}
console.log(ruleName.padEnd(MAX_RULE_NAME_LENGTH), '=', usedSetting);
config[ruleName] = usedSetting;

return config;
};

/**
* Helper function to check for invalid recommended setting.
*/
function checkValidSettings(): boolean {
const validSettings = ['error', 'warn', false];
let result = true;

all.forEach(entry => {
const key = entry[0];
const value = entry[1];
const setting = value.default.meta.docs.recommended;

if (!validSettings.includes(setting)) {
console.error(`ERR! Invalid level for rule ${key}: "${setting}"`);
result = false;
}
});

return result;
}

/**
* Helper function generates configuration.
*/
function generate(rules: Record<string, string>, filePath: string): void {
const config = Object.assign({}, baseConfig, { rules });

fs.writeFileSync(filePath, `${JSON.stringify(config, null, 2)}\n`);
}

if (checkValidSettings()) {
console.log('------------------------- all.json -------------------------');
generate(
all.reduce(reducer, {}),
path.resolve(__dirname, '../src/configs/all.json'),
);

console.log('--------------------- recommended.json ---------------------');
const recommendedSettings = ['error', 'warn'];
generate(
all
.filter(entry =>
recommendedSettings.includes(entry[1].default.meta.docs.recommended),
)
.reduce(reducer, {}),
path.resolve(__dirname, '../src/configs/recommended.json'),
);
}
68 changes: 0 additions & 68 deletions packages/eslint-plugin/tools/update-recommended.ts

This file was deleted.

0 comments on commit b6d7590

Please sign in to comment.