-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypeAliasName to ensure all constants used as T.type_alias are in…
… CamelCase Signed-off-by: Alexandre Terrasa <[email protected]>
- Loading branch information
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubocop" | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# This cop ensures all constants used as `T.type_alias` are using CamelCase. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# FOO_OR_BAR = T.type_alias { T.any(Foo, Bar) } | ||
# | ||
# # good | ||
# FooOrBar = T.type_alias { T.any(Foo, Bar) } | ||
class TypeAliasName < RuboCop::Cop::Cop | ||
MSG = "Type alias constant name should be in CamelCase" | ||
|
||
def_node_matcher(:casgn_type_alias?, <<-PATTERN) | ||
(casgn | ||
_ | ||
_ | ||
(block | ||
(send | ||
(const nil? :T) :type_alias) | ||
_ | ||
_ | ||
)) | ||
PATTERN | ||
|
||
def on_casgn(node) | ||
return unless casgn_type_alias?(node) | ||
|
||
name = node.children[1] | ||
|
||
# From https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cop/naming/class_and_module_camel_case.rb | ||
return unless /_/.match?(name) | ||
|
||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::TypeAliasName, :config) do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
MSG = "Type alias constant name should be in CamelCase" | ||
|
||
describe("offenses") do | ||
it("disallows naming a T.type_alias constant in snake_case") do | ||
expect_offense(<<~RB) | ||
A_B = T.type_alias { T.any(A, B) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{MSG} | ||
A_ = T.type_alias { T.any(A, B) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{MSG} | ||
A_0 = T.type_alias { T.any(A, B) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{MSG} | ||
CONSTANT_NAME = T.type_alias { T.any(A, B) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{MSG} | ||
RB | ||
end | ||
|
||
it("allows naming a T.type_alias constant in CamelCase") do | ||
expect_no_offenses(<<~RB) | ||
X = T.type_alias { T.any(A, B) } | ||
X0 = T.type_alias { X } | ||
Constant = T.type_alias { Foo } | ||
ConstantName = T.type_alias { T.any(A, B) } | ||
HTTP = T.type_alias { Foo } | ||
RB | ||
end | ||
|
||
it("martches only T.type_alias casgn") do | ||
expect_no_offenses(<<~RB) | ||
a_or_b = T.type_alias { T.any(A, B) } | ||
x = T.type_alias { X } | ||
constant = T.type_alias { Foo } | ||
constant_name = T.type_alias { T.any(A, B) } | ||
RB | ||
end | ||
end | ||
end |