Sha256: 69fc31aba2d4e0d583918f39a6e1492bf4409a697b2cf931896c2f84f44bd51a

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'pathname'

module RuboCop
  module Cop
    module Naming
      # @example EnforcedStyle: bar (default)
      #   # Use only ASCII characters
      #
      #   # bad
      #   automação.rb
      #
      #   # good
      #   automacao.rb
      #
      class AsciiFileName < Base
        include RangeHelp

        MSG_ASCII = 'The name of this source file (`%<basename>s`) should use only ASCII characters.'

        def on_new_investigation
          file_path = processed_source.file_path
          return if config.file_to_exclude?(file_path)

          for_bad_filename(file_path) { |range, msg| add_offense(range, message: msg) }
        end

        private

        def for_bad_filename(file_path)
          basename = File.basename(file_path)

          msg = ascii_message(basename) unless filename_ascii?(basename) || bad_filename_allowed?

          yield source_range(processed_source.buffer, 1, 0), msg if msg
        end

        def bad_filename_allowed?
          ignore_executable_scripts? && processed_source.start_with?('#!')
        end

        def ascii_message(basename)
          format(MSG_ASCII, basename: basename)
        end

        def ignore_executable_scripts?
          cop_config['IgnoreExecutableScripts']
        end

        def filename_ascii?(basename)
          basename = basename.sub(/^\./, '')
          basename = basename.sub(/\.[^.]+$/, '')
          basename = basename.sub('+', '_')
          basename.ascii_only?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-livelo-0.5.3 lib/rubocop/cop/naming/ascii_file_name.rb