Skip to content

Commit

Permalink
Add Node#string_literal? to match str and dstr and `Node#string…
Browse files Browse the repository at this point in the history
…_type?` to match `str`, `dstr` and `xstr` types.
  • Loading branch information
dvandersluis committed Nov 4, 2024
1 parent e466119 commit fbb6525
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/new_add_nodestring_literal_to_match_str_and.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#328](https://github.com/rubocop/rubocop-ast/pull/328): Add `Node#string_literal?` to match `str` and `dstr` and `Node#string_type?` to match `str`, `dstr` and `xstr` types. ([@dvandersluis][])
10 changes: 10 additions & 0 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
# @api private
BASIC_LITERALS = (LITERALS - COMPOSITE_LITERALS).freeze
# @api private
STRING_LITERALS = %i[str dstr].freeze
# @api private
MUTABLE_LITERALS = %i[str dstr xstr array hash
regexp irange erange].to_set.freeze
# @api private
Expand Down Expand Up @@ -385,6 +387,10 @@ def basic_literal?
BASIC_LITERALS.include?(type)
end

def string_literal?
STRING_LITERALS.include?(type)
end

def truthy_literal?
TRUTHY_LITERALS.include?(type)
end
Expand Down Expand Up @@ -483,6 +489,10 @@ def boolean_type?
true_type? || false_type?
end

def string_type?
str_type? || dstr_type? || xstr_type?
end

def numeric_type?
int_type? || float_type? || rational_type? || complex_type?
end
Expand Down
70 changes: 69 additions & 1 deletion spec/rubocop/ast/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,74 @@ class << expr
end
end

describe '#string_literal?' do
context 'when string literal' do
let(:src) { %q{'foo'} }

it 'is true' do
expect(node).to be_string_literal
end
end

context 'when interpolated string' do
let(:src) { %q{"foo #{bar}"} }

it 'is true' do
expect(node).to be_string_literal
end
end

context 'when `xstr` node' do
let(:src) { '`ls`' }

it 'is true' do
expect(node).not_to be_string_literal
end
end

context 'when numeric literal' do
let(:src) { '42' }

it 'is false' do
expect(node).not_to be_string_literal
end
end
end

describe '#string_type?' do
context 'when string literal' do
let(:src) { %q{'foo'} }

it 'is true' do
expect(node).to be_string_type
end
end

context 'when interpolated string' do
let(:src) { %q{"foo #{bar}"} }

it 'is true' do
expect(node).to be_string_type
end
end

context 'when `xstr` node' do
let(:src) { '`ls`' }

it 'is true' do
expect(node).to be_string_type
end
end

context 'when numeric literal' do
let(:src) { '42' }

it 'is false' do
expect(node).not_to be_string_type
end
end
end

describe '#numeric_type?' do
context 'when integer literal' do
let(:src) { '42' }
Expand Down Expand Up @@ -899,7 +967,7 @@ class << expr
context 'when string literal' do
let(:src) { '"42"' }

it 'is true' do
it 'is false' do
expect(node).not_to be_numeric_type
end
end
Expand Down

0 comments on commit fbb6525

Please sign in to comment.