From f2fe1b59a19c4a80e6c0ff14d4f81720e6d1c1b9 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Thu, 2 Jun 2022 21:53:41 +0900 Subject: [PATCH 1/4] move check ENV.runtime_cpu_detection to utils/ast --- Library/Homebrew/formula_cellar_checks.rb | 5 +++-- Library/Homebrew/utils/ast.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 46d9ac63ef..f1e92e30dd 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -291,8 +291,9 @@ module FormulaCellarChecks dot_brew_formula = formula.prefix/".brew/#{formula.name}.rb" return unless dot_brew_formula.exist? - # TODO: add methods to `utils/ast` to allow checking for method use - return unless dot_brew_formula.read.include? "ENV.runtime_cpu_detection" + + require "utils/ast" + return unless Utils::AST::FormulaAST.new(dot_brew_formula.read).include_runtime_cpu_detection? # macOS `objdump` is a bit slow, so we prioritise llvm's `llvm-objdump` (~5.7x faster) # or binutils' `objdump` (~1.8x faster) if they are installed. diff --git a/Library/Homebrew/utils/ast.rb b/Library/Homebrew/utils/ast.rb index 5531fa376e..d2c634bf25 100644 --- a/Library/Homebrew/utils/ast.rb +++ b/Library/Homebrew/utils/ast.rb @@ -190,6 +190,19 @@ module Utils tree_rewriter.insert_after(preceding_expr, "\n#{stanza_text(name, value, indent: 2)}") end + sig { returns(T::Boolean) } + def include_runtime_cpu_detection? + install_node = children.find do |child| + (child.is_a? RuboCop::AST::DefNode) && child.method_name == :install + end + + return false if install_node.blank? + + install_node.each_node.any? do |node| + node&.receiver&.const_name == "ENV" && node&.method_name == :runtime_cpu_detection + end + end + private sig { returns(String) } From 8b14738a88da598558346ec7de557401622f4ef5 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Mon, 6 Jun 2022 20:59:31 +0900 Subject: [PATCH 2/4] remove safe navigation operator --- Library/Homebrew/utils/ast.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/ast.rb b/Library/Homebrew/utils/ast.rb index d2c634bf25..679c08f7a6 100644 --- a/Library/Homebrew/utils/ast.rb +++ b/Library/Homebrew/utils/ast.rb @@ -199,7 +199,7 @@ module Utils return false if install_node.blank? install_node.each_node.any? do |node| - node&.receiver&.const_name == "ENV" && node&.method_name == :runtime_cpu_detection + node.send_type? && node.receiver.const_name == "ENV" && node.method_name == :runtime_cpu_detection end end From 7e31574c3f23fdb2c27ab5d7d552e49792d0bddb Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:03:39 +0900 Subject: [PATCH 3/4] avoid nil error --- Library/Homebrew/utils/ast.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/ast.rb b/Library/Homebrew/utils/ast.rb index 679c08f7a6..8132023aef 100644 --- a/Library/Homebrew/utils/ast.rb +++ b/Library/Homebrew/utils/ast.rb @@ -199,7 +199,7 @@ module Utils return false if install_node.blank? install_node.each_node.any? do |node| - node.send_type? && node.receiver.const_name == "ENV" && node.method_name == :runtime_cpu_detection + node.send_type? && node.receiver&.const_name == "ENV" && node.method_name == :runtime_cpu_detection end end From 7a95219d2b60253103f04424e743501e35fe18cb Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Fri, 10 Jun 2022 22:24:20 +0900 Subject: [PATCH 4/4] remove new method and reset without TODO --- Library/Homebrew/formula_cellar_checks.rb | 3 +-- Library/Homebrew/utils/ast.rb | 13 ------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index f1e92e30dd..3a663358fa 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -292,8 +292,7 @@ module FormulaCellarChecks dot_brew_formula = formula.prefix/".brew/#{formula.name}.rb" return unless dot_brew_formula.exist? - require "utils/ast" - return unless Utils::AST::FormulaAST.new(dot_brew_formula.read).include_runtime_cpu_detection? + return unless dot_brew_formula.read.include? "ENV.runtime_cpu_detection" # macOS `objdump` is a bit slow, so we prioritise llvm's `llvm-objdump` (~5.7x faster) # or binutils' `objdump` (~1.8x faster) if they are installed. diff --git a/Library/Homebrew/utils/ast.rb b/Library/Homebrew/utils/ast.rb index 8132023aef..5531fa376e 100644 --- a/Library/Homebrew/utils/ast.rb +++ b/Library/Homebrew/utils/ast.rb @@ -190,19 +190,6 @@ module Utils tree_rewriter.insert_after(preceding_expr, "\n#{stanza_text(name, value, indent: 2)}") end - sig { returns(T::Boolean) } - def include_runtime_cpu_detection? - install_node = children.find do |child| - (child.is_a? RuboCop::AST::DefNode) && child.method_name == :install - end - - return false if install_node.blank? - - install_node.each_node.any? do |node| - node.send_type? && node.receiver&.const_name == "ENV" && node.method_name == :runtime_cpu_detection - end - end - private sig { returns(String) }