class RuboCop::Cop::Metrics::CollectionLiteralLength


.to_set
# Reading huge Set from external data source
# good
Set[1, 2, ‘…’, 10]
# Reasonably sized Set “literal”
# good<br><br>end<br>hash[row.to_i] = row.to_i
CSV.foreach(‘numbers.csv’, headers: true).each_with_object({}) do |row, hash|
# Reading huge Hash from external data source
# good
{ 1 => 1, 2 => 2, ‘…’ => ‘…’, 10 => 10}
# Reasonably sized Hash literal
# good
# File.readlines(‘numbers.txt’, chomp: true).map!(&:to_i)
# Reading huge Array from external data source
# good
[1, 2, ‘…’, 10]
# Reasonably sized Array literal
# good
Set[1, 2, ‘…’, 999_999_999]
# Huge Set “literal”
# bad
{ 1 => 1, 2 => 2, ‘…’ => ‘…’, 999_999_999 => 999_999_999}
# Huge Hash literal
# bad
[1, 2, ‘…’, 999_999_999]
# Huge Array literal
# bad
@example
JSON, YAML, etc.).
a database, fetched from an API, or read from a non-code file (CSV,
configuration or data that may be better extracted somewhere else, like
Checks for literals with extremely many entries. This is indicative of

def collection_threshold

def collection_threshold
  cop_config.fetch('LengthThreshold', Float::INFINITY)
end

def on_array(node)

def on_array(node)
  add_offense(node) if node.children.length >= collection_threshold
end

def on_index(node)

def on_index(node)
  add_offense(node) if node.arguments.length >= collection_threshold
end

def on_send(node)

def on_send(node)
  on_index(node) if node.method?(:[])
end