-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This fixes issue #6378 classes that override to_json need to ensure an optional argument is available. class MyClass def to_json end end While calling MyClass.new.to_json works, calling JSON.generate(MyClass.new) results in an ArgumentError.
- Loading branch information
Anthony Ross
committed
Dec 14, 2018
1 parent
3d4ba9c
commit 8fd679e
Showing
9 changed files
with
118 additions
and
0 deletions.
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
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks to make sure `#to_json` includes an optional argument. | ||
# When overriding `#to_json`, callers may invoke JSON | ||
# generation via `JSON.generate(your_obj)`. Since `JSON#generate` allows | ||
# for an optional argument, your method should too. | ||
# | ||
# @example | ||
# # bad | ||
# def to_json | ||
# end | ||
# | ||
# # good | ||
# def to_json(_opts) | ||
# end | ||
# | ||
class ToJSON < Cop | ||
MSG = ' `#to_json` requires an optional argument to be parsable ' \ | ||
'via JSON.generate(obj).'.freeze | ||
|
||
def on_def(node) | ||
return unless node.method?(:to_json) && node.arguments.empty? | ||
|
||
add_offense(node) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.insert_after(node.loc.name, '(_opts)') | ||
end | ||
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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::ToJSON do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense when using `#to_json` without arguments' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
def to_json | ||
^^^^^^^^^^^ `#to_json` requires an optional argument to be parsable via JSON.generate(obj). | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#to_json` with arguments' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
def to_json(opts) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'autocorrects' do | ||
corrected = autocorrect_source(<<-RUBY.strip_indent) | ||
def to_json | ||
end | ||
RUBY | ||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
def to_json(_opts) | ||
end | ||
RUBY | ||
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