Sha256: d6aa26e7fd5e147051c613137a69f475e3360e817f022b22b92c2e6183f26497

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Sorbet
      # This cop ensures empty class/module definitions in RBI files are
      # done on a single line rather than being split across multiple lines.
      #
      # @example
      #
      #   # bad
      #   module SomeModule
      #   end
      #
      #   # good
      #   module SomeModule; end
      class SingleLineRbiClassModuleDefinitions < RuboCop::Cop::Cop
        MSG = "Empty class/module definitions in RBI files should be on a single line."

        def on_module(node)
          process_node(node)
        end

        def on_class(node)
          process_node(node)
        end

        def autocorrect(node)
          -> (corrector) { corrector.replace(node, convert_newlines(node.source)) }
        end

        protected

        def convert_newlines(source)
          source.sub(/[\r\n]+\s*[\r\n]*/, "; ")
        end

        def process_node(node)
          return if node.body
          return if node.single_line?
          add_offense(node)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-sorbet-0.7.0 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.11 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.10 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.9 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.8 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.7 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.6 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.5 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.4 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb
rubocop-sorbet-0.6.3 lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb