Skip to content

Commit 5f36543

Browse files
authored
Merge pull request #843 from Shopify/at-remove-t-enum
Migrate all `T::Enum` usages to bare Ruby classes
2 parents a36ecc8 + b2844c9 commit 5f36543

10 files changed

Lines changed: 161 additions & 103 deletions

File tree

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ Sorbet/EnforceSigilOrder:
4040

4141
Sorbet/ForbidTStruct:
4242
Enabled: true
43+
44+
Sorbet/ForbidTEnum:
45+
Enabled: true

lib/spoom/cli/deadcode.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def deadcode(*paths)
106106
index.definitions.each do |name, definitions|
107107
$stderr.puts " #{blue(name)}"
108108
definitions.each do |definition|
109-
$stderr.puts " #{yellow(definition.kind.serialize)} #{gray(definition.location.to_s)}"
109+
$stderr.puts " #{yellow(definition.kind.to_s)} #{gray(definition.location.to_s)}"
110110
end
111111
end
112112
$stderr.puts
@@ -116,7 +116,7 @@ def deadcode(*paths)
116116
$stderr.puts "\nReferences:"
117117
index.references.values.flatten.sort_by(&:name).each do |references|
118118
name = references.name
119-
kind = references.kind.serialize
119+
kind = references.kind.to_s
120120
loc = references.location.to_s
121121
$stderr.puts " #{blue(name)} #{yellow(kind)} #{gray(loc)}"
122122
end

lib/spoom/colors.rb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@
22
# frozen_string_literal: true
33

44
module Spoom
5-
class Color < T::Enum
6-
enums do
7-
CLEAR = new("\e[0m")
8-
BOLD = new("\e[1m")
5+
class Color
6+
#: String
7+
attr_reader :ansi_code
98

10-
BLACK = new("\e[30m")
11-
RED = new("\e[31m")
12-
GREEN = new("\e[32m")
13-
YELLOW = new("\e[33m")
14-
BLUE = new("\e[34m")
15-
MAGENTA = new("\e[35m")
16-
CYAN = new("\e[36m")
17-
WHITE = new("\e[37m")
18-
19-
LIGHT_BLACK = new("\e[90m")
20-
LIGHT_RED = new("\e[91m")
21-
LIGHT_GREEN = new("\e[92m")
22-
LIGHT_YELLOW = new("\e[93m")
23-
LIGHT_BLUE = new("\e[94m")
24-
LIGHT_MAGENTA = new("\e[95m")
25-
LIGHT_CYAN = new("\e[96m")
26-
LIGHT_WHITE = new("\e[97m")
9+
#: (String) -> void
10+
def initialize(ansi_code)
11+
@ansi_code = ansi_code
2712
end
2813

29-
#: -> String
30-
def ansi_code
31-
serialize
32-
end
14+
CLEAR = new("\e[0m") #: Color
15+
BOLD = new("\e[1m") #: Color
16+
17+
BLACK = new("\e[30m") #: Color
18+
RED = new("\e[31m") #: Color
19+
GREEN = new("\e[32m") #: Color
20+
YELLOW = new("\e[33m") #: Color
21+
BLUE = new("\e[34m") #: Color
22+
MAGENTA = new("\e[35m") #: Color
23+
CYAN = new("\e[36m") #: Color
24+
WHITE = new("\e[37m") #: Color
25+
26+
LIGHT_BLACK = new("\e[90m") #: Color
27+
LIGHT_RED = new("\e[91m") #: Color
28+
LIGHT_GREEN = new("\e[92m") #: Color
29+
LIGHT_YELLOW = new("\e[93m") #: Color
30+
LIGHT_BLUE = new("\e[94m") #: Color
31+
LIGHT_MAGENTA = new("\e[95m") #: Color
32+
LIGHT_CYAN = new("\e[96m") #: Color
33+
LIGHT_WHITE = new("\e[97m") #: Color
3334
end
3435

3536
module Colorize

lib/spoom/deadcode/definition.rb

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@ module Spoom
55
module Deadcode
66
# A definition is a class, module, method, constant, etc. being defined in the code
77
class Definition
8-
class Kind < T::Enum
9-
enums do
10-
AttrReader = new("attr_reader")
11-
AttrWriter = new("attr_writer")
12-
Class = new("class")
13-
Constant = new("constant")
14-
Method = new("method")
15-
Module = new("module")
8+
class Kind
9+
#: (String) -> void
10+
def initialize(name)
11+
@name = name
1612
end
17-
end
1813

19-
class Status < T::Enum
20-
enums do
21-
# A definition is marked as `ALIVE` if it has at least one reference with the same name
22-
ALIVE = new
23-
# A definition is marked as `DEAD` if it has no reference with the same name
24-
DEAD = new
25-
# A definition can be marked as `IGNORED` if it is not relevant for the analysis
26-
IGNORED = new
14+
# @override
15+
#: -> String
16+
def to_s
17+
@name
2718
end
19+
20+
AttrReader = new("attr_reader") #: Kind
21+
AttrWriter = new("attr_writer") #: Kind
22+
Class = new("class") #: Kind
23+
Constant = new("constant") #: Kind
24+
Method = new("method") #: Kind
25+
Module = new("module") #: Kind
26+
end
27+
28+
class Status
29+
# A definition is marked as `ALIVE` if it has at least one reference with the same name
30+
ALIVE = new #: Status
31+
# A definition is marked as `DEAD` if it has no reference with the same name
32+
DEAD = new #: Status
33+
# A definition can be marked as `IGNORED` if it is not relevant for the analysis
34+
IGNORED = new #: Status
2835
end
2936

3037
#: Kind

lib/spoom/deadcode/remover.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ def node_match_kind?(node, kind)
688688
node.is_a?(Prism::DefNode)
689689
when Definition::Kind::Module
690690
node.is_a?(Prism::ModuleNode)
691+
else
692+
raise Error, "Unsupported node kind: #{node.class}"
691693
end
692694
end
693695
end

lib/spoom/model/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def visit_call_node(node)
226226
current_namespace.mixins << Extend.new(arg.slice)
227227
end
228228
when :public, :private, :protected
229-
@visibility_stack << Visibility.from_serialized(node.name.to_s)
229+
@visibility_stack << Visibility.from_string(node.name.to_s)
230230
if node.arguments
231231
super
232232
@visibility_stack.pop

lib/spoom/model/model.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,34 @@ class AttrReader < Attr; end
192192
class AttrWriter < Attr; end
193193
class AttrAccessor < Attr; end
194194

195-
class Visibility < T::Enum
196-
enums do
197-
Public = new("public")
198-
Protected = new("protected")
199-
Private = new("private")
195+
class Visibility
196+
class << self
197+
#: (String) -> Visibility
198+
def from_string(name)
199+
case name
200+
when "public" then Public
201+
when "protected" then Protected
202+
when "private" then Private
203+
else
204+
raise Error, "Invalid visibility: #{name}"
205+
end
206+
end
207+
end
208+
209+
#: (String) -> void
210+
def initialize(name)
211+
@name = name
200212
end
213+
214+
# @override
215+
#: -> String
216+
def to_s
217+
@name
218+
end
219+
220+
Public = new("public") #: Visibility
221+
Protected = new("protected") #: Visibility
222+
Private = new("private") #: Visibility
201223
end
202224

203225
# A mixin (include, prepend, extend) to a namespace

lib/spoom/model/reference.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ class Model
88
# Constants could be classes, modules, or actual constants.
99
# Methods could be accessors, instance or class methods, aliases, etc.
1010
class Reference
11-
class Kind < T::Enum
12-
enums do
13-
Constant = new("constant")
14-
Method = new("method")
11+
class Kind
12+
#: (String) -> void
13+
def initialize(name)
14+
@name = name
1515
end
16+
17+
#: -> String
18+
def to_s
19+
@name
20+
end
21+
22+
Constant = new("constant") #: Kind
23+
Method = new("method") #: Kind
1624
end
1725

1826
class << self

rbi/spoom.rbi

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ module Spoom::Cli::Helper
8585
def yellow(string); end
8686
end
8787

88+
Spoom::Cli::Helper::HIGHLIGHT_COLOR = T.let(T.unsafe(nil), Spoom::Color)
89+
8890
class Spoom::Cli::Main < ::Thor
8991
include ::Spoom::Colorize
9092
include ::Spoom::Cli::Helper
@@ -214,32 +216,33 @@ Spoom::Cli::Srb::Tc::SORT_CODE = T.let(T.unsafe(nil), String)
214216
Spoom::Cli::Srb::Tc::SORT_ENUM = T.let(T.unsafe(nil), Array)
215217
Spoom::Cli::Srb::Tc::SORT_LOC = T.let(T.unsafe(nil), String)
216218

217-
class Spoom::Color < ::T::Enum
218-
enums do
219-
BLACK = new
220-
BLUE = new
221-
BOLD = new
222-
CLEAR = new
223-
CYAN = new
224-
GREEN = new
225-
LIGHT_BLACK = new
226-
LIGHT_BLUE = new
227-
LIGHT_CYAN = new
228-
LIGHT_GREEN = new
229-
LIGHT_MAGENTA = new
230-
LIGHT_RED = new
231-
LIGHT_WHITE = new
232-
LIGHT_YELLOW = new
233-
MAGENTA = new
234-
RED = new
235-
WHITE = new
236-
YELLOW = new
237-
end
219+
class Spoom::Color
220+
sig { params(ansi_code: ::String).void }
221+
def initialize(ansi_code); end
238222

239223
sig { returns(::String) }
240224
def ansi_code; end
241225
end
242226

227+
Spoom::Color::BLACK = T.let(T.unsafe(nil), Spoom::Color)
228+
Spoom::Color::BLUE = T.let(T.unsafe(nil), Spoom::Color)
229+
Spoom::Color::BOLD = T.let(T.unsafe(nil), Spoom::Color)
230+
Spoom::Color::CLEAR = T.let(T.unsafe(nil), Spoom::Color)
231+
Spoom::Color::CYAN = T.let(T.unsafe(nil), Spoom::Color)
232+
Spoom::Color::GREEN = T.let(T.unsafe(nil), Spoom::Color)
233+
Spoom::Color::LIGHT_BLACK = T.let(T.unsafe(nil), Spoom::Color)
234+
Spoom::Color::LIGHT_BLUE = T.let(T.unsafe(nil), Spoom::Color)
235+
Spoom::Color::LIGHT_CYAN = T.let(T.unsafe(nil), Spoom::Color)
236+
Spoom::Color::LIGHT_GREEN = T.let(T.unsafe(nil), Spoom::Color)
237+
Spoom::Color::LIGHT_MAGENTA = T.let(T.unsafe(nil), Spoom::Color)
238+
Spoom::Color::LIGHT_RED = T.let(T.unsafe(nil), Spoom::Color)
239+
Spoom::Color::LIGHT_WHITE = T.let(T.unsafe(nil), Spoom::Color)
240+
Spoom::Color::LIGHT_YELLOW = T.let(T.unsafe(nil), Spoom::Color)
241+
Spoom::Color::MAGENTA = T.let(T.unsafe(nil), Spoom::Color)
242+
Spoom::Color::RED = T.let(T.unsafe(nil), Spoom::Color)
243+
Spoom::Color::WHITE = T.let(T.unsafe(nil), Spoom::Color)
244+
Spoom::Color::YELLOW = T.let(T.unsafe(nil), Spoom::Color)
245+
243246
module Spoom::Colorize
244247
sig { params(string: ::String, color: ::Spoom::Color).returns(::String) }
245248
def set_color(string, *color); end
@@ -1154,25 +1157,25 @@ class Spoom::Deadcode::Definition
11541157
def to_json(*args); end
11551158
end
11561159

1157-
class Spoom::Deadcode::Definition::Kind < ::T::Enum
1158-
enums do
1159-
AttrReader = new
1160-
AttrWriter = new
1161-
Class = new
1162-
Constant = new
1163-
Method = new
1164-
Module = new
1165-
end
1166-
end
1160+
class Spoom::Deadcode::Definition::Kind
1161+
sig { params(name: ::String).void }
1162+
def initialize(name); end
11671163

1168-
class Spoom::Deadcode::Definition::Status < ::T::Enum
1169-
enums do
1170-
ALIVE = new
1171-
DEAD = new
1172-
IGNORED = new
1173-
end
1164+
sig { override.returns(::String) }
1165+
def to_s; end
11741166
end
11751167

1168+
Spoom::Deadcode::Definition::Kind::AttrReader = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Kind)
1169+
Spoom::Deadcode::Definition::Kind::AttrWriter = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Kind)
1170+
Spoom::Deadcode::Definition::Kind::Class = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Kind)
1171+
Spoom::Deadcode::Definition::Kind::Constant = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Kind)
1172+
Spoom::Deadcode::Definition::Kind::Method = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Kind)
1173+
Spoom::Deadcode::Definition::Kind::Module = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Kind)
1174+
class Spoom::Deadcode::Definition::Status; end
1175+
Spoom::Deadcode::Definition::Status::ALIVE = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Status)
1176+
Spoom::Deadcode::Definition::Status::DEAD = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Status)
1177+
Spoom::Deadcode::Definition::Status::IGNORED = T.let(T.unsafe(nil), Spoom::Deadcode::Definition::Status)
1178+
11761179
class Spoom::Deadcode::ERB < ::Erubi::Engine
11771180
sig { params(input: T.untyped, properties: T.untyped).void }
11781181
def initialize(input, properties = T.unsafe(nil)); end
@@ -2602,13 +2605,17 @@ class Spoom::Model::Reference
26022605
end
26032606
end
26042607

2605-
class Spoom::Model::Reference::Kind < ::T::Enum
2606-
enums do
2607-
Constant = new
2608-
Method = new
2609-
end
2608+
class Spoom::Model::Reference::Kind
2609+
sig { params(name: ::String).void }
2610+
def initialize(name); end
2611+
2612+
sig { returns(::String) }
2613+
def to_s; end
26102614
end
26112615

2616+
Spoom::Model::Reference::Kind::Constant = T.let(T.unsafe(nil), Spoom::Model::Reference::Kind)
2617+
Spoom::Model::Reference::Kind::Method = T.let(T.unsafe(nil), Spoom::Model::Reference::Kind)
2618+
26122619
class Spoom::Model::ReferencesVisitor < ::Spoom::Visitor
26132620
sig { params(file: ::String).void }
26142621
def initialize(file); end
@@ -2758,14 +2765,22 @@ class Spoom::Model::UnresolvedSymbol < ::Spoom::Model::Symbol
27582765
def to_s; end
27592766
end
27602767

2761-
class Spoom::Model::Visibility < ::T::Enum
2762-
enums do
2763-
Private = new
2764-
Protected = new
2765-
Public = new
2768+
class Spoom::Model::Visibility
2769+
sig { params(name: ::String).void }
2770+
def initialize(name); end
2771+
2772+
sig { override.returns(::String) }
2773+
def to_s; end
2774+
2775+
class << self
2776+
sig { params(name: ::String).returns(::Spoom::Model::Visibility) }
2777+
def from_string(name); end
27662778
end
27672779
end
27682780

2781+
Spoom::Model::Visibility::Private = T.let(T.unsafe(nil), Spoom::Model::Visibility)
2782+
Spoom::Model::Visibility::Protected = T.let(T.unsafe(nil), Spoom::Model::Visibility)
2783+
Spoom::Model::Visibility::Public = T.let(T.unsafe(nil), Spoom::Model::Visibility)
27692784
class Spoom::ParseError < ::Spoom::Error; end
27702785

27712786
class Spoom::Poset

test/spoom/model/builder_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def m7; end
357357
model.symbols.values
358358
.flat_map(&:definitions)
359359
.grep(Method)
360-
.map { |d| "#{d.full_name}: #{T.cast(d, Method).visibility.serialize}" },
360+
.map { |d| "#{d.full_name}: #{d.visibility}" },
361361
)
362362
end
363363

@@ -394,7 +394,7 @@ def m6; end
394394
model.symbols.values
395395
.flat_map(&:definitions)
396396
.grep(Method)
397-
.map { |d| "#{d.full_name}: #{T.cast(d, Method).visibility.serialize}" },
397+
.map { |d| "#{d.full_name}: #{d.visibility}" },
398398
)
399399
end
400400

0 commit comments

Comments
 (0)