Skip to content
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

Docs: Improve example of Lint/ToJSON #8605

Merged
merged 1 commit into from Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -3833,12 +3833,23 @@ for an optional argument, your method should too.

[source,ruby]
----
# bad
def to_json
end
class Point
attr_reader :x, :y

# good
def to_json(*_args)
# bad, incorrect arity
def to_json
JSON.generate([x, y])
end

# good, preserving args
def to_json(*args)
JSON.generate([x, y], *args)
end

# good, discarding args
def to_json(*_args)
JSON.generate([x, y])
end
end
----

Expand Down
21 changes: 16 additions & 5 deletions lib/rubocop/cop/lint/to_json.rb
Expand Up @@ -9,12 +9,23 @@ module Lint
# for an optional argument, your method should too.
#
# @example
# # bad
# def to_json
# end
# class Point
# attr_reader :x, :y
#
# # bad, incorrect arity
# def to_json
# JSON.generate([x, y])
# end
#
# # good, preserving args
# def to_json(*args)
# JSON.generate([x, y], *args)
# end
#
# # good
# def to_json(*_args)
# # good, discarding args
# def to_json(*_args)
# JSON.generate([x, y])
# end
# end
#
class ToJSON < Base
Expand Down