/
formula.rb
213 lines (176 loc) 路 6.8 KB
/
formula.rb
1
2
3
4
5
6
7
8
9
10
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# typed: false
# frozen_string_literal: true
require "extend/string"
require "rubocops/shared/helper_functions"
module RuboCop
module Cop
# Superclass for all formula cops.
#
# @api private
class FormulaCop < Base
include RangeHelp
include HelperFunctions
attr_accessor :file_path
@registry = Cop.registry
# This method is called by RuboCop and is the main entry point.
def on_class(node)
@file_path = processed_source.buffer.name
return unless file_path_allowed?
return unless formula_class?(node)
return unless respond_to?(:audit_formula)
class_node, parent_class_node, @body = *node
@formula_name = Pathname.new(@file_path).basename(".rb").to_s
@tap_style_exceptions = nil
audit_formula(node, class_node, parent_class_node, @body)
end
# Yields to block when there is a match.
#
# @param urls [Array] url/mirror method call nodes
# @param regex [Regexp] pattern to match URLs
def audit_urls(urls, regex)
urls.each_with_index do |url_node, index|
url_string_node = parameters(url_node).first
url_string = string_content(url_string_node)
match_object = regex_match_group(url_string_node, regex)
next unless match_object
offending_node(url_string_node.parent)
yield match_object, url_string, index
end
end
# Returns nil if does not depend on dependency_name.
#
# @param dependency_name dependency's name
def depends_on?(dependency_name, *types)
return if @body.nil?
types = [:any] if types.empty?
dependency_nodes = find_every_method_call_by_name(@body, :depends_on)
idx = dependency_nodes.index do |n|
types.any? { |type| depends_on_name_type?(n, dependency_name, type) }
end
return if idx.nil?
@offensive_node = dependency_nodes[idx]
end
# Returns true if given dependency name and dependency type exist in given dependency method call node.
# TODO: Add case where key of hash is an array
def depends_on_name_type?(node, name = nil, type = :required)
name_match = if name
false
else
true # Match only by type when name is nil
end
case type
when :required
type_match = required_dependency?(node)
name_match ||= required_dependency_name?(node, name) if type_match
when :build, :test, :optional, :recommended
type_match = dependency_type_hash_match?(node, type)
name_match ||= dependency_name_hash_match?(node, name) if type_match
when :any
type_match = true
name_match ||= required_dependency_name?(node, name)
name_match ||= dependency_name_hash_match?(node, name)
else
type_match = false
end
@offensive_node = node if type_match || name_match
type_match && name_match
end
def_node_search :required_dependency?, <<~EOS
(send nil? :depends_on ({str sym} _))
EOS
def_node_search :required_dependency_name?, <<~EOS
(send nil? :depends_on ({str sym} %1))
EOS
def_node_search :dependency_type_hash_match?, <<~EOS
(hash (pair ({str sym} _) ({str sym} %1)))
EOS
def_node_search :dependency_name_hash_match?, <<~EOS
(hash (pair ({str sym} %1) (...)))
EOS
# Return all the caveats' string nodes in an array.
def caveats_strings
return [] if @body.nil?
find_strings(find_method_def(@body, :caveats))
end
# Returns the sha256 str node given a sha256 call node.
def get_checksum_node(call)
return if parameters(call).empty? || parameters(call).nil?
if parameters(call).first.str_type?
parameters(call).first
# sha256 is passed as a key-value pair in bottle blocks
elsif parameters(call).first.hash_type?
if parameters(call).first.keys.first.value == :cellar
# sha256 :cellar :any, :tag "hexdigest"
parameters(call).first.values.last
elsif parameters(call).first.keys.first.is_a?(RuboCop::AST::SymbolNode)
# sha256 :tag "hexdigest"
parameters(call).first.values.first
else
# Legacy bottle block syntax
# sha256 "hexdigest" => :tag
parameters(call).first.keys.first
end
end
end
# Yields to a block with comment text as parameter.
def audit_comments
processed_source.comments.each do |comment_node|
@offensive_node = comment_node
yield comment_node.text
end
end
# Returns true if the formula is versioned.
def versioned_formula?
@formula_name.include?("@")
end
# Returns the formula tap.
def formula_tap
return unless (match_obj = @file_path.match(%r{/(homebrew-\w+)/}))
match_obj[1]
end
# Returns whether the given formula exists in the given style exception list.
# Defaults to the current formula being checked.
def tap_style_exception?(list, formula = nil)
if @tap_style_exceptions.nil? && !formula_tap.nil?
@tap_style_exceptions = {}
style_exceptions_dir = "#{File.dirname(File.dirname(@file_path))}/style_exceptions/*.json"
Pathname.glob(style_exceptions_dir).each do |exception_file|
list_name = exception_file.basename.to_s.chomp(".json").to_sym
list_contents = begin
JSON.parse exception_file.read
rescue JSON::ParserError
nil
end
next if list_contents.nil? || list_contents.count.zero?
@tap_style_exceptions[list_name] = list_contents
end
end
return false if @tap_style_exceptions.nil? || @tap_style_exceptions.count.zero?
return false unless @tap_style_exceptions.key? list
@tap_style_exceptions[list].include?(formula || @formula_name)
end
private
def formula_class?(node)
_, class_node, = *node
class_names = %w[
Formula
GithubGistFormula
ScriptFileFormula
AmazonWebServicesFormula
]
class_node && class_names.include?(string_content(class_node))
end
def file_path_allowed?
paths_to_exclude = [%r{/Library/Homebrew/compat/},
%r{/Library/Homebrew/test/}]
return true if @file_path.nil? # file_path is nil when source is directly passed to the cop, e.g. in specs
@file_path !~ Regexp.union(paths_to_exclude)
end
def on_system_methods
@on_system_methods ||= [:intel, :arm, :macos, :linux, :system, *MacOSVersions::SYMBOLS.keys].map do |m|
:"on_#{m}"
end
end
end
end
end