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

Feature Request: Warn invalid location of coding pragma #5490

Closed
znz opened this issue Jan 20, 2018 · 0 comments
Closed

Feature Request: Warn invalid location of coding pragma #5490

znz opened this issue Jan 20, 2018 · 0 comments

Comments

@znz
Copy link
Contributor

znz commented Jan 20, 2018

coding pragma should be first line, or next of shebang.

%  cat /tmp/bad.rb
# frozen_string_literal: true
# coding: ascii
p [''.frozen?, ''.encoding]
%  ruby /tmp/bad.rb
[true, #<Encoding:UTF-8>]
%  cat /tmp/good.rb
# coding: ascii
# frozen_string_literal: true
p [''.frozen?, ''.encoding]
%  ruby /tmp/good.rb
[true, #<Encoding:US-ASCII>]

example: https://github.com/plataformatec/devise/pull/4753/files

koic added a commit to koic/rubocop that referenced this issue Feb 5, 2018
Fixes rubocop#5490.

## Feature

This cop checks coding magic comment should be first line,
or next of shebang.

### Bad case

```ruby
# frozen_string_literal: true
# coding: ascii
```

### Good case

```ruby
# coding: ascii
# frozen_string_literal: true
```

This cop checks only the order of magic comments.  This cop will not do
what other cops have confirmed. (e.g. Such as checking whether Ruby
version supports frozen string literal, etc.)

Also, this cop doesn't check whether shebang is at beginning of file.
if shebang is not at beginning of file, executable script will not
work as intended. IMO, to check if shebang is at beginning of file,
it may be better to make a cop for that.
bbatsov pushed a commit that referenced this issue Feb 5, 2018
Fixes #5490.

## Feature

This cop checks coding magic comment should be first line,
or next of shebang.

### Bad case

```ruby
# frozen_string_literal: true
# coding: ascii
```

### Good case

```ruby
# coding: ascii
# frozen_string_literal: true
```

This cop checks only the order of magic comments.  This cop will not do
what other cops have confirmed. (e.g. Such as checking whether Ruby
version supports frozen string literal, etc.)

Also, this cop doesn't check whether shebang is at beginning of file.
if shebang is not at beginning of file, executable script will not
work as intended. IMO, to check if shebang is at beginning of file,
it may be better to make a cop for that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants