-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ActiveSupport test name extraction into a helper
- Loading branch information
Showing
3 changed files
with
51 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
lib/ruby_lsp/ruby_lsp_rails/support/active_support_test_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Rails | ||
module ActiveSupportTestHelper | ||
extend T::Sig | ||
|
||
sig { params(node: SyntaxTree::Command).returns(T.nilable(String)) } | ||
def active_support_test_name(node) | ||
message_value = node.message.value | ||
return unless message_value == "test" && node.arguments.parts.any? | ||
|
||
first_argument = node.arguments.parts.first | ||
|
||
parts = case first_argument | ||
when SyntaxTree::StringConcat | ||
# We only support two lines of concatenation on test names | ||
if first_argument.left.is_a?(SyntaxTree::StringLiteral) && | ||
first_argument.right.is_a?(SyntaxTree::StringLiteral) | ||
[*first_argument.left.parts, *first_argument.right.parts] | ||
end | ||
when SyntaxTree::StringLiteral | ||
first_argument.parts | ||
end | ||
|
||
# The test name may be a blank string while the code is being typed | ||
return if parts.nil? || parts.empty? | ||
|
||
# We can't handle interpolation yet | ||
return unless parts.all? { |part| part.is_a?(SyntaxTree::TStringContent) } | ||
|
||
test_name = parts.map(&:value).join | ||
test_name unless test_name.empty? | ||
end | ||
end | ||
end | ||
end |