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 RegexpNode regopt predicates #20

Merged
merged 3 commits into from Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### New features

* [#4](https://github.com/rubocop-hq/rubocop-ast/issues/4): Add `interpolation?` for `RegexpNode`. ([@tejasbubane][])
* [#20](https://github.com/rubocop-hq/rubocop-ast/pull/20): Add option predicates for `RegexpNode`. ([@owst][])

## 0.0.3 (2020-05-15)

Expand All @@ -28,3 +29,4 @@
* Gem extracted from RuboCop. ([@marcandre][])

[@marcandre]: https://github.com/marcandre
[@owst]: https://github.com/owst
31 changes: 31 additions & 0 deletions lib/rubocop/ast/node/regexp_node.rb
Expand Up @@ -36,6 +36,37 @@ def content
def interpolation?
children.any?(&:begin_type?)
end

# @return [Bool] if regexp uses the multiline regopt
def multiline?
regopt_include?(:m)
end

# @return [Bool] if regexp uses the extended regopt
def extended?
regopt_include?(:x)
end

# @return [Bool] if regexp uses the ignore-case regopt
def ignore_case?
regopt_include?(:i)
end

# @return [Bool] if regexp uses the single-interpolation regopt
def single_interpolation?
regopt_include?(:o)
end

# @return [Bool] if regexp uses the no-encoding regopt
def no_encoding?
regopt_include?(:n)
end

private

def regopt_include?(option)
regopt.children.include?(option)
end
end
end
end
130 changes: 130 additions & 0 deletions spec/rubocop/ast/regexp_node_spec.rb
Expand Up @@ -160,4 +160,134 @@
it { expect(regexp_node.interpolation?).to eq(false) }
end
end

describe '#multiline?' do
context 'with no options' do
let(:source) { '/x/' }

it { expect(regexp_node.multiline?).to be(false) }
end

context 'with other options' do
let(:source) { '/x/ix' }

it { expect(regexp_node.multiline?).to be(false) }
end

context 'with only m option' do
let(:source) { '/x/m' }

it { expect(regexp_node.multiline?).to be(true) }
end

context 'with m and other options' do
let(:source) { '/x/imx' }

it { expect(regexp_node.multiline?).to be(true) }
end
end

describe '#extended?' do
context 'with no options' do
let(:source) { '/x/' }

it { expect(regexp_node.extended?).to be(false) }
end

context 'with other options' do
let(:source) { '/x/im' }

it { expect(regexp_node.extended?).to be(false) }
end

context 'with only x option' do
let(:source) { '/x/x' }

it { expect(regexp_node.extended?).to be(true) }
end

context 'with x and other options' do
let(:source) { '/x/ixm' }

it { expect(regexp_node.extended?).to be(true) }
end
end

describe '#ignore_case?' do
context 'with no options' do
let(:source) { '/x/' }

it { expect(regexp_node.ignore_case?).to be(false) }
end

context 'with other options' do
let(:source) { '/x/xm' }

it { expect(regexp_node.ignore_case?).to be(false) }
end

context 'with only i option' do
let(:source) { '/x/i' }

it { expect(regexp_node.ignore_case?).to be(true) }
end

context 'with i and other options' do
let(:source) { '/x/xim' }

it { expect(regexp_node.ignore_case?).to be(true) }
end
end

describe '#no_encoding?' do
context 'with no options' do
let(:source) { '/x/' }

it { expect(regexp_node.no_encoding?).to be(false) }
end

context 'with other options' do
let(:source) { '/x/xm' }

it { expect(regexp_node.no_encoding?).to be(false) }
end

context 'with only n option' do
let(:source) { '/x/n' }

it { expect(regexp_node.no_encoding?).to be(true) }
end

context 'with n and other options' do
let(:source) { '/x/xnm' }

it { expect(regexp_node.no_encoding?).to be(true) }
end
end

describe '#single_interpolation?' do
context 'with no options' do
let(:source) { '/x/' }

it { expect(regexp_node.single_interpolation?).to be(false) }
end

context 'with other options' do
let(:source) { '/x/xm' }

it { expect(regexp_node.single_interpolation?).to be(false) }
end

context 'with only o option' do
let(:source) { '/x/o' }

it { expect(regexp_node.single_interpolation?).to be(true) }
end

context 'with o and other options' do
let(:source) { '/x/xom' }

it { expect(regexp_node.single_interpolation?).to be(true) }
end
end
end