Giới Thiệu
Trong bài viết này, chúng ta sẽ tìm hiểu cách thiết lập ESLint 9 cho dự án React Native với sự tích hợp VSCode, sử dụng Husky cho các pre-commit hooks, Prettier để định dạng mã, và CI/CD để đảm bảo chất lượng mã nguồn. Việc thiết lập này sẽ giúp bạn duy trì mã nguồn sạch sẽ và nhất quán, đồng thời phát hiện lỗi sớm hơn trong quá trình phát triển.
Mục Lục
- Cài Đặt Tất Cả Các Phụ Thuộc
- Tạo Các Tập Tin Cấu Hình
- Lệnh Thiết Lập Nhanh
- Tính Năng Tích Hợp VSCode
- Tiện Ích VSCode Cần Thiết
- Sử Dụng
- Vấn Đề Thường Gặp
- Sau Khi Thiết Lập
Cài Đặt Tất Cả Các Phụ Thuộc
Để bắt đầu, bạn cần cài đặt tất cả các phụ thuộc cần thiết cho ESLint, Prettier và Husky:
bash
# Cài đặt các phụ thuộc cơ bản của ESLint
yarn add --dev eslint @eslint/js globals typescript-eslint
# Cài đặt các plugin cho React và React Native
yarn add --dev eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-native
# Tích hợp Prettier
yarn add --dev prettier eslint-config-prettier
# Husky và lint-staged cho các pre-commit hooks
yarn add --dev husky lint-staged
# Các công cụ hỗ trợ khác
yarn add --dev @types/node
Tạo Các Tập Tin Cấu Hình
Cấu Hình ESLint
Tạo một tập tin cấu hình ESLint với tên eslint.config.mjs:
javascript
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import pluginReact from 'eslint-plugin-react';
import pluginReactHooks from 'eslint-plugin-react-hooks';
import pluginReactNative from 'eslint-plugin-react-native';
export default [
{
ignores: ['node_modules/**', 'android/**', 'ios/**', 'build/**', 'dist/**'],
},
js.configs.recommended,
...tseslint.configs.recommended,
pluginReact.configs.flat.recommended,
{
files: ['**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
languageOptions: {
globals: {
...globals.browser,
...globals.node,
__DEV__: 'readonly',
__dirname: 'readonly',
__filename: 'readonly',
},
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
ecmaFeatures: { jsx: true },
},
},
plugins: {
'react-hooks': pluginReactHooks,
'react-native': pluginReactNative,
},
rules: {
'react-native/no-unused-styles': 'error',
'react-native/split-platform-components': 'error',
'react-native/no-inline-styles': 'warn',
'react-native/no-color-literals': 'warn',
'react-native/no-single-element-style-arrays': 'error',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'off', // Tắt cho tương thích ESLint 9
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'react/display-name': 'off',
'no-console': 'warn',
'no-debugger': 'error',
'no-unused-vars': 'off',
'prefer-const': 'error',
'no-var': 'error',
'no-duplicate-imports': 'error',
'max-lines': ['warn', { max: 300, skipBlankLines: true }],
},
settings: {
react: { version: 'detect' },
},
},
];
Cấu Hình Prettier
Tạo tập tin cấu hình Prettier với tên .prettierrc.js:
javascript
module.exports = {
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
printWidth: 100,
bracketSpacing: true,
arrowParens: 'avoid',
};
Tập Tin Bỏ Qua Prettier
Tạo tập tin .prettierignore để chỉ định các tập tin và thư mục không được định dạng:
node_modules
android
ios
build
dist
coverage
*.snap
*.md
*.json
*.lock
*.log
.DS_Store
eslint.config.mjs
Cập Nhật Scripts Trong Package.json
Cập nhật tập tin package.json để thêm các script cho lint và prettier:
json
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"lint:check": "eslint . --max-warnings 0",
"prettier": "prettier --write .",
"prettier:check": "prettier --check .",
"format": "prettier --write . && eslint . --fix",
"prepare": "husky install"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
]
}
}
Thiết Lập Husky Pre-commit Hooks
Khởi tạo Husky và thêm pre-commit hooks:
bash
# Khởi tạo husky
yarn prepare
# Thêm pre-commit hook
npx husky add .husky/pre-commit "npx lint-staged"
# Thêm pre-push hook (tùy chọn)
npx husky add .husky/pre-push "yarn lint:check"
Tạo Cài Đặt VSCode
Tạo tập tin .vscode/settings.json để cấu hình VSCode:
json
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "explicit"
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"eslint.workingDirectories": ["."],
"typescript.preferences.organizeImportsIgnoreCase": false,
"typescript.preferences.includePackageJsonAutoImports": "on",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Gợi Ý Các Tiện Ích VSCode
Tạo tập tin .vscode/extensions.json để đề xuất các tiện ích mở rộng:
json
{
"recommendations": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"ms-vscode.vscode-typescript-next",
"bradlc.vscode-tailwindcss"
]
}
Tạo Workflow GitHub Actions
Tạo tập tin .github/workflows/lint.yml để thiết lập kiểm tra mã tự động:
yaml
name: Code Quality
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn lint:check
- run: yarn prettier:check
Lệnh Thiết Lập Nhanh
Để thiết lập nhanh, bạn có thể sử dụng các lệnh sau:
bash
# 1. Cài đặt tất cả
yarn add --dev eslint @eslint/js globals typescript-eslint eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-native prettier eslint-config-prettier husky lint-staged @types/node
# 2. Khởi tạo husky
yarn prepare
# 3. Thiết lập pre-commit hooks
npx husky add .husky/pre-commit "npx lint-staged"
npx husky add .husky/pre-push "yarn lint:check"
# 4. Chạy định dạng ban đầu
yarn format
Tính Năng Tích Hợp VSCode
- Tự động sửa lỗi khi lưu: ESLint sẽ tự động sửa các vấn đề khi bạn lưu tập tin.
- Định dạng khi lưu: Prettier sẽ định dạng mã khi lưu tập tin.
- Tổ chức imports: Tự động tổ chức imports khi lưu.
- Kiểm tra lỗi thời gian thực: Hiển thị lỗi và cảnh báo ngay trong mã.
- Sửa lỗi nhanh: Nhân Ctrl/Cmd + . để có các gợi ý sửa lỗi.
Tiện Ích VSCode Cần Thiết
Cài đặt các tiện ích mở rộng sau để đảm bảo đầy đủ chức năng:
- ESLint (dbaeumer.vscode-eslint)
- Prettier (esbenp.prettier-vscode)
- TypeScript Importer (pmneo.tsimporter)
Sử Dụng
- Kiểm tra tất cả các tập tin:
yarn lint - Sửa các vấn đề có thể tự động sửa:
yarn lint:fix - Kiểm tra bất kỳ vấn đề nào:
yarn lint:check - Định dạng tất cả các tập tin:
yarn prettier - Định dạng và sửa:
yarn format
Vấn Đề Thường Gặp
react-hooks/exhaustive-depsbị tắt do tương thích với ESLint 9.- Sẽ được bật lại khi plugin cập nhật cho ESLint 9.
Sau Khi Thiết Lập
- Xóa bất kỳ tập tin
.eslintrc.*cũ. - Khởi động lại VSCode để áp dụng các cài đặt.
- Chạy
yarn formatđể định dạng mã hiện tại. - Cam kết các thay đổi để kích hoạt các pre-commit hooks.
Cài đặt này cung cấp một môi trường linting hoàn chỉnh, sẵn sàng cho sản xuất với sự tích hợp của VSCode và các kiểm tra chất lượng tự động.