Класс: RuboCop::Cop::Cask::ArrayAlphabetization Приватный
- Наследует:
- Base
- Object
- Base
- RuboCop::Cop::Cask::ArrayAlphabetization
- Расширен модулем:
- AutoCorrector
- Определён в:
- rubocops/cask/array_alphabetization.rb
Этот класс является частью приватного API. Этот класс можно использовать только в репозитории Homebrew/brew. По возможности третьим сторонам следует избегать использования этого класса, поскольку он может быть удалён или изменён без предупреждения.
Краткое описание методов экземпляра свернуть
- #on_send(node) ⇒ void приватный
- #recursively_find_comments(source, index, line) ⇒ String приватный
- #sort_array(source) ⇒ Array<String> приватный
Подробное описание методов экземпляра
#on_send(node) ⇒ void
Этот метод является частью приватного API. Этот метод можно использовать только в репозитории Homebrew/brew. По возможности третьим сторонам следует избегать использования этого метода, поскольку он может быть удалён или изменён без предупреждения.
Этот метод возвращает неопределённое значение.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # File 'rubocops/cask/array_alphabetization.rb', line 11 def on_send(node) return unless [:conflicts_with, :uninstall, :zap].include?(node.method_name) node.each_descendant(:pair).each do |pair| symbols = pair.children.select(&:sym_type?).map(&:value) next if symbols.intersect?([:signal, :script, :early_script, :args, :input]) pair.each_descendant(:array).each do |array| if array.children.length == 1 add_offense(array, message: "Avoid single-element arrays by removing the []") do |corrector| corrector.replace(array.source_range, array.children.first.source) end end next if array.children.length <= 1 sorted_array = sort_array(array.source.split("\n")).join("\n") next if array.source == sorted_array add_offense(array, message: "The array elements should be ordered alphabetically") do |corrector| corrector.replace(array.source_range, sorted_array) end end end end |
#recursively_find_comments(source, index, line) ⇒ String
Этот метод является частью приватного API. Этот метод можно использовать только в репозитории Homebrew/brew. По возможности третьим сторонам следует избегать использования этого метода, поскольку он может быть удалён или изменён без предупреждения.
68 69 70 71 72 73 74 | # File 'rubocops/cask/array_alphabetization.rb', line 68 def recursively_find_comments(source, index, line) if source.fetch(index - 1).strip.start_with?("#") return recursively_find_comments(source, index - 1, "#{source[index - 1]}\n#{line}") end line end |
#sort_array(source) ⇒ Array<String>
Этот метод является частью приватного API. Этот метод можно использовать только в репозитории Homebrew/brew. По возможности третьим сторонам следует избегать использования этого метода, поскольку он может быть удалён или изменён без предупреждения.
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 | # File 'rubocops/cask/array_alphabetization.rb', line 39 def sort_array(source) # Combine each comment with the line(s) below so that they remain in the same relative location combined_source = source.each_with_index.filter_map do |line, index| next if line.blank? next if line.strip.start_with?("#") next recursively_find_comments(source, index, line) end # Separate the lines into those that should be sorted and those that should not # i.e. skip the opening and closing brackets of the array. to_sort, to_keep = combined_source.partition { |line| !line.include?("[") && !line.include?("]") } # Sort the lines that should be sorted to_sort.sort! do |a, b| a_non_comment = a.split("\n").reject { |line| line.strip.start_with?("#") }.fetch(0) b_non_comment = b.split("\n").reject { |line| line.strip.start_with?("#") }.fetch(0) a_non_comment.downcase <=> b_non_comment.downcase || raise("Expected non-comment lines to be present") end # Merge the sorted lines and the unsorted lines, preserving the original positions of the unsorted lines combined_source.map do |line| next line if to_keep.include?(line) to_sort.shift || raise("Expected to_sort to be present") end end |
© 2009–present Homebrew contributors
Licensed under the BSD 2-Clause License.
https://docs.brew.sh/rubydoc/RuboCop/Cop/Cask/ArrayAlphabetization.html