Class: RuboCop::Cop::FormulaAudit::Miscellaneous

Inherits:
RuboCop::Cop::FormulaCop show all
Defined in:
Library/Homebrew/rubocops/lines_cop.rb

Instance Attribute Summary

Attributes inherited from RuboCop::Cop::FormulaCop

#file_path

Instance Method Summary collapse

Methods inherited from RuboCop::Cop::FormulaCop

#audit_comments, #audit_urls, #block_size, #caveats_strings, #check_precedence, #class_name, #component_precedes?, #depends_on?, #depends_on_name_type?, #end_column, #expression_negated?, #find_all_blocks, #find_block, #find_blocks, #find_const, #find_every_method_call_by_name, #find_instance_call, #find_instance_method_call, #find_method_calls_by_name, #find_method_def, #find_method_with_args, #find_node_method_by_name, #find_strings, #format_component, #formula_tap, #get_checksum_node, #line_number, #line_start_column, #method_called?, #method_called_ever?, #method_called_in_block?, #method_name, #node_equals?, #offending_node, #on_class, #parameters, #parameters_passed?, #problem, #regex_match_group, #size, #source_buffer, #start_column, #string_content, #versioned_formula?

Instance Method Details

#audit_formula(_node, _class_node, _parent_class_node, body_node) ⇒ Object



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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'Library/Homebrew/rubocops/lines_cop.rb', line 144

def audit_formula(_node, _class_node, _parent_class_node, body_node)
  # FileUtils is included in Formula
  # encfs modifies a file with this name, so check for some leading characters
  find_instance_method_call(body_node, "FileUtils", nil) do |method_node|
    problem "Don't need 'FileUtils.' before #{method_node.method_name}"
  end

  # Check for long inreplace block vars
  find_all_blocks(body_node, :inreplace) do |node|
    block_arg = node.arguments.children.first
    next unless block_arg.source.size > 1
    problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"."
  end

  [:rebuild, :version_scheme].each do |method_name|
    find_method_with_args(body_node, method_name, 0) do
      problem "'#{method_name} 0' should be removed"
    end
  end

  [:mac?, :linux?].each do |method_name|
    next unless formula_tap == "homebrew-core"
    find_instance_method_call(body_node, "OS", method_name) do |check|
      problem "Don't use #{check.source}; Homebrew/core only supports macOS"
    end
  end

  find_instance_call(body_node, "ARGV") do |method_node|
    next if [:debug?, :verbose?, :value].index(method_node.method_name)
    problem "Use build instead of ARGV to check options"
  end

  find_instance_method_call(body_node, :man, :+) do |method|
    next unless match = regex_match_group(parameters(method).first, /^man[1-8]$/)
    problem "\"#{method.source}\" should be \"#{match[0]}\""
  end

  # Avoid hard-coding compilers
  find_every_method_call_by_name(body_node, :system).each do |method|
    param = parameters(method).first
    if match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|llvm-gcc|clang)\s?})
      problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
    elsif match = regex_match_group(param, %r{^(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?})
      problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
    end
  end

  find_instance_method_call(body_node, "ENV", :[]=) do |method|
    param = parameters(method)[1]
    if match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|llvm-gcc|clang)\s?})
      problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
    elsif match = regex_match_group(param, %r{^(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?})
      problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
    end
  end

  # Prefer formula path shortcuts in strings
  formula_path_strings(body_node, :share) do |p|
    next unless match = regex_match_group(p, %r{^(/(man))/?})
    problem "\"\#{share}#{match[1]}\" should be \"\#{#{match[2]}}\""
  end

  formula_path_strings(body_node, :prefix) do |p|
    if match = regex_match_group(p, %r{^(/share/(info|man))$})
      problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2]}}\""
    end
    if match = regex_match_group(p, %r{^((/share/man/)(man[1-8]))})
      problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[3]}}\""
    end
    if match = regex_match_group(p, %r{^(/(bin|include|libexec|lib|sbin|share|Frameworks))}i)
      problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2].downcase}}\""
    end
  end

  find_every_method_call_by_name(body_node, :depends_on).each do |method|
    key, value = destructure_hash(parameters(method).first)
    next if key.nil? || value.nil?
    next unless match = regex_match_group(value, /^(lua|perl|python|ruby)(\d*)/)
    problem "#{match[1]} modules should be vendored rather than use deprecated #{method.source}`"
  end

  find_every_method_call_by_name(body_node, :system).each do |method|
    next unless match = regex_match_group(parameters(method).first, /^(env|export)(\s+)?/)
    problem "Use ENV instead of invoking '#{match[1]}' to modify the environment"
  end

  find_every_method_call_by_name(body_node, :depends_on).each do |method|
    next if modifier?(method.parent)
    param = parameters(method).first
    dep, option = hash_dep(param)
    next if dep.nil? || option.nil?
    offending_node(param)
    problem "Dependency #{string_content(dep)} should not use option #{string_content(option)}"
  end

  find_instance_method_call(body_node, :version, :==) do |method|
    next unless parameters_passed?(method, "HEAD")
    problem "Use 'build.head?' instead of inspecting 'version'"
  end

  find_instance_method_call(body_node, "ARGV", :include?) do |method|
    param = parameters(method).first
    next unless match = regex_match_group(param, /^--(HEAD|devel)/)
    problem "Use \"if build.#{match[1].downcase}?\" instead"
  end

  find_const(body_node, "MACOS_VERSION") do
    problem "Use MacOS.version instead of MACOS_VERSION"
  end

  find_const(body_node, "MACOS_FULL_VERSION") do
    problem "Use MacOS.full_version instead of MACOS_FULL_VERSION"
  end

  conditional_dependencies(body_node) do |node, method, param, dep_node|
    dep = string_content(dep_node)
    if node.if?
      if (method == :include? && regex_match_group(param, /^with-#{dep}$/)) ||
         (method == :with? && regex_match_group(param, /^#{dep}$/))
        offending_node(dep_node.parent)
        problem "Replace #{node.source} with #{dep_node.parent.source} => :optional"
      end
    elsif node.unless?
      if (method == :include? && regex_match_group(param, /^without-#{dep}$/)) ||
         (method == :without? && regex_match_group(param, /^#{dep}$/))
        offending_node(dep_node.parent)
        problem "Replace #{node.source} with #{dep_node.parent.source} => :recommended"
      end
    end
  end

  find_method_with_args(body_node, :fails_with, :llvm) do
    problem "'fails_with :llvm' is now a no-op so should be removed"
  end

  find_method_with_args(body_node, :needs, :openmp) do
    problem "'needs :openmp' should be replaced with 'depends_on \"gcc\"'"
  end

  find_method_with_args(body_node, :system, /^(otool|install_name_tool|lipo)/) do
    next if @formula_name == "cctools"
    problem "Use ruby-macho instead of calling #{@offensive_node.source}"
  end

  find_every_method_call_by_name(body_node, :system).each do |method_node|
    # Skip Kibana: npm cache edge (see formula for more details)
    next if @formula_name =~ /^kibana(\@\d+(\.\d+)?)?$/i
    first_param, second_param = parameters(method_node)
    next if !node_equals?(first_param, "npm") ||
            !node_equals?(second_param, "install")
    offending_node(method_node)
    problem "Use Language::Node for npm install args" unless languageNodeModule?(method_node)
  end

  if find_method_def(body_node, :test)
    problem "Use new-style test definitions (test do)"
  end

  find_method_with_args(body_node, :skip_clean, :all) do
    problem "`skip_clean :all` is deprecated; brew no longer strips symbols. " \
            "Pass explicit paths to prevent Homebrew from removing empty folders."
  end

  if find_method_def(@processed_source.ast)
    problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level"
  end

  find_instance_method_call(body_node, :build, :universal?) do
    next if @formula_name == "wine"
    problem "macOS has been 64-bit only since 10.6 so build.universal? is deprecated."
  end

  find_instance_method_call(body_node, "ENV", :universal_binary) do
    next if @formula_name == "wine"
    problem "macOS has been 64-bit only since 10.6 so ENV.universal_binary is deprecated."
  end

  find_instance_method_call(body_node, "ENV", :x11) do
    problem 'Use "depends_on :x11" instead of "ENV.x11"'
  end

  find_every_method_call_by_name(body_node, :depends_on).each do |method|
    next unless method_called?(method, :new)
    problem "`depends_on` can take requirement classes instead of instances"
  end

  os = [:leopard?, :snow_leopard?, :lion?, :mountain_lion?]
  os.each do |version|
    find_instance_method_call(body_node, "MacOS", version) do |method|
      problem "\"#{method.source}\" is deprecated, use a comparison to MacOS.version instead"
    end
  end

  find_instance_method_call(body_node, "Dir", :[]) do |method|
    next unless parameters(method).size == 1
    path = parameters(method).first
    next unless path.str_type?
    next unless match = regex_match_group(path, /^[^\*{},]+$/)
    problem "Dir([\"#{string_content(path)}\"]) is unnecessary; just use \"#{match[0]}\""
  end

  fileutils_methods = Regexp.new(FileUtils.singleton_methods(false).map { |m| "(?-mix:^" + Regexp.escape(m) + "$)" }.join("|"))
  find_every_method_call_by_name(body_node, :system).each do |method|
    param = parameters(method).first
    next unless match = regex_match_group(param, fileutils_methods)
    problem "Use the `#{match}` Ruby method instead of `#{method.source}`"
  end
end

#modifier?(node) ⇒ Boolean

Returns:

  • (Boolean)


353
354
355
356
# File 'Library/Homebrew/rubocops/lines_cop.rb', line 353

def modifier?(node)
  return false unless node.if_type?
  node.modifier_form?
end