Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add config all.json
- Loading branch information
Showing
5 changed files
with
163 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.