Skip to content

Commit

Permalink
Fix #145 Add cop for bad command title capitalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Eneroth3 committed Apr 19, 2023
1 parent 884032d commit f462afd
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ SketchupSuggestions/AddGroup:
Reference: https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#addgroup
Enabled: true

SketchupSuggestions/CommandTitle:
Description: Use title case for command titles.
Reference: https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#commandtitle
Enabled: true

SketchupSuggestions/Compatibility:
Description: Incompatible feature with target SketchUp version.
Reference: https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#compatibility
Expand Down
43 changes: 43 additions & 0 deletions lib/rubocop/sketchup/cop/suggestions/command_title.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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 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)
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
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ In the following section you find all available cops:
#### Department [SketchupSuggestions](cops_suggestions.md)

* [SketchupSuggestions/AddGroup](cops_suggestions.md#sketchupsuggestionsaddgroup)
* [SketchupSuggestions/CommandTitle](cops_suggestions.md#sketchupsuggestionscommandtitle)
* [SketchupSuggestions/Compatibility](cops_suggestions.md#sketchupsuggestionscompatibility)
* [SketchupSuggestions/DynamicComponentInternals](cops_suggestions.md#sketchupsuggestionsdynamiccomponentinternals)
* [SketchupSuggestions/FileEncoding](cops_suggestions.md#sketchupsuggestionsfileencoding)
Expand Down
17 changes: 17 additions & 0 deletions manual/cops_suggestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ face2 = group.entities.add_face(points2)

* [https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#addgroup](https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#addgroup)

<a name='commandtitle'></a>
## SketchupSuggestions/CommandTitle

Enabled by default | Supports autocorrection
--- | ---
Enabled | No

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.

### References

* [https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#commandtitle](https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_suggestions.md#commandtitle)

<a name='compatibility'></a>
## SketchupSuggestions/Compatibility

Expand Down
53 changes: 53 additions & 0 deletions spec/rubocop/cop/sketchup_suggestions/command_title_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require 'spec_helper'

describe RuboCop::Cop::SketchupSuggestions::CommandTitle do

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?
#
# 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

end

0 comments on commit f462afd

Please sign in to comment.