Skip to content

Commit

Permalink
Add RegexpNode regopt predicates (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
owst authored Jun 5, 2020
1 parent 7f23111 commit e98d015
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit e98d015

Please sign in to comment.