class Pedant::CheckConfusingVariableNames

Public Class Methods

requires() click to toggle source
Calls superclass method Pedant::Check::requires
# File lib/pedant/checks/confusing_variable_names.rb, line 31
def self.requires
  super + [:trees]
end

Public Instance Methods

check(file, tree) click to toggle source
# File lib/pedant/checks/confusing_variable_names.rb, line 35
def check(file, tree)
  # Two identifiers with the same normalization are likely to be confused.
  # E.g.: CA_list and ca_list, certlist and cert_list
  def normalize name
    name.gsub(/_/, '').downcase
  end

  # Set of all declared and assigned-to names in the source.
  names = Set.new

  # Handle all local_var and global_var occurrences (both assignments and declarations).
  (tree.all(:Global) + tree.all(:Local)).each do |decl|
    decl.idents.each do |node|
      names << node.name      if node.is_a? Nasl::Identifier
      names << node.lval.name if node.is_a? Nasl::Assignment
    end
  end

  # Add every identifier from every assigned-to Nasl::Lvalue to the set of names.
  tree.all(:Assignment).map(&:lval).each do |lval|
    # Generate a nested array of Nasl::Identifier, representing the tree structure
    # of a Nasl::Lvalue.
    def lval_to_arr(lval)
      return lval if not lval.is_a?(Nasl::Lvalue)
      return [lval.ident, lval.indexes.map { |lval| lval_to_arr(lval)} ]
    end

    if lval.is_a?(Nasl::Lvalue)
      lval_to_arr(lval).flatten.each do |node|
        names << node.name if node.is_a?(Nasl::Identifier)
      end
    end

    if lval.is_a?(Nasl::Identifier)
      names << lval.name
    end
  end

  # Group names together that have the same normalized form.
  confusable_name_groups = {}
  names.each do |name|
    key = normalize(name)
    confusable_name_groups[key] ||= Set.new
    confusable_name_groups[key] << name
  end

  # Throw away the normalized forms, all we care about now is the groups.
  confusable_name_groups = confusable_name_groups.values

  # We only care about groups with more than one name in them.
  confusable_name_groups.map!(&:to_a).select! { |group| group.length > 1 }

  return if confusable_name_groups.length == 0

  warn
  report(:warn, "These sets of names differ only by capitalization or underscores:")
  confusable_name_groups.each do |names|
    report(:warn, "  #{names.join(', ')}")
  end
end
lval_to_arr(lval) click to toggle source

Generate a nested array of Nasl::Identifier, representing the tree structure of a Nasl::Lvalue.

# File lib/pedant/checks/confusing_variable_names.rb, line 57
def lval_to_arr(lval)
  return lval if not lval.is_a?(Nasl::Lvalue)
  return [lval.ident, lval.indexes.map { |lval| lval_to_arr(lval)} ]
end
normalize(name) click to toggle source

Two identifiers with the same normalization are likely to be confused. E.g.: CA_list and ca_list, certlist and cert_list

# File lib/pedant/checks/confusing_variable_names.rb, line 38
def normalize name
  name.gsub(/_/, '').downcase
end
run() click to toggle source
# File lib/pedant/checks/confusing_variable_names.rb, line 96
def run
  # This check will pass by default.
  pass

  # Run this check on the tree from every file.
  @kb[:trees].each { |file, tree| check(file, tree) }
end