forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#1410] Make registered cops aware of `AllCops: MigratedSc…
…hemaVersion` When implementing rubocop#1383, the detection of `AllCops: MigratedSchemaVersion` was initially considered only for cops related to migrations. However, feedback received later, such as in rubocop#1410, indicated that it is also expected to apply to `Style`, `Lint`, and other categories. This suggestion is reasonable, as warnings for migrated files may not be limited to database columns but could also include Ruby programming logic. Excluding `Style` and `Lint` from this consideration would not align with this feedback. This PR modifies the behavior so that all registered cops can detect the value of `AllCops: MigratedSchemaVersion`. Fixes rubocop#1410.
- Loading branch information
Showing
14 changed files
with
79 additions
and
38 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/change_make_registered_cops_aware_of_all_cops_migrated_schema_version.md
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 @@ | ||
* [#1410](https://github.com/rubocop/rubocop-rails/issues/1410): Make registered cops aware of `AllCops: MigratedSchemaVersion`. ([@koic][]) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Rails | ||
# This module allows cops to detect and ignore files that have already been migrated | ||
# by leveraging the `AllCops: MigratedSchemaVersion` configuration. | ||
# | ||
# [source,yaml] | ||
# ----- | ||
# AllCops: | ||
# MigratedSchemaVersion: '20241225000000' | ||
# ----- | ||
# | ||
# When applied to cops, it dynamically overrides the `on_*` methods for all supported | ||
# node types, ensuring that cops skip processing if the file is determined to be a | ||
# migrated file based on the schema version. | ||
module MigrationFileSkippable | ||
Parser::Meta::NODE_TYPES.each do |node_type| | ||
method_name = :"on_#{node_type}" | ||
|
||
class_eval(<<~RUBY, __FILE__, __LINE__ + 1) | ||
def #{method_name}(node, *args) # def on_if(node, *args) | ||
return if already_migrated_file? # return if already_migrated_file? | ||
# | ||
super if method(__method__).super_method # super if method(__method__).super_method | ||
rescue NameError # rescue NameError | ||
# noop # # noop | ||
end # end | ||
RUBY | ||
end | ||
|
||
def self.apply_to_cops! | ||
RuboCop::Cop::Registry.all.each { |cop| cop.prepend(MigrationFileSkippable) } | ||
end | ||
|
||
private | ||
|
||
def already_migrated_file? | ||
return false unless migrated_schema_version | ||
|
||
match_data = File.basename(processed_source.file_path).match(/(?<timestamp>\d{14})/) | ||
schema_version = match_data['timestamp'] if match_data | ||
|
||
return false unless schema_version | ||
|
||
schema_version <= migrated_schema_version.to_s # Ignore applied migration files. | ||
end | ||
|
||
def migrated_schema_version | ||
config.for_all_cops.fetch('MigratedSchemaVersion', nil) | ||
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