-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix #1410] Make registered cops aware of `AllCops: MigratedSchemaVer…
…sion` When implementing #1383, the detection of `AllCops: MigratedSchemaVersion` was initially considered only for cops related to migrations. However, feedback received later, such as in #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 #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 overrides the `add_global_offense` and `add_offense` methods, | ||
# ensuring that cops skip processing if the file is determined to be a migrated file | ||
# based on the schema version. | ||
# | ||
# @api private | ||
module MigrationFileSkippable | ||
def add_global_offense(message = nil, severity: nil) | ||
return if already_migrated_file? | ||
|
||
super if method(__method__).super_method | ||
end | ||
|
||
def add_offense(node_or_range, message: nil, severity: nil, &block) | ||
return if already_migrated_file? | ||
|
||
super if method(__method__).super_method | ||
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 | ||
@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