Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_json_symbolize_names_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1532](https://github.com/rubocop/rubocop-rails/pull/1532): Add new `Rails/JSONSymbolizeNames` cop. ([@viralpraxis][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ Rails/InverseOf:
Include:
- '**/app/models/**/*.rb'

Rails/JSONSymbolizeNames:
Description: 'Use `JSON.parse(json, symbolize_names: true)` instead of `JSON.parse(json).deep_symbolize_keys`.'
Enabled: pending
VersionAdded: '<<next>>'

Rails/LexicallyScopedActionFilter:
Description: "Checks that methods specified in the filter's `only` or `except` options are explicitly defined in the class."
StyleGuide: 'https://rails.rubystyle.guide#lexically-scoped-action-filter'
Expand Down
40 changes: 40 additions & 0 deletions lib/rubocop/cop/rails/json_symbolize_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Use `JSON.parse(json, symbolize_names: true)` instead of `JSON.parse(json).deep_symbolize_keys`.

# Using `symbolize_names: true` is more efficient as it creates symbols during parsing
# rather than requiring a second pass through the data structure.

# @example
# # bad
# JSON.parse(json).deep_symbolize_keys
#
# # good
# JSON.parse(json, symbolize_names: true)
#
class JSONSymbolizeNames < Base
MSG = 'Use `symbolize_names` option.'

RESTRICT_ON_SEND = %i[deep_symbolize_keys].to_set.freeze

JSON_PARSING_METHOD_NAMES = %i[load_file load_file! parse parse!].to_set.freeze

# @!method deep_symbolize_keys?(node)
def_node_matcher :deep_symbolize_keys?, <<~PATTERN
(call
(send (const {nil? cbase} :JSON) JSON_PARSING_METHOD_NAMES ...) :deep_symbolize_keys)
PATTERN

def on_send(node)
deep_symbolize_keys?(node) do
add_offense(node)
end
end
alias on_csend on_send
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
require_relative 'rails/index_with'
require_relative 'rails/inquiry'
require_relative 'rails/inverse_of'
require_relative 'rails/json_symbolize_names'
require_relative 'rails/lexically_scoped_action_filter'
require_relative 'rails/link_to_blank'
require_relative 'rails/mailer_name'
Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/cop/rails/json_symbolize_names_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::JSONSymbolizeNames, :config do
%i[load_file load_file! parse parse!].each do |method_name|
context "with `#{method_name}` method" do
it "registers an offense for `JSON.#{method_name}` followed by `deep_symbolize_keys`" do
expect_offense(<<~RUBY, method_name: method_name)
JSON.#{method_name}(json).deep_symbolize_keys
^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option.
RUBY
end

it "registers an offense for `::JSON.#{method_name}` followed by `deep_symbolize_keys`" do
expect_offense(<<~RUBY, method_name: method_name)
::JSON.#{method_name}(json).deep_symbolize_keys
^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option.
RUBY
end

it "registers an offense for `::JSON.#{method_name}` followed by `deep_symbolize_keys` with safe navigation" do
expect_offense(<<~RUBY, method_name: method_name)
::JSON.#{method_name}("null")&.deep_symbolize_keys
^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option.
RUBY
end

it "does not register for `JSON.#{method_name}` with `symbolize_names` option" do
expect_no_offenses(<<~RUBY)
JSON.#{method_name}(json, symbolize_names: true)
RUBY
end

it "does not register for single `JSON.#{method_name}`" do
expect_no_offenses(<<~RUBY)
JSON.#{method_name}(json)
RUBY
end
end
end
end