Skip to content

Commit

Permalink
rubocops/lines: audit std_npm_args usage
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent committed Aug 4, 2024
1 parent 9279693 commit 1771bf1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Library/Homebrew/rubocops/lines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,43 @@ def audit_formula(formula_nodes)
end
end

# This cop makes sure that formulae use `std_npm_args` instead of older
# `local_npm_install_args` and `std_npm_install_args`.
class StdNpmArgs < FormulaCop
extend AutoCorrector

sig { override.params(formula_nodes: FormulaNodes).void }
def audit_formula(formula_nodes)
return if (body_node = formula_nodes.body_node).nil?

find_method_with_args(body_node, :local_npm_install_args) do
problem "Use 'std_npm_args' instead of '#{@offensive_node.method_name}'." do |corrector|
corrector.replace(@offensive_node.source_range, "std_npm_args(prefix: false)")
end
end

find_method_with_args(body_node, :std_npm_install_args) do |method|
problem "Use 'std_npm_args' instead of '#{@offensive_node.method_name}'." do |corrector|
if (param = parameters(method).first.source) == "libexec"
corrector.replace(@offensive_node.source_range, "std_npm_args")
else
corrector.replace(@offensive_node.source_range, "std_npm_args(prefix: #{param})")
end
end
end

find_every_method_call_by_name(body_node, :system).each do |method|
first_param, second_param = parameters(method)
next if !node_equals?(first_param, "npm") ||
!node_equals?(second_param, "install") ||
method.source.match(/(std_npm_args|local_npm_install_args|std_npm_install_args)/)

offending_node(method)
problem "Use `std_npm_args` for npm install"
end
end
end

# This cop makes sure that formulae depend on `openssl` instead of `quictls`.
class QuicTLSCheck < FormulaCop
extend AutoCorrector
Expand Down
87 changes: 87 additions & 0 deletions Library/Homebrew/test/rubocops/text/std_npm_args_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require "rubocops/lines"

RSpec.describe RuboCop::Cop::FormulaAudit::StdNpmArgs do
subject(:cop) { described_class.new }

context "when auditing node formulae" do
it "reports an offense when `npm install` is called without std_npm_args arguments" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
system "npm", "install"
^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use `std_npm_args` for npm install
end
end
RUBY
end

it "reports and corrects an offense when using local_npm_install_args" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *Language::Node.local_npm_install_args, "--production"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use 'std_npm_args' instead of 'local_npm_install_args'.
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *std_npm_args(prefix: false), "--production"
end
end
RUBY
end

it "reports and corrects an offense when using std_npm_install_args with libexec" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *Language::Node.std_npm_install_args(libexec), "--production"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use 'std_npm_args' instead of 'std_npm_install_args'.
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *std_npm_args, "--production"
end
end
RUBY
end

it "reports and corrects an offense when using std_npm_install_args without libexec" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *Language::Node.std_npm_install_args(buildpath), "--production"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use 'std_npm_args' instead of 'std_npm_install_args'.
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *std_npm_args(prefix: buildpath), "--production"
end
end
RUBY
end

it "does not report an offense when using std_npm_args" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
def install
system "npm", "install", *std_npm_args
end
end
RUBY
end
end
end

0 comments on commit 1771bf1

Please sign in to comment.