Skip to content

Commit e4d78e0

Browse files
committed
Improve interaction with Cursor after testing
1 parent 05ce998 commit e4d78e0

File tree

8 files changed

+99
-66
lines changed

8 files changed

+99
-66
lines changed

lib/chatwerk/api.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module Chatwerk
88
module API
99
class << self
10-
def packages(package_path: nil)
10+
def packages(package_path: '')
1111
packages = Helpers.all_packages(package_path)
1212

1313
if packages.empty?
@@ -28,7 +28,7 @@ def package(package_path:)
2828
raise Chatwerk::Error.new(e, package_path:)
2929
end
3030

31-
def package_todos(package_path:, constant_name: nil)
31+
def package_todos(package_path:, constant_name: '')
3232
package = Helpers.find_package(package_path)
3333
constant_name = Helpers.normalize_constant_name(constant_name)
3434
violations = package.todos
@@ -42,7 +42,7 @@ def package_todos(package_path:, constant_name: nil)
4242
raise Chatwerk::Error.new(e, package_path:, constant_name:)
4343
end
4444

45-
def package_violations(package_path:, constant_name: nil)
45+
def package_violations(package_path:, constant_name: '')
4646
package = Helpers.find_package(package_path)
4747
constant_name = Helpers.normalize_constant_name(constant_name)
4848
violations = package.violations

lib/chatwerk/helpers.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def normalize_package_path(package_path)
2626

2727
def normalize_constant_name(constant_name)
2828
constant_name = constant_name.to_s.strip
29+
return '' if constant_name.empty?
30+
2931
constant_name.sub(/^(::)?/, '::')
3032
end
3133
module_function :normalize_constant_name
@@ -54,8 +56,11 @@ def find_package(path_pattern)
5456
return exact_match if exact_match
5557

5658
raise Chatwerk::Error, <<~ERROR
57-
Found multiple packages for #{path_pattern.inspect}. Please be more specific.
59+
Found multiple packages for #{path_pattern.inspect}:
60+
5861
#{packages.map(&:name).join("\n")}
62+
63+
Please use full path to specify the correct package.
5964
ERROR
6065
end
6166
end

lib/chatwerk/views/no_violations_view.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
module Chatwerk
44
module Views
55
class NoViolationsView < BaseView
6-
def template(package:, constant_name:)
7-
<<~MESSAGE
8-
No violations found in #{package.name.inspect} for #{constant_name.inspect}.
9-
Ensure that constant_name is given in the format of "::ConstantName" or "::ConstantName::NestedConstant".
10-
MESSAGE
6+
def template(package:, constant_name: nil)
7+
if constant_name.nil?
8+
<<~MESSAGE
9+
No violations found in #{package.name.inspect}.
10+
MESSAGE
11+
else
12+
<<~MESSAGE
13+
No violations found in #{package.name.inspect} for #{constant_name.inspect}.
14+
Ensure that constant_name is given in the format of "::ConstantName" or "::ConstantName::NestedConstant".
15+
MESSAGE
16+
end
1117
end
1218
end
1319
end

lib/chatwerk/views/violations_details_view.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ module Chatwerk
44
module Views
55
class ViolationsDetailsView < BaseView
66
def template(package:, violations:, constant_name:)
7-
relevant_violations = violations
8-
.anonymous_sources_with_locations
9-
.select { |c, _| c.start_with?(constant_name) }
7+
relevant_violations = violations.anonymous_sources_with_locations.select { |c, _| c.start_with?(constant_name) }
108

119
if relevant_violations.empty?
1210
Views::NoViolationsView.render(package:, constant_name:)

lib/chatwerk/views/violations_list_view.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@
33
module Chatwerk
44
module Views
55
class ViolationsListView < BaseView
6-
def template(violations:)
7-
say 'These constants violate package boundaries:'
8-
violations.anonymous_source_counts.each do |constant_name, source_counts|
9-
count = source_counts.values.sum
10-
say "#{constant_name}: #{format_count(count, 'violation')}"
6+
# The anonymous_source_counts method returns a hash as follows:
7+
# {
8+
# "::Core::User" => {
9+
# "User.find(_)" => 2,
10+
# "User.create(_)" => 1
11+
# }
12+
# "::Core::Product" => {
13+
# "Product.find(_)" => 1,
14+
# "Product.create(_)" => 1
15+
# }
16+
# }
17+
def template(package:, violations:)
18+
sums = violations.anonymous_source_counts.transform_values do |source_counts|
19+
source_counts.values.sum
20+
end
21+
22+
if sums.empty?
23+
NoViolationsView.render(package:)
24+
else
25+
sums.sort_by { |_, count| -count }.map do |constant_name, count|
26+
"#{constant_name} (#{format_count(count, 'violation')})\n"
27+
end.join
1128
end
1229
end
1330
end

spec/chatwerk/api_spec.rb

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
let(:constant_name) { '::TestPackage::TestClass' }
1010
let(:package) { instance_double('Packwerk::Package', name: 'packs/test_package') }
1111
let(:violations) { instance_double('QueryPackwerk::Violations') }
12+
let(:anonymous_source_counts) { { '::TestPackage::TestClass' => { 'example usage' => 1, 'another example' => 2 } } }
1213
let(:anonymous_sources_with_locations) { { '::TestPackage::TestClass' => { 'example usage' => ['app/models/test.rb:1'] } } }
1314

1415
before do
@@ -93,34 +94,31 @@
9394
allow(violations).to receive(:anonymous_sources_with_locations).and_return(anonymous_sources_with_locations)
9495
end
9596

96-
context 'without constant name' do
97-
let(:constant_name) { '' }
98-
99-
it 'returns the violations list view' do
100-
expect(described_class.package_todos(package_path: package_path)).to eq(<<~STRING)
101-
app/models/test.rb
102-
1: example usage
103-
STRING
104-
end
97+
it 'returns the violations details view' do
98+
expect(described_class.package_todos(package_path: package_path, constant_name: constant_name)).to eq(<<~STRING)
99+
app/models/test.rb
100+
1: example usage
101+
STRING
105102
end
106103

107-
context 'with constant name' do
108-
it 'returns the violations details view' do
109-
expect(described_class.package_todos(package_path: package_path, constant_name: constant_name)).to eq(<<~STRING)
110-
app/models/test.rb
111-
1: example usage
104+
context 'when no violations found' do
105+
it 'returns the no violations view' do
106+
allow(violations).to receive(:anonymous_sources_with_locations).and_return({})
107+
expect(described_class.package_todos(package_path: package_path, constant_name: constant_name).strip).to eq(<<~STRING.strip)
108+
No violations found in "packs/test_package" for "::TestPackage::TestClass".
109+
Ensure that constant_name is given in the format of "::ConstantName" or "::ConstantName::NestedConstant".
112110
STRING
113111
end
112+
end
114113

115-
context 'when no violations found' do
116-
let(:anonymous_sources_with_locations) { {} }
114+
context 'without constant name' do
115+
let(:constant_name) { '' }
117116

118-
it 'returns the no violations view' do
119-
expect(described_class.package_todos(package_path: package_path, constant_name: constant_name).strip).to eq(<<~STRING.strip)
120-
No violations found in "packs/test_package" for "::TestPackage::TestClass".
121-
Ensure that constant_name is given in the format of "::ConstantName" or "::ConstantName::NestedConstant".
122-
STRING
123-
end
117+
it 'returns the violations list view' do
118+
allow(violations).to receive(:anonymous_source_counts).and_return(anonymous_source_counts)
119+
expect(described_class.package_todos(package_path: package_path)).to eq(<<~STRING)
120+
::TestPackage::TestClass (3 violations)
121+
STRING
124122
end
125123
end
126124

@@ -141,37 +139,34 @@
141139
before do
142140
allow(Chatwerk::Helpers).to receive(:all_packages).and_return([package])
143141
allow(package).to receive(:violations).and_return(violations)
144-
allow(violations).to receive(:anonymous_sources_with_locations).and_return(anonymous_sources_with_locations)
145142
end
146143

147-
context 'without constant name' do
148-
let(:constant_name) { '' }
149-
150-
it 'returns the violations list view' do
151-
expect(described_class.package_violations(package_path: package_path)).to eq(<<~STRING)
152-
app/models/test.rb
153-
1: example usage
154-
STRING
155-
end
144+
it 'returns the violations details view' do
145+
allow(violations).to receive(:anonymous_sources_with_locations).and_return(anonymous_sources_with_locations)
146+
expect(described_class.package_violations(package_path: package_path, constant_name: constant_name)).to eq(<<~STRING)
147+
app/models/test.rb
148+
1: example usage
149+
STRING
156150
end
157151

158-
context 'with constant name' do
159-
it 'returns the violations details view' do
152+
context 'when no violations found' do
153+
it 'returns the no violations view' do
154+
allow(violations).to receive(:anonymous_sources_with_locations).and_return({})
160155
expect(described_class.package_violations(package_path: package_path, constant_name: constant_name)).to eq(<<~STRING)
161-
app/models/test.rb
162-
1: example usage
156+
No violations found in "packs/test_package" for "::TestPackage::TestClass".
157+
Ensure that constant_name is given in the format of "::ConstantName" or "::ConstantName::NestedConstant".
163158
STRING
164159
end
160+
end
165161

166-
context 'when no violations found' do
167-
let(:anonymous_sources_with_locations) { {} }
162+
context 'without constant name' do
163+
let(:constant_name) { '' }
168164

169-
it 'returns the no violations view' do
170-
expect(described_class.package_violations(package_path: package_path, constant_name: constant_name)).to eq(<<~STRING)
171-
No violations found in "packs/test_package" for "::TestPackage::TestClass".
172-
Ensure that constant_name is given in the format of "::ConstantName" or "::ConstantName::NestedConstant".
173-
STRING
174-
end
165+
it 'returns the violations list view' do
166+
allow(violations).to receive(:anonymous_source_counts).and_return(anonymous_source_counts)
167+
expect(described_class.package_violations(package_path: package_path)).to eq(<<~STRING)
168+
::TestPackage::TestClass (3 violations)
169+
STRING
175170
end
176171
end
177172

spec/chatwerk/helpers_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
'MyConstant' => '::MyConstant',
4949
'::MyConstant' => '::MyConstant',
5050
' MyConstant ' => '::MyConstant',
51-
nil => '::'
51+
nil => ''
5252
}
5353

5454
examples.each do |input, expected|

spec/chatwerk/views/violations_list_view_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33
RSpec.describe Chatwerk::Views::ViolationsListView do
44
context 'with multiple violations' do
55
it 'renders a list of violations with counts' do
6+
package = instance_double('Chatwerk::Package', name: 'packs/core')
67
violations = instance_double('Chatwerk::Violations')
78
allow(violations).to receive(:anonymous_source_counts).and_return({
89
'::Core::User' => { 'source1' => 2, 'source2' => 1 },
910
'::Core::Product' => { 'source3' => 1 }
1011
})
1112

12-
expect(described_class.render(violations:)).to eq(<<~OUTPUT)
13-
These constants violate package boundaries:
14-
::Core::User: 3 violations
15-
::Core::Product: 1 violation
13+
expect(described_class.render(package:, violations:)).to eq(<<~OUTPUT)
14+
::Core::User (3 violations)
15+
::Core::Product (1 violation)
16+
OUTPUT
17+
end
18+
end
19+
20+
context 'with no violations' do
21+
it 'renders a message indicating no violations' do
22+
package = instance_double('Chatwerk::Package', name: 'packs/core')
23+
violations = instance_double('Chatwerk::Violations')
24+
allow(violations).to receive(:anonymous_source_counts).and_return({})
25+
26+
expect(described_class.render(package:, violations:)).to eq(<<~OUTPUT)
27+
No violations found in "packs/core".
1628
OUTPUT
1729
end
1830
end

0 commit comments

Comments
 (0)