lib/tapioca/compilers/symbol_table/symbol_generator.rb in tapioca-0.4.15 vs lib/tapioca/compilers/symbol_table/symbol_generator.rb in tapioca-0.4.16

- old
+ new

@@ -93,10 +93,11 @@ return if name.start_with?('#<') return if name.downcase == name return if alias_namespaced?(name) return if seen?(name) return unless parent_declares_constant?(name) + return if T::Enum === constant # T::Enum instances are defined via `compile_enums` mark_seen(name) compile_constant(name, constant) end @@ -181,10 +182,11 @@ [ compile_module_helpers(constant), compile_mixins(constant), compile_mixes_in_class_methods(constant), compile_props(constant), + compile_enums(constant), methods, ].select { |b| b != "" }.join("\n\n") end end @@ -215,10 +217,27 @@ indented("#{method} :#{name}, #{type}") end end.join("\n") end + sig { params(constant: Module).returns(String) } + def compile_enums(constant) + return "" unless constant < T::Enum + + enums = T.cast(constant, T::Enum).values.map do |enum_type| + enum_type.instance_variable_get(:@const_name).to_s + end + + content = [ + indented('enums do'), + *enums.map { |e| indented(" #{e} = new") }.join("\n"), + indented('end'), + ] + + content.join("\n") + end + sig { params(name: String, constant: Module).returns(T.nilable(String)) } def compile_subconstants(name, constant) output = constants_of(constant).sort.uniq.map do |constant_name| symbol = (name == "Object" ? "" : name) + "::#{constant_name}" subconstant = resolve_constant(symbol) @@ -343,12 +362,16 @@ # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing def method_missing(symbol, *args) end define_singleton_method(:include) do |mod| - before = singleton_class.ancestors - super(mod).tap do - mixins_from_modules[mod] = singleton_class.ancestors - before + begin + before = singleton_class.ancestors + super(mod).tap do + mixins_from_modules[mod] = singleton_class.ancestors - before + end + rescue Exception # rubocop:disable Lint/RescueException + # this is a best effort, bail if we can't perform this end end class << self def method_missing(symbol, *args)