Skip to content

Commit

Permalink
[Fix #9327] Change Layout/EmptyLineAfterMagicComment to accept top-…
Browse files Browse the repository at this point in the history
…level `shareable_constant_values` directive

Closes #9327
  • Loading branch information
tejasbubane authored and bbatsov committed Jan 26, 2021
1 parent 052eb8a commit 233e4e5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9327](https://github.com/rubocop-hq/rubocop/issues/9327): Change `Layout/EmptyLineAfterMagicComment` to accept top-level `shareable_constant_values` directive. ([@tejasbubane][])
31 changes: 30 additions & 1 deletion lib/rubocop/magic_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(comment)
end

def any?
frozen_string_literal_specified? || encoding_specified?
frozen_string_literal_specified? || encoding_specified? || shareable_constant_value_specified?
end

# Does the magic comment enable the frozen string literal feature.
Expand All @@ -46,13 +46,24 @@ def valid_literal_value?
[true, false].include?(frozen_string_literal)
end

def valid_shareable_constant_value?
%w[none literal experimental_everything experimental_copy].include?(shareable_constant_values)
end

# Was a magic comment for the frozen string literal found?
#
# @return [Boolean]
def frozen_string_literal_specified?
specified?(frozen_string_literal)
end

# Was a shareable_constant_value specified?
#
# @return [Boolean]
def shareable_constant_value_specified?
specified?(shareable_constant_value)
end

# Expose the `frozen_string_literal` value coerced to a boolean if possible.
#
# @return [Boolean] if value is `true` or `false`
Expand All @@ -69,6 +80,13 @@ def frozen_string_literal
end
end

# Expose the `shareable_constant_value` value coerced to a boolean if possible.
#
# @return [String] for shareable_constant_value config
def shareable_constant_value
extract_shareable_constant_value
end

def encoding_specified?
specified?(encoding)
end
Expand Down Expand Up @@ -146,6 +164,10 @@ def encoding
def extract_frozen_string_literal
match('frozen[_-]string[_-]literal')
end

def extract_shareable_constant_value
match('shareable[_-]constant[_-]values')
end
end

# Wrapper for Vim style magic comments.
Expand Down Expand Up @@ -176,6 +198,9 @@ def encoding

# Vim comments cannot specify frozen string literal behavior.
def frozen_string_literal; end

# Vim comments cannot specify shareable constant values behavior.
def shareable_constant_value; end
end

# Wrapper for regular magic comments not bound to an editor.
Expand Down Expand Up @@ -209,6 +234,10 @@ def encoding
def extract_frozen_string_literal
extract(/\A\s*#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/io)
end

def extract_shareable_constant_value
extract(/\A\s*#\s*shareable[_-]constant[_-]value:\s*(#{TOKEN})\s*\z/io)
end
end
end
end
41 changes: 41 additions & 0 deletions spec/rubocop/cop/layout/empty_line_after_magic_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@ class Foo; end
RUBY
end

it 'accepts magic comment followed by encoding' do
expect_no_offenses(<<~RUBY)
# frozen_string_literal: true
# encoding: utf-8
class Foo; end
RUBY
end

it 'accepts magic comment with shareable_constant_value' do
expect_no_offenses(<<~RUBY)
# frozen_string_literal: true
# shareable_constant_value: literal
class Foo; end
RUBY

expect_no_offenses(<<~RUBY)
# shareable_constant_value: experimental_everything
# frozen_string_literal: true
class Foo; end
RUBY
end

it 'registers offense when frozen_string_literal used with shareable_constant_value without empty line' do
expect_offense(<<~RUBY)
# frozen_string_literal: true
# shareable_constant_value: none
class Foo; end
^ Add an empty line after magic comments.
RUBY

expect_correction(<<~RUBY)
# frozen_string_literal: true
# shareable_constant_value: none
class Foo; end
RUBY
end

it 'accepts code that separates the comment from the code with a newline' do
expect_no_offenses(<<~RUBY)
# frozen_string_literal: true
Expand Down

0 comments on commit 233e4e5

Please sign in to comment.