Skip to content

Commit

Permalink
[Fix #3958] Bug of Style/SpaceInsideHashLiteralBraces (#3976)
Browse files Browse the repository at this point in the history
When checking the braces, it uses the type instead of text to avoid
confusion a brace in a string. This avoid a confusion when token text is
a brace, but the token type is a string.
  • Loading branch information
Andrea Nodari authored and bbatsov committed Feb 6, 2017
1 parent 713e6b9 commit b8a7ff6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@
* [#3959](https://github.com/bbatsov/rubocop/issues/3959): Don't wrap "percent arrays" with extra brackets when autocorrecting `Style/MutableConstant`. ([@mikegee][])
* [#3978](https://github.com/bbatsov/rubocop/pull/3978): Fix false positive in `Performance/RegexpMatch` with `English` module. ([@pocke][])
* [#3242](https://github.com/bbatsov/rubocop/issues/3242): Ignore `Errno::ENOENT` during cache cleanup from `File.mtime` too. ([@mikegee][])
* [#3958](https://github.com/bbatsov/rubocop/issues/3958): `Style/SpaceInsideHashLiteralBraces` doesn't add and offence when checking an hash where a value is a left brace string (e.g. { k: '{' }). ([@nodo][])
* [#4006](https://github.com/bbatsov/rubocop/issues/4006): Prevent `Style/WhileUntilModifier` from breaking on a multiline modifier. ([@drenmi][])

## 0.47.1 (2017-01-18)
Expand Down Expand Up @@ -2630,5 +2631,6 @@
[@backus]: https://github.com/backus
[@pat]: https://github.com/pat
[@sinsoku]: https://github.com/sinsoku
[@nodo]: https://github.com/nodo
[@onk]: https://github.com/onk
[@dabroz]: https://github.com/dabroz
6 changes: 3 additions & 3 deletions lib/rubocop/cop/style/space_inside_hash_literal_braces.rb
Expand Up @@ -48,7 +48,7 @@ def check(t1, t2)
return if t1.pos.line < t2.pos.line
return if t2.type == :tCOMMENT # Also indicates there's a line break.

is_empty_braces = t1.text == '{' && t2.text == '}'
is_empty_braces = left_brace?(t1) && right_brace?(t2)
expect_space = expect_space?(t1, t2)

if offense?(t1, t2, expect_space)
Expand All @@ -59,8 +59,8 @@ def check(t1, t2)
end

def expect_space?(t1, t2)
is_same_braces = t1.text == t2.text
is_empty_braces = t1.text == '{' && t2.text == '}'
is_same_braces = t1.type == t2.type
is_empty_braces = left_brace?(t1) && right_brace?(t2)

if is_same_braces && style == :compact
false
Expand Down
Expand Up @@ -214,4 +214,12 @@
expect(cop.offenses).to be_empty
end
end

context 'on { key: "{" }' do
# regression test; see GH issue 3958
it 'does not register an offense' do
inspect_source(cop, '{ key: "{" }')
expect(cop.offenses).to be_empty
end
end
end

0 comments on commit b8a7ff6

Please sign in to comment.