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

[Fix rubocop-hq/rubocop-performance#95] Fix to return correct info from multi-line regexp #7661

Merged
merged 1 commit into from Jan 24, 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 @@
### 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 @@ -4330,3 +4331,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