Skip to content

Commit

Permalink
Add character_literal? to StrNode
Browse files Browse the repository at this point in the history
This PR adds `character_literal?` to `StrNode`.

`...begin.is?('?')` used in rubocop/rubocop#11095
and `Style/CharacterLiteral` can be rewritten by `node.character_literal?`.
  • Loading branch information
koic committed Oct 21, 2022
1 parent c532417 commit 65cb09b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_character_literal_to_str_node.md
@@ -0,0 +1 @@
* [#242](https://github.com/rubocop/rubocop-ast/pull/242): Add `character_literal?` to `StrNode`. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/ast/node/str_node.rb
Expand Up @@ -8,6 +8,10 @@ module AST
class StrNode < Node
include BasicLiteralNode

def character_literal?
loc.respond_to?(:begin) && loc.begin && loc.begin.is?('?')
end

def heredoc?
loc.is_a?(Parser::Source::Map::Heredoc)
end
Expand Down
27 changes: 27 additions & 0 deletions spec/rubocop/ast/str_node_spec.rb
Expand Up @@ -30,6 +30,33 @@
end
end

describe '#character_literal?' do
context 'with a character literal' do
let(:source) { '?\n' }

it { is_expected.to be_character_literal }
end

context 'with a normal string literal' do
let(:source) { '"\n"' }

it { is_expected.not_to be_character_literal }
end

context 'with a heredoc' do
let(:source) do
<<~RUBY
<<-CODE
foo
bar
CODE
RUBY
end

it { is_expected.not_to be_character_literal }
end
end

describe '#heredoc?' do
context 'with a normal string' do
let(:source) { "'foo'" }
Expand Down

0 comments on commit 65cb09b

Please sign in to comment.