From 162ef92f9c6f138ea016d2ea6cbe70870ce93a80 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 25 Dec 2022 16:57:01 +0900 Subject: [PATCH] Suppress `Layout/ClassStructure` offenses Follow up https://github.com/rubocop/rubocop/pull/11329 ```console % bundle exec rake (snip) Offenses: lib/rubocop/ast/node_pattern.rb:105:7: C: [Correctable] Layout/ClassStructure: public_class_methods is supposed to appear before initializer. def self.descend(element, &block) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/rubocop/ast/processed_source.rb:23:7: C: [Correctable] Layout/ClassStructure: constants is supposed to appear before public_class_methods. INVALID_LEVELS = %i[error fatal].freeze ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tasks/changelog.rb:121:3: C: [Correctable] Layout/ClassStructure: public_class_methods is supposed to appear before initializer. def self.pending? ... ^^^^^^^^^^^^^^^^^ 162 files inspected, 3 offenses detected, 3 offenses autocorrectable ``` --- lib/rubocop/ast/node_pattern.rb | 32 ++++++++++++++--------------- lib/rubocop/ast/processed_source.rb | 6 +++--- tasks/changelog.rb | 29 +++++++++++++------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/rubocop/ast/node_pattern.rb b/lib/rubocop/ast/node_pattern.rb index a3e5cddad..ef3879be7 100644 --- a/lib/rubocop/ast/node_pattern.rb +++ b/lib/rubocop/ast/node_pattern.rb @@ -54,6 +54,22 @@ def def_node_search(method_name, pattern_str, **keyword_defaults) VAR = 'node' + # Yields its argument and any descendants, depth-first. + # + def self.descend(element, &block) + return to_enum(__method__, element) unless block + + yield element + + if element.is_a?(::RuboCop::AST::Node) + element.children.each do |child| + descend(child, &block) + end + end + + nil + end + attr_reader :pattern, :ast, :match_code def_delegators :@compiler, :captures, :named_parameters, :positional_parameters @@ -100,22 +116,6 @@ def init_with(coder) # :nodoc: initialize(coder['pattern']) end - # Yields its argument and any descendants, depth-first. - # - def self.descend(element, &block) - return to_enum(__method__, element) unless block - - yield element - - if element.is_a?(::RuboCop::AST::Node) - element.children.each do |child| - descend(child, &block) - end - end - - nil - end - def freeze @match_code.freeze @compiler.freeze diff --git a/lib/rubocop/ast/processed_source.rb b/lib/rubocop/ast/processed_source.rb index 029202365..2333e4f53 100644 --- a/lib/rubocop/ast/processed_source.rb +++ b/lib/rubocop/ast/processed_source.rb @@ -12,6 +12,9 @@ class ProcessedSource # @api private STRING_SOURCE_NAME = '(string)' + INVALID_LEVELS = %i[error fatal].freeze + private_constant :INVALID_LEVELS + attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics, :parser_error, :raw_source, :ruby_version @@ -20,9 +23,6 @@ def self.from_file(path, ruby_version) new(file, ruby_version, path) end - INVALID_LEVELS = %i[error fatal].freeze - private_constant :INVALID_LEVELS - def initialize(source, ruby_version, path = nil) # Defaults source encoding to UTF-8, regardless of the encoding it has # been read with, which could be non-utf8 depending on the default diff --git a/tasks/changelog.rb b/tasks/changelog.rb index 87f568baf..9b5945e3b 100644 --- a/tasks/changelog.rb +++ b/tasks/changelog.rb @@ -77,6 +77,21 @@ def github_user user end end + + def self.pending? + entry_paths.any? + end + + def self.entry_paths + Dir["#{ENTRIES_PATH}*"] + end + + def self.read_entries + entry_paths.to_h do |path| + [path, File.read(path)] + end + end + attr_reader :header, :rest def initialize(content: File.read(PATH), entries: Changelog.read_entries) @@ -118,20 +133,6 @@ def merge_content ].join("\n") end - def self.pending? - entry_paths.any? - end - - def self.entry_paths - Dir["#{ENTRIES_PATH}*"] - end - - def self.read_entries - entry_paths.to_h do |path| - [path, File.read(path)] - end - end - def new_contributor_lines contributors .map { |user| format(CONTRIBUTOR, user: user) }