Skip to content

Commit

Permalink
Merge pull request #7661 from Tietew/ast_regexp_multiline_fix
Browse files Browse the repository at this point in the history
[Fix rubocop/rubocop-performance#95] Fix to return correct info from multi-line regexp
  • Loading branch information
koic committed Jan 24, 2020
2 parents d7a6d1b + 479e733 commit bd18c76
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#7639](https://github.com/rubocop-hq/rubocop/pull/7639): Fix logical operator edge case in `omit_parentheses` style of `Style/MethodCallWithArgsParentheses`. ([@gsamokovarov][])
* [#7661](https://github.com/rubocop-hq/rubocop/pull/7661): Fix to return correct info from multi-line regexp. ([@Tietew][])

### Changes

Expand Down Expand Up @@ -4332,3 +4333,4 @@
[@pirj]: https://github.com/pirj
[@pawptart]: https://github.com/pawptart
[@gfyoung]: https://github.com/gfyoung
[@Tietew]: https://github.com/Tietew
6 changes: 2 additions & 4 deletions lib/rubocop/ast/node/regexp_node.rb
Expand Up @@ -21,14 +21,12 @@ def to_regexp

# @return [RuboCop::AST::Node] a regopt node
def regopt
first, second = *self
first.regopt_type? ? first : second
children.last
end

# @return [String] a string of regexp content
def content
str = children.first
str.str_content || ''
children.select(&:str_type?).map(&:str_content).join
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/ast/regexp_node_spec.rb
Expand Up @@ -23,6 +23,12 @@
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end

context 'with a multi-line regexp without option' do
let(:source) { "/\n.+\n/" }

it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end

context 'with an empty regexp with option' do
let(:source) { '//ix' }

Expand All @@ -34,6 +40,12 @@

it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end

context 'with a multi-line regexp with option' do
let(:source) { "/\n.+\n/ix" }

it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
# rubocop:enable Security/Eval
end

Expand All @@ -54,6 +66,13 @@
it { expect(regopt.children.empty?).to be(true) }
end

context 'with a multi-line regexp without option' do
let(:source) { "/\n.+\n/" }

it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children.empty?).to be(true) }
end

context 'with an empty regexp with option' do
let(:source) { '//ix' }

Expand All @@ -67,6 +86,13 @@
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children).to eq(%i[i m x]) }
end

context 'with a multi-line regexp with option' do
let(:source) { "/\n.+\n/imx" }

it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children).to eq(%i[i m x]) }
end
end

describe '#content' do
Expand All @@ -84,6 +110,12 @@
it { expect(content).to eq('.+') }
end

context 'with a multi-line regexp without option' do
let(:source) { "/\n.+\n/" }

it { expect(content).to eq("\n.+\n") }
end

context 'with an empty regexp with option' do
let(:source) { '//ix' }

Expand All @@ -95,5 +127,11 @@

it { expect(content).to eq('.+') }
end

context 'with a multi-line regexp with option' do
let(:source) { "/\n.+\n/imx" }

it { expect(content).to eq("\n.+\n") }
end
end
end

0 comments on commit bd18c76

Please sign in to comment.