-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Command title and description #167
base: main
Are you sure you want to change the base?
Changes from 5 commits
bab56ca
7eee0a2
b5ef4f1
1ae795c
099a42e
8cb1817
c81c5b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module SketchupSuggestions | ||
# SketchUp command titles should be in title case, e.g. "Make Component" | ||
# and "Follow Me". | ||
# | ||
# In English, capitalize the first letter of each word. Other languages | ||
# may have different rules. | ||
class CommandTitle < SketchUp::Cop | ||
|
||
def_node_matcher :init_entity?, <<-PATTERN | ||
(send (const (const nil? :UI) :Command ) :new ... ) | ||
PATTERN | ||
|
||
MESSAGE = 'Use title case in command titles. '\ | ||
'In English, capitalize the first letter of each word.' | ||
|
||
def on_send(node) | ||
return unless init_entity?(node) | ||
return if node.arguments.first.str_content.nil? | ||
return if title_case?(node.arguments.first.str_content) | ||
|
||
add_offense(node, message: MESSAGE) | ||
end | ||
|
||
private | ||
|
||
# REVIEW: Extract to where they can be re-used? | ||
def title_case?(text) | ||
text == title_case(text) | ||
end | ||
|
||
def title_case(text) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth a comment on how this differ from Ruby's own title case method. |
||
text.gsub(/^(.)/) { ::Regexp.last_match(1).upcase } | ||
.gsub(/\ (.)/) { " #{::Regexp.last_match(1).upcase}" } | ||
.gsub(/-(.)/) { "-#{::Regexp.last_match(1).upcase}" } | ||
.gsub(/(\.)$/, '') | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::SketchupSuggestions::CommandTitle do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this pass the tests we need to change this line to:
And then remove:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is what was done to all our existing cops to fix the build: |
||
|
||
subject(:cop) { described_class.new } | ||
|
||
bad_capitalization = [ | ||
'text', | ||
'text text', | ||
'Text text', | ||
'text Text', | ||
'Text.', | ||
'Text Text.', | ||
'Text text.', | ||
'text 2-Point', | ||
'text 2-point' | ||
] | ||
|
||
bad_capitalization.each do |keyword| | ||
it "registers an offense when using UI::Command.new(\"#{keyword}\")" do | ||
expect_offense(<<~RUBY, keyword: keyword) | ||
UI::Command.new("%{keyword}") | ||
^^^^^^^^^^^^^^^^^^{keyword}^^ Use title case in command titles. [...] | ||
RUBY | ||
end | ||
|
||
# TODO: Add for setter methods. | ||
# Can we test by how a local variable was defined? Or only by its name? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you want to test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking something like one node catcher than looks for |
||
# | ||
# command = UI::Command.new("Test") | ||
# command.menu_text = "%{keyword}" | ||
# | ||
# menu.add_item("%{keyword}") | ||
end | ||
|
||
good_capitalization = [ | ||
'Text', | ||
'Text Text', | ||
'Text 2-Point', | ||
'文本' | ||
] | ||
|
||
good_capitalization.each do |keyword| | ||
it "does not register an offense when using UI::Command.new(\"#{keyword}\")" do | ||
expect_no_offenses(<<~RUBY, keyword: keyword) | ||
UI::Command.new("%{keyword}") | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using UI::Command.new(variable)' do | ||
expect_no_offenses(<<~RUBY) | ||
UI::Command.new(variable) | ||
RUBY | ||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe in a
lib\rubocop\sketchup\formatting.rb
file? ARuboCop::SketchUp::Formatting
mixing module?Like
lib\rubocop\sketchup\namespace_checker.rb
?