forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#9328] Recognize shareable_constant_value magic comment
- Loading branch information
Showing
7 changed files
with
339 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#9328](https://github.com/rubocop-hq/rubocop/issues/9328): Recognize shareable_constant_value magic comment. ([@caalberts][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common functionality for dealing with shareable constant value. | ||
module ShareableConstantValue | ||
module_function | ||
|
||
LITERAL = :literal | ||
EXPERIMENTAL_ANYTHING = :experimental_everything | ||
|
||
def shareable_constant_value? | ||
target_ruby_version >= 3.0 && shareable_constant_value_comment_exists? | ||
end | ||
|
||
def shareable_constant_value_comment_exists? | ||
leading_comments.any? do |line| | ||
shareable_constant_value = MagicComment.parse(line).shareable_constant_value | ||
|
||
[LITERAL, EXPERIMENTAL_ANYTHING].include?(shareable_constant_value) | ||
end | ||
end | ||
|
||
private | ||
|
||
def leading_comments | ||
processed_source | ||
.tokens | ||
.take_while(&:comment?) | ||
.map(&:text) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.