Skip to content

[GH-6357] Add missing Rubyzen APIs when parsing tests#19

Merged
Stelios Frantzeskakis (steliosfran) merged 2 commits into
mainfrom
6357-add-missing-rubyzen-apis-when-parsing-tests
Apr 3, 2026
Merged

[GH-6357] Add missing Rubyzen APIs when parsing tests#19
Stelios Frantzeskakis (steliosfran) merged 2 commits into
mainfrom
6357-add-missing-rubyzen-apis-when-parsing-tests

Conversation

@steliosfran

@steliosfran Stelios Frantzeskakis (steliosfran) commented Mar 12, 2026

Copy link
Copy Markdown
Collaborator

New Rubyzen APIs to improve writing lint rules for our tests:

  • Added missing call_sites in FileDeclaration
  • Added blocks in FileDeclaration
  • Added CallSiteProvider in BlockDeclaration
  • Added BlocksCollection
  • Added keyword_arg_value_pairs in CallSiteDeclaration

Example lint rule we had before, parsing nodes manually:

let(:all_call_sites) do
  test_files.without_paths(*baseline).flat_map do |file|
    file.ast.each_node(:send).map do |node|
      Rubyzen::Declarations::CallSiteDeclaration.new(node, file)
    end
  end
end

let(:profile_factory_call_sites) do
  all_call_sites.filter do |cs|
    cs.method_name.to_s == "create" &&
      cs.source_code.include?(':profile')
  end
end

let(:violations) do
  profile_factory_call_sites.filter do |cs|
    (cs.source_code.match?(/\blatitude:\s*['"]?[-+]?\d+(\.\d+)?['"]?/) ||
     cs.source_code.match?(/\blatitude:\s*-\d+(\.\d+)?/)) &&
      (cs.source_code.match?(/\blongitude:\s*['"]?[-+]?\d+(\.\d+)?['"]?/) ||
       cs.source_code.match?(/\blongitude:\s*-\d+(\.\d+)?/))
  end
end

it 'uses location traits instead of hardcoded coordinates' do
  expect(violations).to be_empty
end

This can be now written as:

let(:profile_factory_calls) do
  test_files.without_paths(*baseline)
    .call_sites
    .with_name('create')
    .with_symbol(:profile)
end

it 'uses location traits instead of hardcoded coordinates' do
  expect(profile_factory_calls).to be_false { |cs|
    cs.keyword_arg_value_pairs[:latitude].is_a?(Numeric) &&
      cs.keyword_arg_value_pairs[:longitude].is_a?(Numeric)
  }
end

In addition, we can now parse directly any block of code in our tests:

test_files.blocks.with_method_name('describe') # All describe blocks
test_files.blocks.with_method_name('context') # All context blocks
test_files.blocks.with_method_name('let') # All let blocks

@bkoell Bastian (bkoell) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with comments.

end

def method_name
node.method_name.to_s

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking semantic change? Do we need to update any HR linters?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it could be if any lint rule was using it, but I just ran them all and they all pass.

Basically the parent.name here was wrong.

end.uniq
end

private

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional to also make symbols and strings methods public?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bastian (@bkoell) yes they were added to expand the API so you can now use them directly in the lint rules. They are also used in the Collections.

Comment thread lib/rubyzen/matchers/be_false_matcher.rb
@steliosfran Stelios Frantzeskakis (steliosfran) merged commit f9300db into main Apr 3, 2026
1 check failed
@steliosfran Stelios Frantzeskakis (steliosfran) deleted the 6357-add-missing-rubyzen-apis-when-parsing-tests branch April 3, 2026 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants