-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Style/FileTouch
cop
#13484
Merged
bbatsov
merged 1 commit into
rubocop:master
from
lovro-bikic:feature/style-file-touch-cop
Nov 26, 2024
Merged
Add new Style/FileTouch
cop
#13484
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
* [#13484](https://github.com/rubocop/rubocop/pull/13484): Add new `Style/FileTouch` cop. ([@lovro-bikic][]) |
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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for usage of `File.open` in append mode with empty block. | ||
# | ||
# Such a usage only creates a new file, but it doesn't update | ||
# timestamps for an existing file, which might have been the intention. | ||
# | ||
# For example, for an existing file `foo.txt`: | ||
# | ||
# ruby -e "puts File.mtime('foo.txt')" | ||
# # 2024-11-26 12:17:23 +0100 | ||
# | ||
# ruby -e "File.open('foo.txt', 'a') {}" | ||
# | ||
# ruby -e "puts File.mtime('foo.txt')" | ||
# # 2024-11-26 12:17:23 +0100 -> unchanged | ||
# | ||
# If the intention was to update timestamps, `FileUtils.touch('foo.txt')` | ||
# should be used instead. | ||
# | ||
# @safety | ||
lovro-bikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Autocorrection is unsafe for this cop because unlike `File.open`, | ||
# `FileUtils.touch` updates an existing file's timestamps. | ||
# | ||
# @example | ||
# # bad | ||
# File.open(filename, 'a') {} | ||
# File.open(filename, 'a+') {} | ||
# | ||
# # good | ||
# FileUtils.touch(filename) | ||
# | ||
class FileTouch < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `FileUtils.touch(%<argument>s)` instead of `File.open` in ' \ | ||
'append mode with empty block.' | ||
|
||
RESTRICT_ON_SEND = %i[open].freeze | ||
|
||
APPEND_FILE_MODES = %w[a a+ ab a+b at a+t].to_set.freeze | ||
lovro-bikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# @!method file_open?(node) | ||
def_node_matcher :file_open?, <<~PATTERN | ||
(send | ||
(const {nil? cbase} :File) :open | ||
$(...) | ||
(str %APPEND_FILE_MODES)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
filename = file_open?(node) | ||
parent = node.parent | ||
|
||
return unless filename | ||
return unless parent && empty_block?(parent) | ||
|
||
message = format(MSG, argument: filename.source) | ||
add_offense(parent, message: message) do |corrector| | ||
corrector.replace(parent, "FileUtils.touch(#{filename.source})") | ||
end | ||
end | ||
|
||
private | ||
|
||
def empty_block?(node) | ||
node.block_type? && !node.body | ||
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::FileTouch, :config do | ||
it 'registers an offense when using `File.open` in append mode with empty block' do | ||
expect_offense(<<~RUBY) | ||
File.open(filename, 'a') {} | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `FileUtils.touch(filename)` instead of `File.open` in append mode with empty block. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
FileUtils.touch(filename) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `File.open` in append mode without a block' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(filename, 'a') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `File.open` in write mode' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(filename, 'w') {} | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `File.open` without an access mode' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(filename) {} | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this should be a Lint cop instead. In the wild, I've seen
File.open
used to make sure a file exists or to try update its timestamps (much liketouch
utility), the latter of which actually doesn't happen:so this could be considered erroneous code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not on the fence about changing the department, but I think it'd be good to add this to the cop's rationale and examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I've updated the docs, let me know if I made it clear enough.