Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delimiters' and related predicates for RegexpNode` #41

Merged
merged 4 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#20](https://github.com/rubocop-hq/rubocop-ast/pull/20): Add option predicates for `RegexpNode`. ([@owst][])
* [#11](https://github.com/rubocop-hq/rubocop-ast/issues/11): Add `argument_type?` method to make it easy to recognize argument nodes. ([@tejasbubane][])
* [#31](https://github.com/rubocop-hq/rubocop-ast/pull/31): Use `param === node` to match params, which allows Regexp, Proc, Set, etc. ([@marcandre][])
* [#41](https://github.com/rubocop-hq/rubocop-ast/pull/41): Add `delimiter?' predicate for `RegexpNode`. ([@owst][])

## 0.0.3 (2020-05-15)

Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/ast/node/regexp_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def content
children.select(&:str_type?).map(&:str_content).join
end

# @return [Bool] if char is one of the delimiters
def delimiter?(char)
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you can also add a delimiters method that returns the two of them and implement this one in terms of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea - I took a quick look for related predicates used by RuboCop, so added those too, let me know what you think

[loc.begin.source[-1], loc.end.source[0]].include?(char)
end

# @return [Bool] if regexp contains interpolation
def interpolation?
children.any?(&:begin_type?)
Expand Down
57 changes: 57 additions & 0 deletions spec/rubocop/ast/regexp_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,63 @@
end
end

describe '#delimiter?' do
context 'with /-delimiters' do
let(:source) { '/abc/' }

it { expect(regexp_node.delimiter?('/')).to eq(true) }

it { expect(regexp_node.delimiter?('{')).to eq(false) }
end

context 'with %r/-delimiters' do
let(:source) { '%r/abc/' }

it { expect(regexp_node.delimiter?('/')).to eq(true) }

it { expect(regexp_node.delimiter?('{')).to eq(false) }
it { expect(regexp_node.delimiter?('}')).to eq(false) }
it { expect(regexp_node.delimiter?('%')).to eq(false) }
it { expect(regexp_node.delimiter?('r')).to eq(false) }
it { expect(regexp_node.delimiter?('%r')).to eq(false) }
it { expect(regexp_node.delimiter?('%r/')).to eq(false) }
end

context 'with %r{-delimiters' do
let(:source) { '%r{abc}' }

it { expect(regexp_node.delimiter?('{')).to eq(true) }
it { expect(regexp_node.delimiter?('}')).to eq(true) }

it { expect(regexp_node.delimiter?('/')).to eq(false) }
it { expect(regexp_node.delimiter?('%')).to eq(false) }
it { expect(regexp_node.delimiter?('r')).to eq(false) }
it { expect(regexp_node.delimiter?('%r')).to eq(false) }
it { expect(regexp_node.delimiter?('%r/')).to eq(false) }
it { expect(regexp_node.delimiter?('%r{')).to eq(false) }
end

context 'with multi-line %r{-delimiters' do
let(:source) do
<<~SRC
%r{
abc
}x
SRC
end

it { expect(regexp_node.delimiter?('{')).to eq(true) }
it { expect(regexp_node.delimiter?('}')).to eq(true) }

it { expect(regexp_node.delimiter?('/')).to eq(false) }
it { expect(regexp_node.delimiter?('%')).to eq(false) }
it { expect(regexp_node.delimiter?('r')).to eq(false) }
it { expect(regexp_node.delimiter?('%r')).to eq(false) }
it { expect(regexp_node.delimiter?('%r/')).to eq(false) }
it { expect(regexp_node.delimiter?('%r{')).to eq(false) }
end
end

describe '#interpolation?' do
context 'with direct variable interpoation' do
let(:source) { '/\n\n#{foo}(abc)+/' }
Expand Down