rubocops/lines: audit std_npm_args usage

This commit is contained in:
Branch Vincent 2024-07-25 21:00:10 -07:00
parent 9279693a34
commit 1771bf1bbb
No known key found for this signature in database
2 changed files with 124 additions and 0 deletions

View File

@ -214,6 +214,43 @@ module RuboCop
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

View File

@ -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