Skip to content

Commit 1771bf1

Browse files
committed
rubocops/lines: audit std_npm_args usage
1 parent 9279693 commit 1771bf1

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

Library/Homebrew/rubocops/lines.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,43 @@ def audit_formula(formula_nodes)
214214
end
215215
end
216216

217+
# This cop makes sure that formulae use `std_npm_args` instead of older
218+
# `local_npm_install_args` and `std_npm_install_args`.
219+
class StdNpmArgs < FormulaCop
220+
extend AutoCorrector
221+
222+
sig { override.params(formula_nodes: FormulaNodes).void }
223+
def audit_formula(formula_nodes)
224+
return if (body_node = formula_nodes.body_node).nil?
225+
226+
find_method_with_args(body_node, :local_npm_install_args) do
227+
problem "Use 'std_npm_args' instead of '#{@offensive_node.method_name}'." do |corrector|
228+
corrector.replace(@offensive_node.source_range, "std_npm_args(prefix: false)")
229+
end
230+
end
231+
232+
find_method_with_args(body_node, :std_npm_install_args) do |method|
233+
problem "Use 'std_npm_args' instead of '#{@offensive_node.method_name}'." do |corrector|
234+
if (param = parameters(method).first.source) == "libexec"
235+
corrector.replace(@offensive_node.source_range, "std_npm_args")
236+
else
237+
corrector.replace(@offensive_node.source_range, "std_npm_args(prefix: #{param})")
238+
end
239+
end
240+
end
241+
242+
find_every_method_call_by_name(body_node, :system).each do |method|
243+
first_param, second_param = parameters(method)
244+
next if !node_equals?(first_param, "npm") ||
245+
!node_equals?(second_param, "install") ||
246+
method.source.match(/(std_npm_args|local_npm_install_args|std_npm_install_args)/)
247+
248+
offending_node(method)
249+
problem "Use `std_npm_args` for npm install"
250+
end
251+
end
252+
end
253+
217254
# This cop makes sure that formulae depend on `openssl` instead of `quictls`.
218255
class QuicTLSCheck < FormulaCop
219256
extend AutoCorrector
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocops/lines"
4+
5+
RSpec.describe RuboCop::Cop::FormulaAudit::StdNpmArgs do
6+
subject(:cop) { described_class.new }
7+
8+
context "when auditing node formulae" do
9+
it "reports an offense when `npm install` is called without std_npm_args arguments" do
10+
expect_offense(<<~RUBY)
11+
class Foo < Formula
12+
def install
13+
system "npm", "install"
14+
^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use `std_npm_args` for npm install
15+
end
16+
end
17+
RUBY
18+
end
19+
20+
it "reports and corrects an offense when using local_npm_install_args" do
21+
expect_offense(<<~RUBY)
22+
class Foo < Formula
23+
def install
24+
system "npm", "install", *Language::Node.local_npm_install_args, "--production"
25+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use 'std_npm_args' instead of 'local_npm_install_args'.
26+
end
27+
end
28+
RUBY
29+
30+
expect_correction(<<~RUBY)
31+
class Foo < Formula
32+
def install
33+
system "npm", "install", *std_npm_args(prefix: false), "--production"
34+
end
35+
end
36+
RUBY
37+
end
38+
39+
it "reports and corrects an offense when using std_npm_install_args with libexec" do
40+
expect_offense(<<~RUBY)
41+
class Foo < Formula
42+
def install
43+
system "npm", "install", *Language::Node.std_npm_install_args(libexec), "--production"
44+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use 'std_npm_args' instead of 'std_npm_install_args'.
45+
end
46+
end
47+
RUBY
48+
49+
expect_correction(<<~RUBY)
50+
class Foo < Formula
51+
def install
52+
system "npm", "install", *std_npm_args, "--production"
53+
end
54+
end
55+
RUBY
56+
end
57+
58+
it "reports and corrects an offense when using std_npm_install_args without libexec" do
59+
expect_offense(<<~RUBY)
60+
class Foo < Formula
61+
def install
62+
system "npm", "install", *Language::Node.std_npm_install_args(buildpath), "--production"
63+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/StdNpmArgs: Use 'std_npm_args' instead of 'std_npm_install_args'.
64+
end
65+
end
66+
RUBY
67+
68+
expect_correction(<<~RUBY)
69+
class Foo < Formula
70+
def install
71+
system "npm", "install", *std_npm_args(prefix: buildpath), "--production"
72+
end
73+
end
74+
RUBY
75+
end
76+
77+
it "does not report an offense when using std_npm_args" do
78+
expect_no_offenses(<<~RUBY)
79+
class Foo < Formula
80+
def install
81+
system "npm", "install", *std_npm_args
82+
end
83+
end
84+
RUBY
85+
end
86+
end
87+
end

0 commit comments

Comments
 (0)