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

PercentLiteralDelimiters incorrectly automatically modifies escaped percent literal delimiter #4363

Closed
AustinBGibbons opened this issue May 10, 2017 · 0 comments
Labels

Comments

@AustinBGibbons
Copy link

AustinBGibbons commented May 10, 2017

Rubocop is correcting

x = %w(\()

to

x = %w[\(]

but these are not the same thing

[1] pry(main)> x = %w( \( )
=> ["("]
[2] pry(main)> x = %w[\(]
=> ["\\("]

Expected behavior

Rubocop should not create the change, put issue a warning if the existing or target delimiters are contained within the array

Actual behavior

file.rb:16:9: C: [Corrected] %w-literals should be delimited by [ and ].
    x = %w(\()
        ^^^^^^

Steps to reproduce the problem

Add

x = %w(\()

to a file, and run rubocop on that file.

Additionally, this is in our .rubocop.yml file

Style/PercentLiteralDelimiters:
  PreferredDelimiters:
    # Using `[]` for string arrays instead of `()`, since normal arrays are
    # indicated with `[]` not `()`.
    '%w': '[]'
    '%W': '[]'
    '%i': '[]'

RuboCop version

$ rubocop -V
0.48.1 (using Parser 2.4.0.0, running on ruby 2.2.2 x86_64-darwin14)
@Drenmi Drenmi added the bug label Jul 20, 2017
koic added a commit to koic/rubocop that referenced this issue Nov 11, 2017
…ters` cop

Fixes rubocop#4363.

Percent notation targeted by Issue # 4363 are `%w` and `%i`.
`%W` and `%I` are not targets.

## %w / %W

The meaning changes.

```ruby
> %w(\() # => ["("]
> %w[\(] # => ["\\("]
```

The meaning does not change.

```ruby
> %W(\() # => ["("]
> %W[\(] # => ["("]
```

## %i / %I

The meaning changes.

```ruby
> %i(\() # => [:"("]
> %i[\(] # => [:"\\("]
```

The meaning does not change

```ruby
> %I(\() # => [:"("]
> %I[\(] # => [:"("]
```

Also, if we use open delimiter and closing delimiter like `(...)`, we
need to target both of them.

```ruby
> %w(\() # => ["("]
> %w[\(] # => ["\\("]
> %w(\)) # => [")"]
> %w[\)] # => ["\\)"]
```

There are 4 cases of `%w(...)`, `%w[...]`, `%w{...}`, and `%w<...>`.

This commit changes so that it will not be offense if the same character
as a percent notation delimiter is used for elements of an array.
bbatsov pushed a commit that referenced this issue Nov 11, 2017
…ters` cop (#5020)

Fixes #4363.

Percent notation targeted by Issue # 4363 are `%w` and `%i`.
`%W` and `%I` are not targets.

## %w / %W

The meaning changes.

```ruby
> %w(\() # => ["("]
> %w[\(] # => ["\\("]
```

The meaning does not change.

```ruby
> %W(\() # => ["("]
> %W[\(] # => ["("]
```

## %i / %I

The meaning changes.

```ruby
> %i(\() # => [:"("]
> %i[\(] # => [:"\\("]
```

The meaning does not change

```ruby
> %I(\() # => [:"("]
> %I[\(] # => [:"("]
```

Also, if we use open delimiter and closing delimiter like `(...)`, we
need to target both of them.

```ruby
> %w(\() # => ["("]
> %w[\(] # => ["\\("]
> %w(\)) # => [")"]
> %w[\)] # => ["\\)"]
```

There are 4 cases of `%w(...)`, `%w[...]`, `%w{...}`, and `%w<...>`.

This commit changes so that it will not be offense if the same character
as a percent notation delimiter is used for elements of an array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants