|
| 1 | +# 🤖 Coding Agents & AI Contributors Guide |
| 2 | + |
| 3 | +This guide provides specific guidelines for AI coding agents (like Claude Code) contributing to React on Rails. It supplements the main [CONTRIBUTING.md](./CONTRIBUTING.md) with AI-specific workflows and patterns. |
| 4 | + |
| 5 | +## Quick Reference Commands |
| 6 | + |
| 7 | +### Essential Commands |
| 8 | + |
| 9 | +```bash |
| 10 | +# Install dependencies |
| 11 | +bundle && yarn |
| 12 | + |
| 13 | +# Run tests |
| 14 | +bundle exec rspec # All tests (from project root) |
| 15 | +cd spec/dummy && bundle exec rspec # Dummy app tests only |
| 16 | + |
| 17 | +# Linting & Formatting |
| 18 | +bundle exec rubocop # Ruby linting |
| 19 | +bundle exec rubocop [file_path] # Lint specific file |
| 20 | +# Note: yarn format requires local setup, format manually |
| 21 | + |
| 22 | +# Development |
| 23 | +cd spec/dummy && foreman start # Start dummy app with webpack |
| 24 | +``` |
| 25 | + |
| 26 | +### CI Compliance Checklist |
| 27 | + |
| 28 | +- [ ] `bundle exec rubocop` passes with no offenses |
| 29 | +- [ ] All RSpec tests pass |
| 30 | +- [ ] No trailing whitespace |
| 31 | +- [ ] Line length ≤120 characters |
| 32 | +- [ ] Security violations properly scoped with disable comments |
| 33 | + |
| 34 | +## Development Patterns for AI Contributors |
| 35 | + |
| 36 | +### 1. Task Management |
| 37 | + |
| 38 | +Always use TodoWrite tool for multi-step tasks to: |
| 39 | + |
| 40 | +- Track progress transparently |
| 41 | +- Show the user what's being worked on |
| 42 | +- Ensure no steps are forgotten |
| 43 | +- Mark tasks complete as you finish them |
| 44 | + |
| 45 | +```markdown |
| 46 | +Example workflow: |
| 47 | + |
| 48 | +1. Analyze the problem |
| 49 | +2. Create test cases |
| 50 | +3. Implement the fix |
| 51 | +4. Run tests |
| 52 | +5. Fix linting issues |
| 53 | +6. Update documentation |
| 54 | +``` |
| 55 | + |
| 56 | +### 2. Test-Driven Development |
| 57 | + |
| 58 | +When fixing bugs or adding features: |
| 59 | + |
| 60 | +1. **Create failing tests first** that reproduce the issue |
| 61 | +2. **Implement the minimal fix** to make tests pass |
| 62 | +3. **Add comprehensive test coverage** for edge cases |
| 63 | +4. **Verify all existing tests still pass** |
| 64 | + |
| 65 | +### 3. File Processing Guidelines |
| 66 | + |
| 67 | +When working with file generation or processing: |
| 68 | + |
| 69 | +- **Filter by extension**: Only process relevant files (e.g., `.js/.jsx/.ts/.tsx` for React components) |
| 70 | +- **Validate assumptions**: Don't assume all files in a directory are components |
| 71 | +- **Handle edge cases**: CSS modules, config files, etc. should be excluded appropriately |
| 72 | + |
| 73 | +Example from CSS module fix: |
| 74 | + |
| 75 | +```ruby |
| 76 | +COMPONENT_EXTENSIONS = /\.(jsx?|tsx?)$/ |
| 77 | + |
| 78 | +def filter_component_files(paths) |
| 79 | + paths.grep(COMPONENT_EXTENSIONS) |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | +## RuboCop Compliance Patterns |
| 84 | + |
| 85 | +### Common Fixes |
| 86 | + |
| 87 | +1. **Trailing Whitespace** |
| 88 | + |
| 89 | + ```ruby |
| 90 | + # Bad |
| 91 | + let(:value) { "test" } |
| 92 | + |
| 93 | + # Good |
| 94 | + let(:value) { "test" } |
| 95 | + ``` |
| 96 | + |
| 97 | +2. **Line Length (120 chars max)** |
| 98 | + |
| 99 | + ```ruby |
| 100 | + # Bad |
| 101 | + expect { eval(pack_content.gsub(/import.*from.*['"];/, "").gsub(/ReactOnRails\.register.*/, "")) }.not_to raise_error |
| 102 | + |
| 103 | + # Good |
| 104 | + sanitized_content = pack_content.gsub(/import.*from.*['"];/, "") |
| 105 | + .gsub(/ReactOnRails\.register.*/, "") |
| 106 | + expect { eval(sanitized_content) }.not_to raise_error |
| 107 | + ``` |
| 108 | + |
| 109 | +3. **Named Subjects (RSpec)** |
| 110 | + |
| 111 | + ```ruby |
| 112 | + # Bad |
| 113 | + describe "#method_name" do |
| 114 | + subject { instance.method_name(arg) } |
| 115 | + |
| 116 | + it "does something" do |
| 117 | + expect(subject).to eq "result" |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + # Good |
| 122 | + describe "#method_name" do |
| 123 | + subject(:method_result) { instance.method_name(arg) } |
| 124 | + |
| 125 | + it "does something" do |
| 126 | + expect(method_result).to eq "result" |
| 127 | + end |
| 128 | + end |
| 129 | + ``` |
| 130 | + |
| 131 | +4. **Security/Eval Violations** |
| 132 | + |
| 133 | + ```ruby |
| 134 | + # Bad |
| 135 | + expect { eval(dangerous_code) }.not_to raise_error |
| 136 | + |
| 137 | + # Good |
| 138 | + # rubocop:disable Security/Eval |
| 139 | + sanitized_content = dangerous_code.gsub(/harmful_pattern/, "") |
| 140 | + expect { eval(sanitized_content) }.not_to raise_error |
| 141 | + # rubocop:enable Security/Eval |
| 142 | + ``` |
| 143 | + |
| 144 | +### RuboCop Workflow |
| 145 | + |
| 146 | +1. Run `bundle exec rubocop [file]` to see violations |
| 147 | +2. Fix violations manually or with auto-correct where safe |
| 148 | +3. Re-run to verify fixes |
| 149 | +4. Use disable comments sparingly and with good reason |
| 150 | + |
| 151 | +## Testing Best Practices |
| 152 | + |
| 153 | +### Test Structure |
| 154 | + |
| 155 | +```ruby |
| 156 | +describe "FeatureName" do |
| 157 | + context "when condition A" do |
| 158 | + let(:setup) { create_test_condition } |
| 159 | + |
| 160 | + before do |
| 161 | + # Setup code |
| 162 | + end |
| 163 | + |
| 164 | + it "does expected behavior" do |
| 165 | + # Arrange, Act, Assert |
| 166 | + end |
| 167 | + end |
| 168 | +end |
| 169 | +``` |
| 170 | + |
| 171 | +### Test Fixtures |
| 172 | + |
| 173 | +- Create realistic test data that represents edge cases |
| 174 | +- Use descriptive names for fixtures and variables |
| 175 | +- Clean up after tests (handled by RSpec automatically in most cases) |
| 176 | + |
| 177 | +### CSS Module Testing Example |
| 178 | + |
| 179 | +```ruby |
| 180 | +# Create test fixtures |
| 181 | +Write.create("ComponentWithCSSModule.module.css", css_content) |
| 182 | +Write.create("ComponentWithCSSModule.jsx", jsx_content) |
| 183 | + |
| 184 | +# Test the behavior |
| 185 | +it "ignores CSS module files during pack generation" do |
| 186 | + generated_packs = PacksGenerator.instance.generate_packs_if_stale |
| 187 | + expect(generated_packs).not_to include("ComponentWithCSSModule.module.js") |
| 188 | +end |
| 189 | +``` |
| 190 | + |
| 191 | +## Git & PR Workflow |
| 192 | + |
| 193 | +### Branch Management |
| 194 | + |
| 195 | +```bash |
| 196 | +git checkout -b fix/descriptive-name |
| 197 | +# Make changes |
| 198 | +git add . |
| 199 | +git commit -m "Descriptive commit message |
| 200 | +
|
| 201 | +- Bullet points for major changes |
| 202 | +- Reference issue numbers |
| 203 | +- Include 🤖 Generated with Claude Code signature" |
| 204 | + |
| 205 | +git push -u origin fix/descriptive-name |
| 206 | +``` |
| 207 | + |
| 208 | +### Commit Message Format |
| 209 | + |
| 210 | +``` |
| 211 | +Brief description of the change |
| 212 | +
|
| 213 | +- Detailed bullet points of what changed |
| 214 | +- Why the change was needed |
| 215 | +- Any breaking changes or considerations |
| 216 | +
|
| 217 | +Fixes #issue_number |
| 218 | +
|
| 219 | +🤖 Generated with [Claude Code](https://claude.ai/code) |
| 220 | +
|
| 221 | +Co-Authored-By: Claude <noreply@anthropic.com> |
| 222 | +``` |
| 223 | + |
| 224 | +### PR Creation |
| 225 | + |
| 226 | +Use `gh pr create` with: |
| 227 | + |
| 228 | +- Clear title referencing the issue |
| 229 | +- Comprehensive description with summary and test plan |
| 230 | +- Link to the issue being fixed |
| 231 | +- Include the Claude Code signature |
| 232 | + |
| 233 | +## Common Pitfalls & Solutions |
| 234 | + |
| 235 | +### 1. File Path Issues |
| 236 | + |
| 237 | +- Always use absolute paths in tools |
| 238 | +- Check current working directory with `pwd` |
| 239 | +- Use proper path joining methods |
| 240 | + |
| 241 | +### 2. Test Environment |
| 242 | + |
| 243 | +- Run tests from correct directory (often project root) |
| 244 | +- Understand the difference between gem tests vs dummy app tests |
| 245 | +- Clean up test artifacts appropriately |
| 246 | + |
| 247 | +### 3. Dependency Management |
| 248 | + |
| 249 | +- Don't assume packages are installed globally |
| 250 | +- Use `bundle exec` for Ruby commands |
| 251 | +- Verify setup with `bundle && yarn` when needed |
| 252 | + |
| 253 | +### 4. RuboCop Configuration |
| 254 | + |
| 255 | +- Different rules may apply to different directories |
| 256 | +- Use `bundle exec rubocop` (not global rubocop) |
| 257 | +- Check `.rubocop.yml` files for project-specific rules |
| 258 | + |
| 259 | +## Debugging Workflow |
| 260 | + |
| 261 | +1. **Understand the Problem** |
| 262 | + - Read the issue carefully |
| 263 | + - Reproduce the bug if possible |
| 264 | + - Identify root cause |
| 265 | + |
| 266 | +2. **Create Minimal Test Case** |
| 267 | + - Write failing test that demonstrates issue |
| 268 | + - Keep it focused and minimal |
| 269 | + |
| 270 | +3. **Implement Fix** |
| 271 | + - Make smallest change possible |
| 272 | + - Ensure fix doesn't break existing functionality |
| 273 | + - Follow existing code patterns |
| 274 | + |
| 275 | +4. **Verify Solution** |
| 276 | + - All new tests pass |
| 277 | + - All existing tests still pass |
| 278 | + - RuboCop compliance maintained |
| 279 | + - Manual testing if applicable |
| 280 | + |
| 281 | +## IDE Configuration for AI Context |
| 282 | + |
| 283 | +When analyzing codebases, ignore these directories to avoid confusion: |
| 284 | + |
| 285 | +- `/coverage`, `/tmp`, `/gen-examples` |
| 286 | +- `/node_package/lib`, `/node_modules` |
| 287 | +- `/spec/dummy/app/assets/webpack` |
| 288 | +- `/spec/dummy/log`, `/spec/dummy/node_modules`, `/spec/dummy/tmp` |
| 289 | +- `/spec/react_on_rails/dummy-for-generators` |
| 290 | + |
| 291 | +## Communication with Human Maintainers |
| 292 | + |
| 293 | +- Be transparent about AI-generated changes |
| 294 | +- Explain reasoning behind implementation choices |
| 295 | +- Ask for clarification when requirements are ambiguous |
| 296 | +- Provide comprehensive commit messages and PR descriptions |
| 297 | +- Include test plans and verification steps |
| 298 | + |
| 299 | +## Resources |
| 300 | + |
| 301 | +- [Main Contributing Guide](./CONTRIBUTING.md) |
| 302 | +- [Pull Request Guidelines](./docs/contributor-info/pull-requests.md) |
| 303 | +- [Generator Testing](./docs/contributor-info/generator-testing.md) |
| 304 | +- [RuboCop Documentation](https://docs.rubocop.org/) |
| 305 | +- [RSpec Best Practices](https://rspec.info/) |
| 306 | + |
| 307 | +--- |
| 308 | + |
| 309 | +This guide evolves based on AI contributor experiences. Suggest improvements via issues or PRs! |
0 commit comments