Sha256: 4ff4b7cbf023a43108f6c92aabf607ea6f1d74c4e42b09ba432368d95b96b773
Contents?: true
Size: 1.34 KB
Versions: 21
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Lint # This cop checks for `:true` and `:false` symbols. # In most cases it would be a typo. # # @example # # # bad # :true # # # good # true # # @example # # # bad # :false # # # good # false class BooleanSymbol < Base extend AutoCorrector MSG = 'Symbol with a boolean name - ' \ 'you probably meant to use `%<boolean>s`.' def_node_matcher :boolean_symbol?, '(sym {:true :false})' def on_sym(node) return unless boolean_symbol?(node) parent = node.parent return if parent&.array_type? && parent&.percent_literal?(:symbol) add_offense(node, message: format(MSG, boolean: node.value)) do |corrector| autocorrect(corrector, node) end end private def autocorrect(corrector, node) boolean_literal = node.source.delete(':') parent = node.parent if parent&.pair_type? && node.equal?(parent.children[0]) corrector.remove(parent.loc.operator) boolean_literal = "#{node.source} =>" end corrector.replace(node, boolean_literal) end end end end end
Version data entries
21 entries across 21 versions & 1 rubygems