.eslintrc.js
truyền thống, ESLint 9+ hỗ trợ cấu hình theo kiểu flat modular, giúp dễ dàng phân chia quy tắc theo từng thư mục và ngữ cảnh.eslint-plugin-prettier
và config chuẩn:// .prettierrc.jsmodule.exports = { semi: true, singleQuote: true, trailingComma: 'es5', printWidth: 80, overrides: [ { files: '*.{ts,tsx}', options: { trailingComma: 'all' }, }, ],};
// ESLint config snippet{ plugins: { prettier: require('eslint-plugin-prettier') }, extends: ['prettier'], rules: { 'prettier/prettier': 'error', },}
// rules/consistent-error-handling.jsmodule.exports = { meta: { type: 'problem', fixable: 'code', messages: { inconsistentError: 'Error "{{ errorName }}" should start with "error" or "err"', }, }, create(context) { return { CatchClause(node) { const param = node.param; if (param && param.type === 'Identifier') { const errorName = param.name; const validPatterns = ['error', 'err', 'e']; if (!validPatterns.some(pattern => errorName.toLowerCase().startsWith(pattern))) { context.report({ node: param, messageId: 'inconsistentError', data: { errorName }, fix(fixer) { return fixer.replaceText(param, 'error'); }, }); } } }, }; },};
# .github/workflows/code-quality.ymlname: Code Qualityon: [push, pull_request]jobs: quality: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '18' cache: 'npm' - run: npm ci - name: ESLint with SARIF run: | npx eslint . \ --format=@microsoft/eslint-formatter-sarif \ --output-file=eslint-results.sarif \ --max-warnings=0 - name: Upload to GitHub Security uses: github/codeql-action/upload-sarif@v2 if: always() with: sarif_file: eslint-results.sarif - name: Prettier Check run: npx prettier --check .
lint-staged
và husky
:// package.json excerpt{ "lint-staged": { "*.{js,jsx,ts,tsx}": [ "eslint --fix --max-warnings=0", "prettier --write" ], "*.{json,md,yml}": ["prettier --write"] }, "husky": { "hooks": { "pre-commit": "lint-staged" } }}
Loại Quy Tắc | Tính Chất | Ví Dụ |
---|---|---|
Quy tắc nhanh | Ưu tiên dùng | 'prefer-const', 'no-var', 'eqeqeq' |
Quy tắc tốn tài nguyên | Cẩn trọng dùng | '@typescript-eslint/no-floating-promises', 'import/no-cycles' |
node_modules/**
dist/**
.min.js
, file định nghĩa TypeScript **/*.d.ts
const LINT_PHASE = process.env.LINT_PHASE || 'introduction';const ruleLevel = { introduction: 'warn', // tuần 1-2: cảnh báo enforcement: 'error', // tuần 3-4: lỗi chặn commit strict: 'error', // tuần 5+: bắt fix tất cả}[LINT_PHASE];
export default [ { rules: { 'prefer-const': ruleLevel, 'complexity': [ruleLevel, { max: 15 }], }, }];