From 1771bf1bbb4e52bb4e24c779adfae004402a4989 Mon Sep 17 00:00:00 2001 From: Branch Vincent Date: Thu, 25 Jul 2024 21:00:10 -0700 Subject: [PATCH] rubocops/lines: audit `std_npm_args` usage --- Library/Homebrew/rubocops/lines.rb | 37 ++++++++ .../test/rubocops/text/std_npm_args_spec.rb | 87 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 Library/Homebrew/test/rubocops/text/std_npm_args_spec.rb diff --git a/Library/Homebrew/rubocops/lines.rb b/Library/Homebrew/rubocops/lines.rb index 7932991e3a..2c1ef8103c 100644 --- a/Library/Homebrew/rubocops/lines.rb +++ b/Library/Homebrew/rubocops/lines.rb @@ -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 diff --git a/Library/Homebrew/test/rubocops/text/std_npm_args_spec.rb b/Library/Homebrew/test/rubocops/text/std_npm_args_spec.rb new file mode 100644 index 0000000000..c0bb9e97ed --- /dev/null +++ b/Library/Homebrew/test/rubocops/text/std_npm_args_spec.rb @@ -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