Skip to content

Commit

Permalink
Deprecate ConstantNameInspector#constant_name_from_node without relat…
Browse files Browse the repository at this point in the history
…ive_file:
  • Loading branch information
gmcgibbon committed Nov 29, 2023
1 parent 2bc8184 commit 0923c43
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/packwerk/reference_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ def reference_from_node(node, ancestors:, relative_file:)
constant_name = T.let(nil, T.nilable(String))

@constant_name_inspectors.each do |inspector|
constant_name = inspector.constant_name_from_node(
node,
constant_name = inspect_node(
inspector,
node: node,
ancestors: ancestors,
relative_file: relative_file
)
Expand All @@ -101,6 +102,29 @@ def reference_from_node(node, ancestors:, relative_file:)

private

sig do
params(
inspector: ConstantNameInspector,
node: Parser::AST::Node,
ancestors: T::Array[Parser::AST::Node],
relative_file: String
).returns(T.nilable(String))
end
def inspect_node(inspector, node:, ancestors:, relative_file:)
inspector.constant_name_from_node(node, ancestors: ancestors, relative_file: relative_file)
rescue ArgumentError => error
if error.message == "unknown keyword: :relative_file"
T.unsafe(inspector).constant_name_from_node(node, ancestors: ancestors).tap do
warn(<<~MSG.squish)
#{T.cast(inspector, Object).class}#reference_from_node without a relative_file: keyword
argument is deprecated and will be required in Packwerk 3.1.1.
MSG
end
else
raise
end
end

sig do
params(
constant_name: String,
Expand Down
23 changes: 23 additions & 0 deletions test/unit/packwerk/reference_extractor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ def teardown
assert_equal "::Order", reference.constant.name
end

test "constant name inspector without file name kwarg is deprecated but works" do
_, error_output = capture_io do
process(
":orders",
"components/timeline/app/models/entry.rb",
[DeprecatedInspector.new],
)
end

assert_equal(<<~MSG.squish, error_output.chomp)
Packwerk::ReferenceExtractorTest::DeprecatedInspector#reference_from_node without
a relative_file: keyword argument is deprecated and will be required in Packwerk 3.1.1.
MSG
end

private

class DummyAssociationInspector
Expand All @@ -235,6 +250,14 @@ def constant_name_from_node(node, ancestors:, relative_file:)
end
end

class DeprecatedInspector
T.unsafe(self).include(ConstantNameInspector)

def constant_name_from_node(node, ancestors:)
"Something"
end
end

DEFAULT_INSPECTORS = [ConstNodeInspector.new, DummyAssociationInspector.new]

def process(code, file_path, constant_name_inspectors = DEFAULT_INSPECTORS)
Expand Down

0 comments on commit 0923c43

Please sign in to comment.