ts-standardx
Version:
Yet another configurable linter for TypeScript and JavaScript.
159 lines (152 loc) • 4.86 kB
JavaScript
;
var eslintConfigPrettier = require('eslint-config-prettier');
var eslintrc_json = require('eslint-config-standard/eslintrc.json');
const rules = {
"@typescript-eslint/array-type": ["error", { default: "array-simple" }],
"@typescript-eslint/consistent-type-assertions": [
"error",
{
assertionStyle: "as",
objectLiteralTypeAssertions: "allow-as-parameter"
}
],
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
"@typescript-eslint/explicit-function-return-type": ["error", { allowExpressions: true }],
"@typescript-eslint/method-signature-style": "error",
"@typescript-eslint/no-base-to-string": "error",
"@typescript-eslint/no-dynamic-delete": "error",
"@typescript-eslint/no-extraneous-class": ["error", { allowWithDecorator: true }],
"@typescript-eslint/no-floating-promises": ["error", { ignoreIIFE: true }],
"@typescript-eslint/no-invalid-void-type": "error",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-this-alias": ["error", { allowDestructuring: true }],
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-nullish-coalescing": [
"error",
{
ignoreConditionalTests: false,
ignoreMixedLogicalExpressions: false
}
],
"@typescript-eslint/prefer-optional-chain": "error",
"@typescript-eslint/prefer-readonly": "error",
"@typescript-eslint/prefer-reduce-type-parameter": "error",
"@typescript-eslint/prefer-ts-expect-error": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": ["error", { ignoreStringArrays: true }],
"@typescript-eslint/restrict-plus-operands": ["error", { checkCompoundAssignments: true }],
"@typescript-eslint/return-await": ["error", "always"],
"@typescript-eslint/strict-boolean-expressions": [
"error",
{
allowString: false,
allowNumber: false,
allowNullableObject: false
}
],
"@typescript-eslint/triple-slash-reference": [
"error",
{
lib: "never",
path: "never",
types: "prefer-import"
}
]
};
const isTypescriptRule = (ruleName) => ruleName.startsWith("@typescript-eslint/");
const isModuleAvailable = (path) => {
try {
require.resolve(path);
return true;
} catch {
return false;
}
};
const prettierCompatRules = {
"arrow-body-style": "off",
"prefer-arrow-callback": "off",
quotes: [
"error",
"single",
{
avoidEscape: true,
allowTemplateLiterals: false
}
]
};
const prettierTypescriptCompatRules = Object.fromEntries(Object.entries(eslintConfigPrettier.rules).filter(([ruleName, ruleEntry]) => isTypescriptRule(ruleName) && ruleEntry !== 0));
const standardEquivalentRuleNames = [
"lines-between-class-members",
"no-implied-eval",
"no-loss-of-precision",
"no-throw-literal",
"no-unused-expressions",
"no-unused-vars",
"no-useless-constructor",
"quotes"
];
const standardEquivalentRules = Object.fromEntries(standardEquivalentRuleNames.map((ruleName) => [
[ruleName, "off"],
[`@typescript-eslint/${ruleName}`, eslintrc_json.rules[ruleName]]
]).flat());
const compatRules = {
camelcase: "off",
"@typescript-eslint/naming-convention": [
"error",
{
selector: "variableLike",
format: ["camelCase", "PascalCase", "UPPER_CASE"],
leadingUnderscore: "allowSingleOrDouble",
trailingUnderscore: "allowSingleOrDouble"
}
],
"dot-notation": "off",
"@typescript-eslint/dot-notation": "error",
"@typescript-eslint/no-dupe-class-members": "error",
"@typescript-eslint/no-duplicate-imports": "error",
"@typescript-eslint/no-redeclare": ["error", { builtinGlobals: false }],
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": [
"error",
{
functions: false,
classes: false,
variables: false,
ignoreTypeReferences: true
}
],
"no-void": ["error", { allowAsStatement: true }],
...standardEquivalentRules
};
const PRETTIER_STANDARD = {
semi: false,
singleQuote: true,
trailingComma: "none",
bracketSameLine: true,
arrowParens: "avoid"
};
const eslintrc = {
extends: ["standard", "standard-jsx", "prettier"],
plugins: ["prettier"],
rules: {
"prettier/prettier": ["error", PRETTIER_STANDARD],
...prettierCompatRules
},
overrides: isModuleAvailable("typescript") ? [
{
files: ["**/*.ts", "**/*.tsx"],
extends: ["plugin:@typescript-eslint/recommended"],
parserOptions: {
project: "./tsconfig.json"
},
rules: {
...rules,
...compatRules,
...prettierTypescriptCompatRules
}
}
] : void 0
};
module.exports = eslintrc;