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] Honour shareable_constant_value magic comment
- Loading branch information
1 parent
2053dbe
commit 22db5aa
Showing
8 changed files
with
249 additions
and
3 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
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/rubocop/issues/9328): Recognize shareable_constant_value magic comment. ([@thearjunmdas][]) |
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common functionality for dealing with Shareable Constant Value | ||
module ShareableConstantValue | ||
module_function | ||
|
||
def recent_shareable_value?(node) | ||
shareable_constant_comment = magic_comment_in_scope node | ||
return false if shareable_constant_comment.nil? | ||
|
||
shareable_constant_value = MagicComment.parse(shareable_constant_comment) | ||
.shareable_constant_value | ||
shareable_constant_value_enabled? shareable_constant_value | ||
end | ||
|
||
# Identifies the most recent magic comment with valid shareable constant values | ||
# thats in scope for this node | ||
def magic_comment_in_scope(node) | ||
processed_source_till_node(node).reverse_each.find do |line| | ||
MagicComment.parse(line).valid_shareable_constant_value? | ||
end | ||
end | ||
|
||
private | ||
|
||
def processed_source_till_node(node) | ||
processed_source.lines[0..(node.last_line - 1)] | ||
end | ||
|
||
def shareable_constant_value_enabled?(value) | ||
%w[literal experimental_everything experimental_copy].include? value | ||
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
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