本文档定义了 RPA App 项目的代码规范和开发流程标准。
项目使用 Prettier 进行代码格式化,配置如下:
// .prettierrc.js
{
printWidth: 100, // 行宽限制
tabWidth: 2, // 缩进宽度
useTabs: false, // 使用空格
semi: true, // 语句末尾分号
singleQuote: true, // 使用单引号
jsxSingleQuote: false, // JSX中使用双引号
trailingComma: 'all', // 尾随逗号
bracketSpacing: true, // 对象括号间空格
arrowParens: 'avoid', // 箭头函数参数括号
}
# 格式化所有文件
npm run format
# 检查格式化
npm run format:check
TypeScript 规则
any
类型(警告)const
(错误)React 规则
React Native 规则
Import 规则
# 运行 ESLint 检查
npm run lint
# 自动修复可修复的问题
npm run lint:fix
# 修复 import 排序
npm run lint:imports
# TypeScript 类型检查
npm run type-check
# 完整检查(类型、lint、格式、测试)
npm run check-all
# 自动修复所有可修复的问题
npm run fix-all
<type>(<scope>): <subject>
<body>
<footer>
feat
: 新功能fix
: 修复 bugdocs
: 文档更新style
: 代码格式化refactor
: 重构代码perf
: 性能优化test
: 测试相关build
: 构建相关ci
: CI/CD 相关chore
: 其他杂项rpa
: RPA 功能相关(项目特定)android
: Android 平台相关web
: Web 调试相关http
: HTTP 服务相关ui
: 用户界面script
: 脚本执行config
: 配置相关deps
: 依赖更新# 好的提交信息
feat(http): 添加服务器健康检查功能
fix(ui): 修复重启按钮样式问题
docs: 更新 README 安装说明
rpa(script): 优化脚本执行错误处理
# 不好的提交信息
update code
fix bug
add feature
项目配置了以下 Git Hooks:
// ✅ 好的做法
interface User {
id: number;
name: string;
email?: string;
}
// ❌ 避免使用 any
const userData: any = {};
// ✅ 使用具体类型
const userData: User = {
id: 1,
name: 'John',
};
// ✅ 明确的参数和返回类型
function processUser(user: User): Promise<boolean> {
return Promise.resolve(true);
}
// ✅ 箭头函数
const calculateTotal = (items: Item[]): number => {
return items.reduce((sum, item) => sum + item.price, 0);
};
// ✅ 正确的导入顺序(自动排序)
import NetInfo from '@react-native-community/netinfo'; // 第三方库
import React, { useState, useEffect } from 'react'; // 第三方库
import { View, Text, Alert } from 'react-native'; // 第三方库
import { ScriptExecutor } from './components/ScriptExecutor'; // 内部模块
import { HttpService } from './services/HttpService'; // 内部模块
import { User, UserRole } from './types/User'; // 内部模块
// ✅ 默认导出
export default class RPAService {
// ...
}
// ✅ 命名导出
export { HttpService, ScriptManager };
导入语句按以下顺序自动排序:
每个组内按字母顺序排列,组间自动添加空行。
// ✅ 函数组件
interface Props {
title: string;
onPress?: () => void;
}
const CustomButton: React.FC<Props> = ({ title, onPress }) => {
return (
<TouchableOpacity style={styles.button} onPress={onPress}>
<Text style={styles.text}>{title}</Text>
</TouchableOpacity>
);
};
// ✅ StyleSheet.create
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
},
button: {
paddingHorizontal: 16,
paddingVertical: 8,
borderRadius: 4,
},
});
// ❌ 避免内联样式
<View style= />;
// ✅ 正确的 Hook 使用
const [isLoading, setIsLoading] = useState<boolean>(false);
const [data, setData] = useState<User[]>([]);
useEffect(() => {
fetchData();
}, []);
// ✅ 自定义 Hook
const useHttpService = () => {
const [service] = useState(() => HttpService.getInstance());
return service;
};
src/
├── components/ # React 组件
│ ├── common/ # 通用组件
│ └── screens/ # 页面组件
├── services/ # 服务类
├── utils/ # 工具函数
├── types/ # 类型定义
├── hooks/ # 自定义 Hooks
└── constants/ # 常量定义
UserProfile.tsx
)HttpService.ts
)formatUtils.ts
)User.ts
)apiConstants.ts
)// ✅ 组件文件 - 默认导出
export default UserProfile;
// ✅ 服务文件 - 命名导出
export { HttpService };
// ✅ 类型文件 - 命名导出
export interface User {
// ...
}
export type UserRole = 'admin' | 'user';
// ✅ 工具文件 - 命名导出
export const formatDate = (date: Date): string => {
// ...
};
项目包含了 VSCode 工作区配置 (.vscode/settings.json
):
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit", // 自动修复 ESLint 问题
"source.organizeImports": "explicit", // 自动排序导入
"source.removeUnusedImports": "explicit" // 移除未使用导入
},
"editor.formatOnSave": true // 保存时自动格式化
}
{
"typescript.preferences.organizeImportsIgnoreCase": false,
"typescript.preferences.organizeImportsCollation": "ordinal",
"typescript.updateImportsOnFileMove.enabled": "always",
"typescript.suggest.autoImports": true
}
必装扩展:
可选扩展:
开发前
git pull origin main
npm install
开发中
提交前
# 方式一:手动运行检查
npm run check-all
# 方式二:自动修复后提交
npm run fix-all
git add .
git commit -m "feat: 添加新功能"
# 方式三:直接提交(husky 会自动检查)
git add .
git commit -m "feat: 添加新功能"
推送前
git push origin feature-branch
每次提交前自动运行(通过 Husky):
代码格式化和 ESLint 检查 (lint-staged)
TypeScript 类型检查
tsc --noEmit
验证类型单元测试
文件大小检查
每次提交时自动检查提交信息格式是否符合规范。
建议在 CI/CD 流程中添加:
遵循这些规范将帮助我们维护高质量、一致性的代码库,提高团队协作效率。