From 72f8c698ad17f235762ecd265cbb5beb126b2661 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sun, 30 Oct 2022 10:18:40 +0100 Subject: [PATCH] Update the `Style/ModuleFunction` documentation to suggest `class << self` as an alternative (#11045) --- .../change_module_function-forbidden_msg.md | 1 + lib/rubocop/cop/style/module_function.rb | 34 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 changelog/change_module_function-forbidden_msg.md diff --git a/changelog/change_module_function-forbidden_msg.md b/changelog/change_module_function-forbidden_msg.md new file mode 100644 index 00000000000..1b86fcf94ab --- /dev/null +++ b/changelog/change_module_function-forbidden_msg.md @@ -0,0 +1 @@ +* [#11045](https://github.com/rubocop/rubocop/pull/11045): Update the `Style/ModuleFunction` documentation to suggest `class << self` as an alternative. ([@rdeckard][]) diff --git a/lib/rubocop/cop/style/module_function.rb b/lib/rubocop/cop/style/module_function.rb index 39e0e0b4d14..b9044c225af 100644 --- a/lib/rubocop/cop/style/module_function.rb +++ b/lib/rubocop/cop/style/module_function.rb @@ -3,13 +3,15 @@ module RuboCop module Cop module Style - # Checks for use of `extend self` or `module_function` in a - # module. + # Checks for use of `extend self` or `module_function` in a module. # - # Supported styles are: module_function, extend_self, forbidden. `forbidden` - # style prohibits the usage of both styles. + # Supported styles are: `module_function` (default), `extend_self` and `forbidden`. # - # NOTE: the cop won't be activated when the module contains any private methods. + # NOTES: + # + # - `forbidden` style prohibits the usage of both styles + # - in default mode (`module_function`), the cop won't be activated when the module + # contains any private methods # # @safety # Autocorrection is unsafe (and is disabled by default) because `extend self` @@ -28,7 +30,6 @@ module Style # # ... # end # - # @example EnforcedStyle: module_function (default) # # good # module Test # extend self @@ -37,6 +38,13 @@ module Style # # ... # end # + # # good + # module Test + # class << self + # # ... + # end + # end + # # @example EnforcedStyle: extend_self # # bad # module Test @@ -50,6 +58,13 @@ module Style # # ... # end # + # # good + # module Test + # class << self + # # ... + # end + # end + # # @example EnforcedStyle: forbidden # # bad # module Test @@ -70,6 +85,13 @@ module Style # private # # ... # end + # + # # good + # module Test + # class << self + # # ... + # end + # end class ModuleFunction < Base include ConfigurableEnforcedStyle extend AutoCorrector