ruby-grape / ruby-grape/grape

Best practices via Rubocop

Open
#1,694 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Is there a list of good practices for Grape? I would like to add some rubocop cops to check developers are following good practices.

For example: it is better to use declared(params) instead of params as params doesn't remove unvalidated params. Cop:

module RuboCop
  module Cop
    module Grape
      class Params < RuboCop::Cop::Cop
        MSG = 'Avoid using params variable. Use declared(params) instead (to remove unvalidated params). More info: https://github.com/ruby-grape/grape#declared'.freeze

        def_node_matcher :params_used?, <<-END
          (send (send nil :params) :[] ...)
        END

        def on_send(node)
          return unless in_grape_api?(node)
          params_used?(node) do
            add_offense(node, :expression, format(MSG, node.source))
          end
        end

        private

        def in_grape_api?(node)
          path = node.location.expression.source_buffer.name
          models_path = File.join(Dir.pwd, 'app', 'api')

          path.start_with?(models_path)
        end
      end
    end
  end
end

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed Params cop and the linked Grape guidance for declared(params). Determine which Grape best practices are agreed and which RuboCop cops would be in scope; done requires an agreed practices list and implementation scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend-api-design, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.