Class: Pedant::CheckConfusingVariableNames
- Defined in:
- lib/pedant/checks/confusing_variable_names.rb
Instance Attribute Summary
Attributes inherited from Check
Class Method Summary collapse
Instance Method Summary collapse
- #check(file, tree) ⇒ Object
-
#normalize(name) ⇒ Object
Two identifiers with the same normalization are likely to be confused.
- #run ⇒ Object
Methods inherited from Check
all, depends, #fail, #fatal, friendly_name, inherited, #initialize, initialize!, list, #pass, provides, ready?, #report, run_checks_in_dependency_order, #skip, #warn
Constructor Details
This class inherits a constructor from Pedant::Check
Class Method Details
.requires ⇒ Object
31 32 33 |
# File 'lib/pedant/checks/confusing_variable_names.rb', line 31 def self.requires super + [:trees] end |
Instance Method Details
#check(file, tree) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# 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 |
#normalize(name) ⇒ Object
Two identifiers with the same normalization are likely to be confused. E.g.: CA_list and ca_list, certlist and cert_list
38 39 40 |
# File 'lib/pedant/checks/confusing_variable_names.rb', line 38 def normalize name name.gsub(/_/, '').downcase end |
#run ⇒ Object
96 97 98 99 100 101 102 |
# 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 |