rubyzen-lint 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/LICENSE +201 -0
- data/README.md +110 -0
- data/lib/rubyzen/cache/parse_cache.rb +36 -0
- data/lib/rubyzen/collections/attributes_collection.rb +32 -0
- data/lib/rubyzen/collections/base_collection.rb +30 -0
- data/lib/rubyzen/collections/blocks_collection.rb +27 -0
- data/lib/rubyzen/collections/call_site_collection.rb +52 -0
- data/lib/rubyzen/collections/classes_collection.rb +77 -0
- data/lib/rubyzen/collections/constants_collection.rb +11 -0
- data/lib/rubyzen/collections/declaration_collection.rb +11 -0
- data/lib/rubyzen/collections/file_collection.rb +81 -0
- data/lib/rubyzen/collections/macros_collection.rb +11 -0
- data/lib/rubyzen/collections/methods_collection.rb +67 -0
- data/lib/rubyzen/collections/modules_collection.rb +36 -0
- data/lib/rubyzen/collections/parameters_collection.rb +11 -0
- data/lib/rubyzen/collections/raises_collection.rb +26 -0
- data/lib/rubyzen/collections/requires_collection.rb +32 -0
- data/lib/rubyzen/collections/rescues_collection.rb +19 -0
- data/lib/rubyzen/declarations/attribute_declaration.rb +62 -0
- data/lib/rubyzen/declarations/block_declaration.rb +49 -0
- data/lib/rubyzen/declarations/call_site_declaration.rb +98 -0
- data/lib/rubyzen/declarations/class_declaration.rb +168 -0
- data/lib/rubyzen/declarations/constant_declaration.rb +155 -0
- data/lib/rubyzen/declarations/file_declaration.rb +69 -0
- data/lib/rubyzen/declarations/if_statement_declaration.rb +44 -0
- data/lib/rubyzen/declarations/macro_declaration.rb +81 -0
- data/lib/rubyzen/declarations/method_declaration.rb +63 -0
- data/lib/rubyzen/declarations/module_declaration.rb +115 -0
- data/lib/rubyzen/declarations/parameter_declaration.rb +43 -0
- data/lib/rubyzen/declarations/raise_declaration.rb +87 -0
- data/lib/rubyzen/declarations/require_declaration.rb +61 -0
- data/lib/rubyzen/declarations/rescue_declaration.rb +54 -0
- data/lib/rubyzen/lint.rb +1 -0
- data/lib/rubyzen/matchers/matcher_helpers.rb +176 -0
- data/lib/rubyzen/matchers/zen_empty_matcher.rb +54 -0
- data/lib/rubyzen/matchers/zen_false_matcher.rb +63 -0
- data/lib/rubyzen/matchers/zen_true_matcher.rb +57 -0
- data/lib/rubyzen/parsers/a_s_t_parser.rb +33 -0
- data/lib/rubyzen/project.rb +69 -0
- data/lib/rubyzen/providers/attributes_provider.rb +19 -0
- data/lib/rubyzen/providers/blocks_provider.rb +15 -0
- data/lib/rubyzen/providers/call_site_provider.rb +15 -0
- data/lib/rubyzen/providers/class_name_provider.rb +36 -0
- data/lib/rubyzen/providers/collection_filter_provider.rb +64 -0
- data/lib/rubyzen/providers/constants_provider.rb +17 -0
- data/lib/rubyzen/providers/file_path_provider.rb +26 -0
- data/lib/rubyzen/providers/if_statements_provider.rb +11 -0
- data/lib/rubyzen/providers/line_number_provider.rb +11 -0
- data/lib/rubyzen/providers/lines_of_code_provider.rb +11 -0
- data/lib/rubyzen/providers/macros_provider.rb +17 -0
- data/lib/rubyzen/providers/raises_provider.rb +19 -0
- data/lib/rubyzen/providers/requires_provider.rb +19 -0
- data/lib/rubyzen/providers/rescues_provider.rb +17 -0
- data/lib/rubyzen/providers/source_code_provider.rb +11 -0
- data/lib/rubyzen/providers/visibility_provider.rb +49 -0
- data/lib/rubyzen/version.rb +3 -0
- data/lib/rubyzen-lint.rb +1 -0
- data/lib/rubyzen.rb +98 -0
- data/rubyzen-lint.gemspec +28 -0
- metadata +148 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6e0bc97dbd17202e4245c3c8c6b1d16f7fcdef64eab20ff96ab835113e21fbd9
|
|
4
|
+
data.tar.gz: 4fb9b3a30754c283823a44e208bd4de9ca0a035db5733df418ef1e70bd98172d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c23bf87e539bdda57d28a5b9f0b5fdbbee847a88ce93bf012c92a1c00eaf48fe1f5e2a99bea6a2e070749c2c74451821026f742a2e4e50e12c5395ed4295b870
|
|
7
|
+
data.tar.gz: 189a609b17528f6d0898defda84762df30273f4607f4e97882d21d255b111d719818adb9148d452c4ff5b9d7c29dbcc5c68749f36d0e96c86740451d096dc778
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Rubyzen
|
|
2
|
+
|
|
3
|
+
Rubyzen is an architectural linter for Ruby that lets you write architectural lint rules as unit tests, inspired by [Konsist](https://github.com/LemonAppDev/konsist) (for Kotlin) and [Harmonize](https://github.com/perrystreetsoftware/Harmonize) (for Swift).
|
|
4
|
+
|
|
5
|
+
## Architectural linters in the era of AI-generated code
|
|
6
|
+
|
|
7
|
+
In the era of AI-generated code, architectural flaws and subtle bugs happen faster than ever. AI agents produce code that passes tests and looks reasonable but subtly violates your team's architecture. As more code is produced, faster, it becomes impractical for manual code reviews to catch all these violations.
|
|
8
|
+
|
|
9
|
+
Architectural lint rules act as deterministic guardrails. They catch the architectural or structural mistakes that AI introduces, such as calling the database from a presenter or performing business logic in a controller, before they get merged. And since they run as unit tests, they provide immediate feedback to the AI agents to fix their own code.
|
|
10
|
+
|
|
11
|
+
## Why Yet Another Linter?
|
|
12
|
+
|
|
13
|
+
Traditional linters such as [RuboCop](https://github.com/rubocop/rubocop) require dealing with the raw AST, which has a steep learning curve and makes rules hard to write, read, and maintain. Rubyzen abstracts away the AST details, providing a high-level API that allows developers to write lint rules in a more natural way — the same way we write tests.
|
|
14
|
+
|
|
15
|
+
## Advantages
|
|
16
|
+
|
|
17
|
+
- **Readable, Easy-to-Use API:** Rubyzen provides a high-level API to access files, classes, methods, parameters, and more, without having to deal with low-level AST operations.
|
|
18
|
+
|
|
19
|
+
- **Architectural Enforcement & Documentation:** By writing lint rules as tests, you can use the Given-When-Then style to enforce and document your architecture directly within the codebase.
|
|
20
|
+
|
|
21
|
+
- **Less Manual Reviews:** With architectural rules automatically enforced, code reviews can focus on complex issues instead of repeating the same architectural feedback.
|
|
22
|
+
|
|
23
|
+
- **AI-Friendly Feedback Loop:** When lint rules fail, the failure messages tell AI agents exactly what they violated and where, allowing them to self-correct their code.
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
Add Rubyzen to your Gemfile:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem 'rubyzen-lint', group: :test
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then run `bundle install`.
|
|
34
|
+
|
|
35
|
+
That's it. Rubyzen auto-discovers your project structure (`app/`, `lib/`, `src/`, `spec/`) from your project root. No environment variables or configuration needed.
|
|
36
|
+
|
|
37
|
+
## Write your first set of lint rules
|
|
38
|
+
|
|
39
|
+
Create a spec file anywhere in your project (e.g., `spec/architecture/sample_spec.rb`) and start enforcing your architecture:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
require 'rubyzen'
|
|
43
|
+
|
|
44
|
+
RSpec.describe 'Architecture rules' do
|
|
45
|
+
let(:project) { Rubyzen::Project.new }
|
|
46
|
+
let(:controllers) { project.files.with_paths('app/controllers/').classes }
|
|
47
|
+
let(:presenters) { project.files.with_paths('app/presenters/').classes }
|
|
48
|
+
|
|
49
|
+
it 'controllers do not call ActiveRecord directly' do
|
|
50
|
+
expect(controllers.all_methods.call_sites.with_name('where')).to zen_empty
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'controllers inherit from ApplicationController' do
|
|
54
|
+
expect(controllers).to zen_true { |c| c.superclass_name == 'ApplicationController' }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'presenters do not depend on repositories' do
|
|
58
|
+
expect(presenters.all_methods.call_sites).to zen_false { |cs|
|
|
59
|
+
cs.receiver&.end_with?('Repository')
|
|
60
|
+
}
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
You can find more sample lint rules in the [`sample_project/spec/`](sample_project/spec/) directory.
|
|
66
|
+
|
|
67
|
+
## Run your lint rules
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
bundle exec rspec spec/architecture/
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Custom Paths (Optional)
|
|
74
|
+
|
|
75
|
+
By default, `Rubyzen::Project.new` scans standard directories (`app/`, `lib/`, `src/`, `spec/`) from your project root. You can override this:
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
# In your spec file — scope to specific directories
|
|
79
|
+
project = Rubyzen::Project.new(['app/models', 'app/controllers'])
|
|
80
|
+
|
|
81
|
+
# Or in spec/spec_helper.rb — configure globally for all specs
|
|
82
|
+
Rubyzen.configure do |config|
|
|
83
|
+
config.paths = ['app', 'lib']
|
|
84
|
+
end
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## CI Integration
|
|
88
|
+
|
|
89
|
+
Add a step to your existing CI workflow to run your lint rules automatically when a PR is opened:
|
|
90
|
+
|
|
91
|
+
```yaml
|
|
92
|
+
- name: Run architecture lint rules
|
|
93
|
+
run: bundle exec rspec spec/architecture/
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## AI Agent Skills
|
|
97
|
+
|
|
98
|
+
Rubyzen includes agent skills in `.claude/skills/` (also symlinked at `.github/skills/`) that work with both Claude Code and GitHub Copilot:
|
|
99
|
+
|
|
100
|
+
| Skill | Purpose |
|
|
101
|
+
|---|---|
|
|
102
|
+
| `write-lint-rule` | Write an architectural lint rule using the Rubyzen API |
|
|
103
|
+
| `run-lint-rules` | Run sample project lint rules and verify the violations are detected |
|
|
104
|
+
| `expand-rubyzen` | Add a new Rubyzen API (Declaration + Provider + Collection) |
|
|
105
|
+
| `add-rubyzen-tests` | Write unit tests for Rubyzen's own components |
|
|
106
|
+
| `run-tests` | Run Rubyzen's unit test suite |
|
|
107
|
+
|
|
108
|
+
## Contributing
|
|
109
|
+
|
|
110
|
+
Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for instructions on setting up the project, enhancing the API, and adding tests.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'digest'
|
|
2
|
+
|
|
3
|
+
module Rubyzen
|
|
4
|
+
module Cache
|
|
5
|
+
# In-memory cache for parsed AST results, keyed by file path and SHA256 checksum.
|
|
6
|
+
# Automatically invalidates entries when file contents change.
|
|
7
|
+
class ParseCache
|
|
8
|
+
def initialize
|
|
9
|
+
@cache = {}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Returns the cached result for the given file, or yields to parse and cache it.
|
|
13
|
+
#
|
|
14
|
+
# @param file_path [String] absolute path to the file
|
|
15
|
+
# @yield block that parses the file and returns the result to cache
|
|
16
|
+
# @return [Object] the cached or freshly parsed result
|
|
17
|
+
def fetch_or_parse(file_path, &block)
|
|
18
|
+
checksum = file_checksum(file_path)
|
|
19
|
+
|
|
20
|
+
if @cache.key?(file_path) && @cache[file_path][:checksum] == checksum
|
|
21
|
+
@cache[file_path][:result]
|
|
22
|
+
else
|
|
23
|
+
result = block.call
|
|
24
|
+
@cache[file_path] = { checksum: checksum, result: result }
|
|
25
|
+
result
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def file_checksum(file_path)
|
|
32
|
+
Digest::SHA256.file(file_path).hexdigest
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module Rubyzen
|
|
2
|
+
module Collections
|
|
3
|
+
# Collection of attribute declarations (attr_reader, attr_writer, attr_accessor).
|
|
4
|
+
#
|
|
5
|
+
# @example Ensuring no class uses attr_accessor
|
|
6
|
+
# expect(controllers.attributes.accessors).to zen_empty
|
|
7
|
+
class AttributesCollection < BaseCollection
|
|
8
|
+
include Rubyzen::Providers::CollectionFilterProvider
|
|
9
|
+
|
|
10
|
+
# Returns only +attr_reader+ attributes.
|
|
11
|
+
#
|
|
12
|
+
# @return [AttributesCollection]
|
|
13
|
+
def readers
|
|
14
|
+
filter(&:reader?)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Returns only +attr_writer+ attributes.
|
|
18
|
+
#
|
|
19
|
+
# @return [AttributesCollection]
|
|
20
|
+
def writers
|
|
21
|
+
filter(&:writer?)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Returns only +attr_accessor+ attributes.
|
|
25
|
+
#
|
|
26
|
+
# @return [AttributesCollection]
|
|
27
|
+
def accessors
|
|
28
|
+
filter(&:accessor?)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module Rubyzen
|
|
2
|
+
module Collections
|
|
3
|
+
# Base collection class for all Rubyzen collections.
|
|
4
|
+
# Extends Array and replaces +select+/+reject+ with a single +filter+ method
|
|
5
|
+
# that preserves the collection subclass type.
|
|
6
|
+
#
|
|
7
|
+
# @example Filtering a collection with a block
|
|
8
|
+
# controllers.filter { |c| c.name.end_with?('Controller') }
|
|
9
|
+
class BaseCollection < Array
|
|
10
|
+
undef_method(:select)
|
|
11
|
+
undef_method(:reject)
|
|
12
|
+
|
|
13
|
+
# Filters elements by the given block, returning a new collection of the same type.
|
|
14
|
+
#
|
|
15
|
+
# @yield [element] block that returns truthy to keep the element
|
|
16
|
+
# @return [BaseCollection] a new collection containing only matching elements
|
|
17
|
+
# @return [Enumerator] if no block is given
|
|
18
|
+
def filter
|
|
19
|
+
return enum_for(:filter) unless block_given?
|
|
20
|
+
|
|
21
|
+
result = self.class.new
|
|
22
|
+
each do |elem|
|
|
23
|
+
result << elem if yield(elem)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
result
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Rubyzen
|
|
2
|
+
module Collections
|
|
3
|
+
# Collection of block declarations (do...end / { }) found in files or methods.
|
|
4
|
+
#
|
|
5
|
+
# @example Finding blocks passed to a specific method
|
|
6
|
+
# project.files.blocks.with_method_name('describe')
|
|
7
|
+
class BlocksCollection < BaseCollection
|
|
8
|
+
include Rubyzen::Providers::CollectionFilterProvider
|
|
9
|
+
|
|
10
|
+
# Filters blocks by the method name they are passed to.
|
|
11
|
+
#
|
|
12
|
+
# @param method_name [String] the method name to match
|
|
13
|
+
# @return [BlocksCollection]
|
|
14
|
+
def with_method_name(method_name)
|
|
15
|
+
filter { |block| block.method_name == method_name }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Returns all call sites found inside every block.
|
|
19
|
+
#
|
|
20
|
+
# @return [CallSiteCollection]
|
|
21
|
+
def call_sites
|
|
22
|
+
all_call_sites = flat_map(&:call_sites)
|
|
23
|
+
CallSiteCollection.new(all_call_sites)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
|
|
2
|
+
module Rubyzen
|
|
3
|
+
module Collections
|
|
4
|
+
# Collection of method call sites found in classes, methods, or blocks.
|
|
5
|
+
#
|
|
6
|
+
# @example Ensuring controllers do not call .where directly
|
|
7
|
+
# expect(controllers.that { have_call_sites_with_names('.where') }).to zen_empty
|
|
8
|
+
class CallSiteCollection < BaseCollection
|
|
9
|
+
include Rubyzen::Providers::CollectionFilterProvider
|
|
10
|
+
|
|
11
|
+
# Filters call sites by the receiver expression.
|
|
12
|
+
#
|
|
13
|
+
# @param receiver [String] the receiver to match
|
|
14
|
+
# @return [CallSiteCollection]
|
|
15
|
+
def with_receiver(receiver)
|
|
16
|
+
filter { |call_site| call_site.receiver == receiver }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Filters call sites by method name.
|
|
20
|
+
#
|
|
21
|
+
# @param name [String] the method name to match
|
|
22
|
+
# @return [CallSiteCollection]
|
|
23
|
+
def with_name(name)
|
|
24
|
+
filter { |call_site| call_site.name == name }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Alias for {#with_name}.
|
|
28
|
+
#
|
|
29
|
+
# @param method_name [String] the method name to match
|
|
30
|
+
# @return [CallSiteCollection]
|
|
31
|
+
def with_method_name(method_name)
|
|
32
|
+
with_name(method_name)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Filters call sites that include the given symbol argument.
|
|
36
|
+
#
|
|
37
|
+
# @param symbol [Symbol] the symbol argument to match
|
|
38
|
+
# @return [CallSiteCollection]
|
|
39
|
+
def with_symbol(symbol)
|
|
40
|
+
filter { |call_site| call_site.symbols.include?(symbol) }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Filters call sites that include the given keyword argument.
|
|
44
|
+
#
|
|
45
|
+
# @param keyword_arg [Symbol] the keyword argument to match
|
|
46
|
+
# @return [CallSiteCollection]
|
|
47
|
+
def with_keyword_arg(keyword_arg)
|
|
48
|
+
filter { |call_site| call_site.keyword_args.include?(keyword_arg) }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
module Rubyzen
|
|
2
|
+
module Collections
|
|
3
|
+
# Collection of class declarations with methods for navigating into
|
|
4
|
+
# child elements (methods, attributes, macros) and filtering by inheritance.
|
|
5
|
+
#
|
|
6
|
+
# @example Ensuring controllers inherit from ApplicationController
|
|
7
|
+
# expect(controllers.with_parent_prefix('ApplicationController')).not_to zen_empty
|
|
8
|
+
class ClassesCollection < BaseCollection
|
|
9
|
+
include Rubyzen::Providers::CollectionFilterProvider
|
|
10
|
+
|
|
11
|
+
# Returns all instance and class methods across every class in the collection.
|
|
12
|
+
#
|
|
13
|
+
# @return [MethodsCollection]
|
|
14
|
+
def all_methods
|
|
15
|
+
instance_plus_class_methods = flat_map { |klass| klass.instance_methods + klass.class_methods }
|
|
16
|
+
MethodsCollection.new(instance_plus_class_methods)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns all attribute declarations across every class.
|
|
20
|
+
#
|
|
21
|
+
# @return [AttributesCollection]
|
|
22
|
+
def attributes
|
|
23
|
+
all_attributes = flat_map(&:attributes)
|
|
24
|
+
AttributesCollection.new(all_attributes)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Returns all macro invocations across every class.
|
|
28
|
+
#
|
|
29
|
+
# @return [MacrosCollection]
|
|
30
|
+
def macros
|
|
31
|
+
all_macros = flat_map(&:macros)
|
|
32
|
+
MacrosCollection.new(all_macros)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Returns all rescue declarations across every class.
|
|
36
|
+
#
|
|
37
|
+
# @return [RescuesCollection]
|
|
38
|
+
def rescues
|
|
39
|
+
all_rescues = flat_map(&:rescues)
|
|
40
|
+
RescuesCollection.new(all_rescues)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Returns all raise declarations across every class.
|
|
44
|
+
#
|
|
45
|
+
# @return [RaisesCollection]
|
|
46
|
+
def raises
|
|
47
|
+
all_raises = flat_map(&:raises)
|
|
48
|
+
RaisesCollection.new(all_raises)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Filters classes whose superclass starts with the given prefix.
|
|
52
|
+
#
|
|
53
|
+
# @param prefix [String] the superclass name prefix to match
|
|
54
|
+
# @return [ClassesCollection]
|
|
55
|
+
def with_parent_prefix(prefix)
|
|
56
|
+
filter { |klass| klass.superclass_prefix?(prefix) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Filters classes that contain at least one macro with the given name.
|
|
60
|
+
#
|
|
61
|
+
# @param macro_name [String] the macro name to search for
|
|
62
|
+
# @return [ClassesCollection]
|
|
63
|
+
def with_macro_name(macro_name)
|
|
64
|
+
filter { |klass| klass.macros.with_name(macro_name).any? }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Merges two ClassesCollections into a new ClassesCollection.
|
|
68
|
+
#
|
|
69
|
+
# @param other [ClassesCollection] the collection to merge
|
|
70
|
+
# @return [ClassesCollection]
|
|
71
|
+
def +(other)
|
|
72
|
+
merged = super(other)
|
|
73
|
+
self.class.new(merged)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Rubyzen
|
|
2
|
+
module Collections
|
|
3
|
+
# Collection of constant declarations found in files, classes, or modules.
|
|
4
|
+
#
|
|
5
|
+
# @example Filtering constants by name
|
|
6
|
+
# project.files.constants.with_name('VERSION')
|
|
7
|
+
class ConstantsCollection < BaseCollection
|
|
8
|
+
include Rubyzen::Providers::CollectionFilterProvider
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Rubyzen
|
|
2
|
+
module Collections
|
|
3
|
+
# Generic collection for declaration objects that do not require
|
|
4
|
+
# specialized filtering methods (e.g., if statements).
|
|
5
|
+
#
|
|
6
|
+
# @example Collecting if-statement declarations from methods
|
|
7
|
+
# project.files.classes.all_methods.if_statements
|
|
8
|
+
class DeclarationCollection < BaseCollection
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|