rubocop-view_component 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +7 -0
- data/IMPLEMENTATION_SUMMARY.md +172 -0
- data/LICENSE.txt +21 -0
- data/PLAN.md +625 -0
- data/README.md +53 -0
- data/Rakefile +35 -0
- data/config/default.yml +33 -0
- data/lib/rubocop/cop/view_component/base.rb +41 -0
- data/lib/rubocop/cop/view_component/component_suffix.rb +33 -0
- data/lib/rubocop/cop/view_component/no_global_state.rb +58 -0
- data/lib/rubocop/cop/view_component/prefer_private_methods.rb +73 -0
- data/lib/rubocop/cop/view_component/prefer_slots.rb +105 -0
- data/lib/rubocop/cop/view_component_cops.rb +7 -0
- data/lib/rubocop/view_component/plugin.rb +31 -0
- data/lib/rubocop/view_component/version.rb +7 -0
- data/lib/rubocop/view_component.rb +10 -0
- data/lib/rubocop-view_component.rb +9 -0
- data/spec/rubocop/cop/view_component/component_suffix_spec.rb +86 -0
- data/spec/rubocop/cop/view_component/no_global_state_spec.rb +119 -0
- data/spec/rubocop/cop/view_component/prefer_private_methods_spec.rb +136 -0
- data/spec/rubocop/cop/view_component/prefer_slots_spec.rb +119 -0
- data/spec/spec_helper.rb +14 -0
- metadata +108 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d84d4738ad3da42500e46d9f0b2bb90160eb5501e6dc515fea63bf582917f1f1
|
|
4
|
+
data.tar.gz: 9376295000ba2f7c30e8f74e8c707371333e2681454ad402e4d33472f05576a7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a4058fb7e22e45a737703dccb110560b2360f66010feccc9afde757ac8551351872cfb9a4cc0022d511c453eb96bfddcb6adde9ecf16426aba8bad34aa4ffc09
|
|
7
|
+
data.tar.gz: 27297ff15bb964213a7017cbb37920b5c8950570a405c654a18bcedb8d742a6c6f46a873fa7239653e5ed70b09360fccd36d4fbd670bb5c78385a55266608409
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# RuboCop ViewComponent Extension - Implementation Summary
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Successfully implemented Phase 1 of the RuboCop ViewComponent extension following a Test-Driven Development (TDD) approach. All 36 tests pass, and the cops are fully functional.
|
|
6
|
+
|
|
7
|
+
## Implemented Cops
|
|
8
|
+
|
|
9
|
+
### 1. ViewComponent/ComponentSuffix
|
|
10
|
+
**Purpose**: Enforces that ViewComponent classes end with the `Component` suffix.
|
|
11
|
+
|
|
12
|
+
**Severity**: Warning
|
|
13
|
+
|
|
14
|
+
**Example**:
|
|
15
|
+
```ruby
|
|
16
|
+
# bad
|
|
17
|
+
class FooBar < ViewComponent::Base
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# good
|
|
21
|
+
class FooBarComponent < ViewComponent::Base
|
|
22
|
+
end
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. ViewComponent/NoGlobalState
|
|
26
|
+
**Purpose**: Prevents direct access to global state (params, request, session, cookies, flash) within ViewComponent classes.
|
|
27
|
+
|
|
28
|
+
**Severity**: Warning
|
|
29
|
+
|
|
30
|
+
**Detects**: Access to `params`, `request`, `session`, `cookies`, `flash`
|
|
31
|
+
|
|
32
|
+
**Example**:
|
|
33
|
+
```ruby
|
|
34
|
+
# bad
|
|
35
|
+
class UserComponent < ViewComponent::Base
|
|
36
|
+
def admin?
|
|
37
|
+
params[:admin]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# good
|
|
42
|
+
class UserComponent < ViewComponent::Base
|
|
43
|
+
def initialize(admin:)
|
|
44
|
+
@admin = admin
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def admin?
|
|
48
|
+
@admin
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 3. ViewComponent/PreferPrivateMethods
|
|
54
|
+
**Purpose**: Suggests making helper methods private in ViewComponents.
|
|
55
|
+
|
|
56
|
+
**Severity**: Convention
|
|
57
|
+
|
|
58
|
+
**Allowed Public Methods**: `initialize`, `call`, `before_render`, `before_render_check`, `render?`
|
|
59
|
+
|
|
60
|
+
**Example**:
|
|
61
|
+
```ruby
|
|
62
|
+
# bad
|
|
63
|
+
class CardComponent < ViewComponent::Base
|
|
64
|
+
def formatted_title
|
|
65
|
+
@title.upcase
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# good
|
|
70
|
+
class CardComponent < ViewComponent::Base
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def formatted_title
|
|
74
|
+
@title.upcase
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 4. ViewComponent/PreferSlots
|
|
80
|
+
**Purpose**: Detects parameters that accept HTML content and suggests using slots instead.
|
|
81
|
+
|
|
82
|
+
**Severity**: Warning
|
|
83
|
+
|
|
84
|
+
**Detected Patterns**:
|
|
85
|
+
- Parameters ending with `_html`
|
|
86
|
+
- Parameters ending with `_content`
|
|
87
|
+
- Parameters starting with `html_` (except `html_class`, `html_classes`, `html_id`, `html_tag`)
|
|
88
|
+
- Parameters named `content`
|
|
89
|
+
- Parameters with `html_safe` default values
|
|
90
|
+
|
|
91
|
+
**Example**:
|
|
92
|
+
```ruby
|
|
93
|
+
# bad
|
|
94
|
+
class ModalComponent < ViewComponent::Base
|
|
95
|
+
def initialize(title:, body_html:)
|
|
96
|
+
@title = title
|
|
97
|
+
@body_html = body_html
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# good
|
|
102
|
+
class ModalComponent < ViewComponent::Base
|
|
103
|
+
renders_one :body
|
|
104
|
+
|
|
105
|
+
def initialize(title:)
|
|
106
|
+
@title = title
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## File Structure
|
|
112
|
+
|
|
113
|
+
### Core Files
|
|
114
|
+
- `lib/rubocop/cop/view_component/base.rb` - Shared helper methods
|
|
115
|
+
- `lib/rubocop/cop/view_component/component_suffix.rb` - ComponentSuffix cop
|
|
116
|
+
- `lib/rubocop/cop/view_component/no_global_state.rb` - NoGlobalState cop
|
|
117
|
+
- `lib/rubocop/cop/view_component/prefer_private_methods.rb` - PreferPrivateMethods cop
|
|
118
|
+
- `lib/rubocop/cop/view_component/prefer_slots.rb` - PreferSlots cop
|
|
119
|
+
- `lib/rubocop/cop/view_component_cops.rb` - Requires all cops
|
|
120
|
+
- `config/default.yml` - Cop configurations
|
|
121
|
+
|
|
122
|
+
### Test Files
|
|
123
|
+
- `spec/rubocop/cop/view_component/component_suffix_spec.rb` - 10 tests
|
|
124
|
+
- `spec/rubocop/cop/view_component/no_global_state_spec.rb` - 10 tests
|
|
125
|
+
- `spec/rubocop/cop/view_component/prefer_private_methods_spec.rb` - 8 tests
|
|
126
|
+
- `spec/rubocop/cop/view_component/prefer_slots_spec.rb` - 8 tests
|
|
127
|
+
|
|
128
|
+
## Test Results
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
36 examples, 0 failures
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
All cops are working correctly and detecting violations as expected.
|
|
135
|
+
|
|
136
|
+
## Verification
|
|
137
|
+
|
|
138
|
+
Successfully tested with a sample file containing multiple violations:
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
class FooBar < ViewComponent::Base
|
|
142
|
+
def initialize(title:, body_html:)
|
|
143
|
+
@title = title
|
|
144
|
+
@body_html = body_html
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def helper_method
|
|
148
|
+
params[:id]
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**Detected Violations**:
|
|
154
|
+
1. ComponentSuffix: Class name should end with `Component`
|
|
155
|
+
2. NoGlobalState: Avoid accessing `params` directly
|
|
156
|
+
3. PreferPrivateMethods: `helper_method` should be private
|
|
157
|
+
4. PreferSlots: `body_html:` should use slots instead
|
|
158
|
+
|
|
159
|
+
## Design Decisions
|
|
160
|
+
|
|
161
|
+
1. **No Auto-correction**: All cops are detection-only (no `extend AutoCorrector`)
|
|
162
|
+
2. **Module-based Architecture**: Shared helpers in `ViewComponent::Base` module
|
|
163
|
+
3. **ViewComponent 4.0+ Target**: Focused on modern ViewComponent patterns
|
|
164
|
+
4. **RSpec Testing Framework**: Using RuboCop's official testing support
|
|
165
|
+
|
|
166
|
+
## Next Steps (Future Phases)
|
|
167
|
+
|
|
168
|
+
Phase 1 is complete. Future phases could include:
|
|
169
|
+
- Additional cops for other ViewComponent best practices
|
|
170
|
+
- Auto-correction support for simpler violations
|
|
171
|
+
- Performance optimizations
|
|
172
|
+
- Integration with CI/CD pipelines
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andy Waite
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|