|
| 1 | +# AI Agent SonarCloud Workflow |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document outlines the workflow for AI agents working with this codebase to ensure compliance with SonarCloud quality standards using the existing SonarQube extension. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- SonarQube extension is installed and configured in your IDE |
| 10 | +- Extension is connected to the SonarCloud project |
| 11 | +- Real-time analysis is enabled |
| 12 | + |
| 13 | +## Workflow Steps |
| 14 | + |
| 15 | +### 1. Before Making Any Changes |
| 16 | + |
| 17 | +#### Check Current State |
| 18 | +```bash |
| 19 | +# Run quality check |
| 20 | +./scripts/check-quality.sh |
| 21 | + |
| 22 | +# Check specific file |
| 23 | +./scripts/pre-change-check.sh src/path/to/file.js |
| 24 | +``` |
| 25 | + |
| 26 | +#### Review IDE Diagnostics |
| 27 | +- Open the file in your IDE |
| 28 | +- Check the Problems panel for SonarCloud issues |
| 29 | +- Look for SonarLint annotations in the code |
| 30 | +- Note any existing issues that should be fixed |
| 31 | + |
| 32 | +### 2. During Development |
| 33 | + |
| 34 | +#### Follow SonarCloud Patterns |
| 35 | +- **Code Style**: Match existing code patterns |
| 36 | +- **Error Handling**: Use try/catch for async operations |
| 37 | +- **Security**: Avoid eval(), innerHTML, document.write |
| 38 | +- **Complexity**: Keep functions small and focused |
| 39 | +- **Performance**: Avoid unnecessary computations |
| 40 | + |
| 41 | +#### Common SonarCloud Rules to Follow |
| 42 | + |
| 43 | +**JavaScript/React Specific:** |
| 44 | +- Use `const` and `let` instead of `var` |
| 45 | +- Prefer arrow functions for callbacks |
| 46 | +- Use proper error handling for async operations |
| 47 | +- Avoid console.log in production code |
| 48 | +- Use meaningful variable names |
| 49 | +- Keep functions under 50 lines when possible |
| 50 | + |
| 51 | +**React Component Patterns:** |
| 52 | +- Use hooks appropriately |
| 53 | +- Avoid direct state mutations |
| 54 | +- Use proper prop types |
| 55 | +- Handle component lifecycle correctly |
| 56 | + |
| 57 | +**Web3/Ethereum Patterns:** |
| 58 | +- Handle BigNumber operations carefully |
| 59 | +- Use proper error handling for contract calls |
| 60 | +- Validate addresses and parameters |
| 61 | +- Use safe arithmetic operations |
| 62 | + |
| 63 | +### 3. After Making Changes |
| 64 | + |
| 65 | +#### Verify Quality |
| 66 | +```bash |
| 67 | +# Quick check for common issues |
| 68 | +./scripts/check-quality.sh |
| 69 | + |
| 70 | +# Check modified file specifically |
| 71 | +./scripts/pre-change-check.sh src/path/to/modified/file.js |
| 72 | +``` |
| 73 | + |
| 74 | +#### IDE Verification |
| 75 | +- Check Problems panel for new issues |
| 76 | +- Ensure no new SonarCloud warnings appear |
| 77 | +- Verify existing issues are fixed (if touched) |
| 78 | + |
| 79 | +### 4. Quality Gates |
| 80 | + |
| 81 | +#### Must Fix (Blocker/Critical) |
| 82 | +- Security vulnerabilities |
| 83 | +- Major bugs or logic errors |
| 84 | +- Critical performance issues |
| 85 | + |
| 86 | +#### Should Fix (Major) |
| 87 | +- Code smells in modified files |
| 88 | +- Maintainability issues |
| 89 | +- Performance improvements |
| 90 | + |
| 91 | +#### Can Fix (Minor) |
| 92 | +- Style inconsistencies |
| 93 | +- Documentation improvements |
| 94 | +- Minor optimizations |
| 95 | + |
| 96 | +## Common SonarCloud Issues and Solutions |
| 97 | + |
| 98 | +### 1. Complexity Issues |
| 99 | +```javascript |
| 100 | +// ❌ Too complex |
| 101 | +function processData(data) { |
| 102 | + if (data && data.length > 0) { |
| 103 | + for (let i = 0; i < data.length; i++) { |
| 104 | + if (data[i].status === 'active') { |
| 105 | + // lots of nested logic... |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// ✅ Simplified |
| 112 | +function processData(data) { |
| 113 | + if (!data?.length) return; |
| 114 | + |
| 115 | + const activeItems = data.filter(item => item.status === 'active'); |
| 116 | + return activeItems.map(processActiveItem); |
| 117 | +} |
| 118 | + |
| 119 | +function processActiveItem(item) { |
| 120 | + // focused logic |
| 121 | +} |
| 122 | +``` |
| 123 | +
|
| 124 | +### 2. Async/Await Error Handling |
| 125 | +```javascript |
| 126 | +// ❌ No error handling |
| 127 | +async function fetchData() { |
| 128 | + const response = await fetch('/api/data'); |
| 129 | + return response.json(); |
| 130 | +} |
| 131 | + |
| 132 | +// ✅ Proper error handling |
| 133 | +async function fetchData() { |
| 134 | + try { |
| 135 | + const response = await fetch('/api/data'); |
| 136 | + if (!response.ok) { |
| 137 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 138 | + } |
| 139 | + return await response.json(); |
| 140 | + } catch (error) { |
| 141 | + console.error('Error fetching data:', error); |
| 142 | + throw error; |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | +
|
| 147 | +### 3. React Component Issues |
| 148 | +```javascript |
| 149 | +// ❌ Direct state mutation |
| 150 | +this.state.items.push(newItem); |
| 151 | + |
| 152 | +// ✅ Proper state update |
| 153 | +this.setState(prevState => ({ |
| 154 | + items: [...prevState.items, newItem] |
| 155 | +})); |
| 156 | +``` |
| 157 | +
|
| 158 | +### 4. Web3 Patterns |
| 159 | +```javascript |
| 160 | +// ❌ No validation |
| 161 | +function transfer(address, amount) { |
| 162 | + contract.transfer(address, amount); |
| 163 | +} |
| 164 | + |
| 165 | +// ✅ Proper validation |
| 166 | +function transfer(address, amount) { |
| 167 | + if (!ethers.utils.isAddress(address)) { |
| 168 | + throw new Error('Invalid address'); |
| 169 | + } |
| 170 | + if (amount <= 0) { |
| 171 | + throw new Error('Amount must be positive'); |
| 172 | + } |
| 173 | + return contract.transfer(address, amount); |
| 174 | +} |
| 175 | +``` |
| 176 | +
|
| 177 | +## AI Agent Best Practices |
| 178 | +
|
| 179 | +### 1. Proactive Quality Checking |
| 180 | +- Always check file quality before modifications |
| 181 | +- Use IDE diagnostics as primary source of truth |
| 182 | +- Run scripts to understand current state |
| 183 | +
|
| 184 | +### 2. Incremental Improvements |
| 185 | +- Fix existing issues when touching files |
| 186 | +- Don't introduce new quality issues |
| 187 | +- Maintain or improve overall quality score |
| 188 | +
|
| 189 | +### 3. Documentation |
| 190 | +- Update comments when changing logic |
| 191 | +- Document complex business rules |
| 192 | +- Explain Web3/blockchain specific patterns |
| 193 | +
|
| 194 | +### 4. Testing |
| 195 | +- Run existing tests after changes |
| 196 | +- Ensure no regressions |
| 197 | +- Add tests for new functionality |
| 198 | +
|
| 199 | +## Troubleshooting |
| 200 | +
|
| 201 | +### SonarQube Extension Issues |
| 202 | +- Restart VS Code if analysis stops working |
| 203 | +- Check extension status in VS Code |
| 204 | +- Verify connection to SonarCloud project |
| 205 | +
|
| 206 | +### Common Problems |
| 207 | +- **No diagnostics appearing**: Check extension configuration |
| 208 | +- **Too many false positives**: Review SonarCloud project settings |
| 209 | +- **Performance issues**: Consider excluding large files from analysis |
| 210 | +
|
| 211 | +## Resources |
| 212 | +
|
| 213 | +- [SonarCloud Documentation](https://docs.sonarcloud.io/) |
| 214 | +- [SonarLint VS Code Extension](https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarlint-vscode) |
| 215 | +- [Project CLAUDE.md](../CLAUDE.md) - AI agent guidelines |
| 216 | +- [Quality Check Script](../scripts/check-quality.sh) |
| 217 | +
|
| 218 | +## Summary |
| 219 | +
|
| 220 | +This workflow ensures that AI agents can effectively use the existing SonarQube extension to maintain code quality without requiring additional tools. The key is to leverage the real-time feedback from the IDE and follow established patterns in the codebase. |
0 commit comments