Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix #4097] Add require English for special globals
When replacing short global variables with descriptive aliases from English stdlib module, ensure English module is required. Adds a new configuration `RequireEnglish` (default `true`) for `Style/SpecialGlobalVars` cop. When enabled, this will add a `require 'English'` statement if not already present at the top of the file.
- Loading branch information
Showing
9 changed files
with
316 additions
and
116 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 @@ | ||
* [#4097](https://github.com/rubocop/rubocop/issues/4097): Add require English for special globals. ([@biinari][]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# This class ensures a require statement is present for a standard library | ||
# determined by the variable library_name | ||
class RequireLibraryCorrector | ||
extend RangeHelp | ||
|
||
class << self | ||
def correct(corrector, node, library_name) | ||
node = node.parent while node.parent? | ||
node = node.children.first if node.begin_type? | ||
corrector.insert_before(node, require_statement(library_name)) | ||
end | ||
|
||
def require_statement(library_name) | ||
"require '#{library_name}'\n" | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Ensure a require statement is present for a standard library determined | ||
# by variable library_name | ||
module RequireLibrary | ||
extend NodePattern::Macros | ||
|
||
def ensure_required(corrector, node, library_name) | ||
node = node.parent while node.parent&.parent? | ||
|
||
if node.parent&.begin_type? | ||
return if @required_libs.include?(library_name) | ||
|
||
remove_subsequent_requires(corrector, node, library_name) | ||
end | ||
|
||
RequireLibraryCorrector.correct(corrector, node, library_name) | ||
end | ||
|
||
def remove_subsequent_requires(corrector, node, library_name) | ||
node.right_siblings.each do |sibling| | ||
next unless require_library_name?(sibling, library_name) | ||
|
||
range = range_by_whole_lines(sibling.source_range, include_final_newline: true) | ||
corrector.remove(range) | ||
end | ||
end | ||
|
||
def on_send(node) | ||
return if node.parent&.parent? | ||
|
||
name = require_any_library?(node) | ||
return if name.nil? | ||
|
||
@required_libs.add(name) | ||
end | ||
|
||
private | ||
|
||
def on_new_investigation | ||
# Holds the required files at top-level | ||
@required_libs = Set.new | ||
super | ||
end | ||
|
||
# @!method require_any_library?(node) | ||
def_node_matcher :require_any_library?, <<~PATTERN | ||
(send {(const {nil? cbase} :Kernel) nil?} :require (str $_)) | ||
PATTERN | ||
|
||
# @!method require_library_name?(node, library_name) | ||
def_node_matcher :require_library_name?, <<~PATTERN | ||
(send {(const {nil? cbase} :Kernel) nil?} :require (str %1)) | ||
PATTERN | ||
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.