Phase 0: перевод на TypeScript, строгий ESLint (strictTypeChecked + sonarjs + import-x)
This commit is contained in:
+258
-15
@@ -1,21 +1,264 @@
|
||||
// ESLint конфигурация для backend (Node.js, ES Modules)
|
||||
/**
|
||||
* ESLint конфигурация для backend (TypeScript, Node.js, ES Modules).
|
||||
* strictTypeChecked + sonarjs + import-x + eslint-comments.
|
||||
*/
|
||||
import js from '@eslint/js';
|
||||
import comments from '@eslint-community/eslint-plugin-eslint-comments';
|
||||
import importX from 'eslint-plugin-import-x';
|
||||
import sonarjs from 'eslint-plugin-sonarjs';
|
||||
import globals from 'globals';
|
||||
import { defineConfig, globalIgnores } from 'eslint/config';
|
||||
import tseslint from 'typescript-eslint';
|
||||
|
||||
export default [
|
||||
js.configs.recommended,
|
||||
const CONFIG_ROOT = import.meta.dirname;
|
||||
|
||||
const TEST_FILES = ['**/*.{test,spec}.ts', 'tests/**/*.{ts,mts,cts}'];
|
||||
const CONFIG_AND_SCRIPT_FILES = ['**/*.config.{ts,mts,cts}', '**/eslint.config.*'];
|
||||
|
||||
export default defineConfig(
|
||||
/* ── Глобальные игноры ─────────────────────────────────────── */
|
||||
globalIgnores([
|
||||
'**/node_modules/**',
|
||||
'**/dist/**',
|
||||
'**/build/**',
|
||||
'**/coverage/**',
|
||||
'**/uploads/**',
|
||||
'**/*.min.js',
|
||||
]),
|
||||
|
||||
/* ── Политика линтера ─────────────────────────────────────── */
|
||||
{
|
||||
files: ['src/**/*.js', 'tests/**/*.js'],
|
||||
languageOptions: {
|
||||
ecmaVersion: 2024,
|
||||
sourceType: 'module',
|
||||
globals: {
|
||||
...globals.node,
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
|
||||
'no-console': 'off',
|
||||
name: 'server/linter-policy',
|
||||
linterOptions: {
|
||||
noInlineConfig: false,
|
||||
reportUnusedDisableDirectives: 'error',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
/* ── TypeScript — strict mode ──────────────────────────────── */
|
||||
...tseslint.configs.strictTypeChecked,
|
||||
|
||||
/* ── Импорты (import-x) ────────────────────────────────────── */
|
||||
importX.flatConfigs.recommended,
|
||||
importX.flatConfigs.typescript,
|
||||
|
||||
/* ── SonarJS ───────────────────────────────────────────────── */
|
||||
sonarjs.configs.recommended,
|
||||
|
||||
/* ── Правила сервера ───────────────────────────────────────── */
|
||||
{
|
||||
name: 'server/rules',
|
||||
files: ['**/*.ts'],
|
||||
languageOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'module',
|
||||
globals: globals.node,
|
||||
parserOptions: {
|
||||
projectService: {
|
||||
allowDefaultProject: ['*.config.ts'],
|
||||
},
|
||||
tsconfigRootDir: CONFIG_ROOT,
|
||||
},
|
||||
},
|
||||
settings: {
|
||||
'import-x/resolver': {
|
||||
typescript: { alwaysTryTypes: true },
|
||||
node: true,
|
||||
},
|
||||
},
|
||||
plugins: {
|
||||
'@eslint-community/eslint-comments': comments,
|
||||
},
|
||||
rules: {
|
||||
...js.configs.recommended.rules,
|
||||
|
||||
/* Отключаем базовые правила, которые заменяет TS */
|
||||
'no-redeclare': 'off',
|
||||
'no-unused-vars': 'off',
|
||||
'no-use-before-define': 'off',
|
||||
'no-shadow': 'off',
|
||||
'no-throw-literal': 'off',
|
||||
'consistent-return': 'off',
|
||||
'prefer-promise-reject-errors': 'off',
|
||||
'require-await': 'off',
|
||||
|
||||
/* Корректность */
|
||||
'array-callback-return': ['error', { allowImplicit: false, checkForEach: true }],
|
||||
curly: ['error', 'all'],
|
||||
eqeqeq: ['error', 'always', { null: 'ignore' }],
|
||||
'logical-assignment-operators': ['error', 'always', { enforceForIfStatements: true }],
|
||||
'no-console': ['error', { allow: ['error', 'warn'] }],
|
||||
'no-debugger': 'error',
|
||||
'no-eval': 'error',
|
||||
'no-alert': 'error',
|
||||
'no-else-return': ['error', { allowElseIf: false }],
|
||||
'no-extend-native': 'error',
|
||||
'no-implicit-coercion': 'error',
|
||||
'no-lone-blocks': 'error',
|
||||
'no-nested-ternary': 'error',
|
||||
'no-param-reassign': ['error', { props: true }],
|
||||
'no-promise-executor-return': 'error',
|
||||
'no-return-assign': ['error', 'always'],
|
||||
'no-self-compare': 'error',
|
||||
'no-sequences': 'error',
|
||||
'no-unneeded-ternary': 'error',
|
||||
'no-useless-concat': 'error',
|
||||
'no-useless-return': 'error',
|
||||
'object-shorthand': ['error', 'always'],
|
||||
'prefer-const': 'error',
|
||||
'prefer-object-spread': 'error',
|
||||
'require-atomic-updates': 'error',
|
||||
|
||||
/* TS — типы */
|
||||
'@typescript-eslint/ban-ts-comment': [
|
||||
'error',
|
||||
{
|
||||
'ts-check': false,
|
||||
'ts-expect-error': 'allow-with-description',
|
||||
'ts-ignore': true,
|
||||
'ts-nocheck': true,
|
||||
minimumDescriptionLength: 20,
|
||||
},
|
||||
],
|
||||
'@typescript-eslint/consistent-type-assertions': ['error', { assertionStyle: 'never' }],
|
||||
'@typescript-eslint/no-explicit-any': 'error',
|
||||
'@typescript-eslint/no-non-null-assertion': 'error',
|
||||
'@typescript-eslint/no-unnecessary-type-assertion': ['error', { checkLiteralConstAssertions: true }],
|
||||
'@typescript-eslint/no-unsafe-argument': 'error',
|
||||
'@typescript-eslint/no-unsafe-assignment': 'error',
|
||||
'@typescript-eslint/no-unsafe-call': 'error',
|
||||
'@typescript-eslint/no-unsafe-member-access': 'error',
|
||||
'@typescript-eslint/no-unsafe-return': 'error',
|
||||
'@typescript-eslint/no-unsafe-type-assertion': 'error',
|
||||
|
||||
/* TS — контракты */
|
||||
'@typescript-eslint/consistent-return': 'error',
|
||||
'@typescript-eslint/consistent-type-exports': 'error',
|
||||
'@typescript-eslint/consistent-type-imports': [
|
||||
'error',
|
||||
{ prefer: 'type-imports', fixStyle: 'separate-type-imports' },
|
||||
],
|
||||
'@typescript-eslint/no-deprecated': 'error',
|
||||
'@typescript-eslint/no-misused-promises': [
|
||||
'error',
|
||||
{
|
||||
checksConditionals: true,
|
||||
checksSpreads: true,
|
||||
checksVoidReturn: {
|
||||
arguments: true,
|
||||
properties: true,
|
||||
returns: true,
|
||||
variables: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
'@typescript-eslint/no-unnecessary-condition': [
|
||||
'error',
|
||||
{ allowConstantLoopConditions: 'only-allowed-literals' },
|
||||
],
|
||||
'@typescript-eslint/no-unnecessary-type-arguments': 'error',
|
||||
'@typescript-eslint/only-throw-error': 'error',
|
||||
'@typescript-eslint/prefer-nullish-coalescing': [
|
||||
'error',
|
||||
{
|
||||
ignoreBooleanCoercion: false,
|
||||
ignoreConditionalTests: false,
|
||||
ignorePrimitives: { bigint: false, boolean: false, number: false, string: false },
|
||||
ignoreTernaryTests: false,
|
||||
},
|
||||
],
|
||||
'@typescript-eslint/prefer-optional-chain': [
|
||||
'error',
|
||||
{
|
||||
allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing: false,
|
||||
checkAny: true,
|
||||
checkBigInt: true,
|
||||
checkBoolean: true,
|
||||
checkNumber: true,
|
||||
checkString: true,
|
||||
requireNullish: true,
|
||||
},
|
||||
],
|
||||
'@typescript-eslint/require-await': 'error',
|
||||
'@typescript-eslint/return-await': ['error', 'error-handling-correctness-only'],
|
||||
'@typescript-eslint/strict-boolean-expressions': 'error',
|
||||
'@typescript-eslint/promise-function-async': 'error',
|
||||
'@typescript-eslint/no-floating-promises': ['error', { ignoreIIFE: false, ignoreVoid: false }],
|
||||
'@typescript-eslint/no-shadow': 'error',
|
||||
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'error',
|
||||
'@typescript-eslint/switch-exhaustiveness-check': [
|
||||
'error',
|
||||
{
|
||||
allowDefaultCaseForExhaustiveSwitch: false,
|
||||
considerDefaultExhaustiveForUnions: false,
|
||||
requireDefaultForNonUnion: true,
|
||||
},
|
||||
],
|
||||
|
||||
/* Сложность */
|
||||
complexity: ['error', 10],
|
||||
'max-classes-per-file': ['error', 1],
|
||||
'max-depth': ['error', 3],
|
||||
'max-lines': ['error', { max: 400, skipBlankLines: true, skipComments: true }],
|
||||
'max-lines-per-function': ['error', { IIFEs: true, max: 80, skipBlankLines: true, skipComments: true }],
|
||||
'max-nested-callbacks': ['error', 3],
|
||||
'max-params': ['error', 4],
|
||||
'max-statements': ['error', 25],
|
||||
'sonarjs/cognitive-complexity': ['error', 10],
|
||||
'sonarjs/no-duplicate-string': 'error',
|
||||
|
||||
/* Импорты */
|
||||
'import-x/first': 'error',
|
||||
'import-x/newline-after-import': 'error',
|
||||
'import-x/no-absolute-path': 'error',
|
||||
'import-x/no-cycle': ['warn', { ignoreExternal: true, maxDepth: 5 }],
|
||||
'import-x/no-duplicates': 'error',
|
||||
'import-x/no-extraneous-dependencies': [
|
||||
'error',
|
||||
{ devDependencies: false, optionalDependencies: false, peerDependencies: false },
|
||||
],
|
||||
'import-x/no-named-as-default': 'error',
|
||||
'import-x/no-named-as-default-member': 'error',
|
||||
'import-x/no-self-import': 'error',
|
||||
'import-x/no-useless-path-segments': 'error',
|
||||
'import-x/order': ['error', {
|
||||
alphabetize: { caseInsensitive: true, order: 'asc' },
|
||||
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'type'],
|
||||
'newlines-between': 'always',
|
||||
}],
|
||||
|
||||
/* Запрет необъяснённых disable-директив */
|
||||
'@eslint-community/eslint-comments/disable-enable-pair': ['error', { allowWholeFile: false }],
|
||||
'@eslint-community/eslint-comments/no-aggregating-enable': 'error',
|
||||
'@eslint-community/eslint-comments/no-duplicate-disable': 'error',
|
||||
'@eslint-community/eslint-comments/no-unlimited-disable': 'error',
|
||||
'@eslint-community/eslint-comments/require-description': ['error', { ignore: ['eslint-enable'] }],
|
||||
},
|
||||
},
|
||||
|
||||
/* ── Конфигурационные файлы ────────────────────────────────── */
|
||||
{
|
||||
name: 'server/config-files',
|
||||
files: CONFIG_AND_SCRIPT_FILES,
|
||||
rules: {
|
||||
'no-console': ['error', { allow: ['error', 'warn'] }],
|
||||
'import-x/no-extraneous-dependencies': ['error', { devDependencies: true }],
|
||||
},
|
||||
},
|
||||
|
||||
/* ── Тестовые файлы ────────────────────────────────────────── */
|
||||
{
|
||||
name: 'server/tests',
|
||||
files: TEST_FILES,
|
||||
rules: {
|
||||
'max-lines': ['error', { max: 700, skipBlankLines: true, skipComments: true }],
|
||||
'max-lines-per-function': 'off',
|
||||
'max-statements': 'off',
|
||||
'sonarjs/cognitive-complexity': 'off',
|
||||
'sonarjs/no-duplicate-string': 'off',
|
||||
'import-x/no-extraneous-dependencies': ['error', { devDependencies: true }],
|
||||
'@typescript-eslint/no-floating-promises': 'off',
|
||||
'@typescript-eslint/require-await': 'off',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
Generated
+2798
-316
File diff suppressed because it is too large
Load Diff
+21
-6
@@ -4,10 +4,11 @@
|
||||
"private": true,
|
||||
"description": "News Editor API — Express + MongoDB + JWT + Socket.IO",
|
||||
"type": "module",
|
||||
"main": "src/server.js",
|
||||
"main": "dist/server.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon src/server.js",
|
||||
"start": "node src/server.js",
|
||||
"dev": "tsx watch src/server.ts",
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"start": "node dist/server.js",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"lint": "eslint src/ tests/"
|
||||
@@ -24,12 +25,26 @@
|
||||
"socket.io": "^4.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^10.0.1",
|
||||
"@eslint-community/eslint-plugin-eslint-comments": "^4.4.1",
|
||||
"@eslint/js": "^9.17.0",
|
||||
"@types/bcrypt": "^5.0.2",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^5.0.0",
|
||||
"@types/jsonwebtoken": "^9.0.7",
|
||||
"@types/multer": "^1.4.12",
|
||||
"@types/node": "^22.10.0",
|
||||
"@types/supertest": "^6.0.2",
|
||||
"eslint": "^9.17.0",
|
||||
"globals": "^17.11.0",
|
||||
"eslint-import-resolver-node": "^0.4.0",
|
||||
"eslint-import-resolver-typescript": "^4.4.5",
|
||||
"eslint-plugin-import-x": "^4.3.0",
|
||||
"eslint-plugin-sonarjs": "^3.0.1",
|
||||
"globals": "^16.0.0",
|
||||
"mongodb-memory-server": "^10.1.2",
|
||||
"nodemon": "^3.1.9",
|
||||
"supertest": "^7.0.0",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.7.2",
|
||||
"typescript-eslint": "^8.18.0",
|
||||
"vitest": "^3.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"lib": ["ES2022"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noImplicitOverride": true
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user