Skip to content

Add ConstantInRoutes Cop #1492

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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_add_constant_in_routes_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1492](https://github.com/rubocop/rubocop-rails/pull/1492) Add new `ConstantInRoutes` Cop. ([@skipkayhil][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ Rails/CompactBlank:
Safe: false
VersionAdded: '2.13'

Rails/ConstantInRoutes:
Description: 'Checks for constants defined in config/routes.rb files.'
Enabled: pending
Safe: false
VersionAdded: <<next>>

Rails/ContentTag:
Description: 'Use `tag.something` instead of `tag(:something)`.'
Reference:
Expand Down
43 changes: 43 additions & 0 deletions lib/rubocop/cop/rails/constant_in_routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Prevent defining constants in config/routes.rb files.
#
# Defining constants in a routes.rb file can lead to constant redefinition
# warnings because routes.rb is a reloadable file.
#
# @example
# # bad
# Rails.application.routes.draw do
# ADMIN_CONSTRAINT = ->(req) { req.subdomain == 'admin' }
#
# get "/users", constraints: ADMIN_CONSTRAINT
# end
#
# # good
# Rails.application.routes.draw do
# admin_constraint = ->(req) { req.subdomain == 'admin' }
#
# get "/users", constraints: admin_constraint
# end
#
class ConstantInRoutes < Base
MSG = 'Do not define constants in config/routes.rb. Use a local variable or move this constant somewhere else.'

def on_casgn(node)
return unless in_routes_file?

add_offense(node)
end

private

def in_routes_file?
processed_source.file_path&.end_with?('config/routes.rb')
end
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 @@ -33,6 +33,7 @@
require_relative 'rails/blank'
require_relative 'rails/bulk_change_table'
require_relative 'rails/compact_blank'
require_relative 'rails/constant_in_routes'
require_relative 'rails/content_tag'
require_relative 'rails/create_table_with_timestamps'
require_relative 'rails/dangerous_column_names'
Expand Down
41 changes: 41 additions & 0 deletions spec/rubocop/cop/rails/constant_in_routes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::ConstantInRoutes, :config do
context 'when in config/routes.rb file' do
let(:source) { 'config/routes.rb' }

it 'registers an offense when defining constants in routes' do
expect_offense(<<~RUBY, source)
API_VERSION = 'v1'
^^^^^^^^^^^^^^^^^^ Do not define constants in config/routes.rb. Use a local variable or move this constant somewhere else.
Rails.application.routes.draw do
BASE_PATH = '/api'
^^^^^^^^^^^^^^^^^^ Do not define constants in config/routes.rb. Use a local variable or move this constant somewhere else.
get "\#{BASE_PATH}\#{API_VERSION}/users", to: 'users#index'
end
RUBY
end

it 'registers an offense when defining nested constants' do
expect_offense(<<~RUBY, source)
Api::VERSION = 'v1'
^^^^^^^^^^^^^^^^^^^ Do not define constants in config/routes.rb. Use a local variable or move this constant somewhere else.
Rails.application.routes.draw do
get "/users", to: 'users#index'
end
RUBY
end

it 'registers an offense when defining constants in opened classes' do
expect_offense(<<~RUBY, source)
class Api
VERSION = 'v1'
^^^^^^^^^^^^^^ Do not define constants in config/routes.rb. Use a local variable or move this constant somewhere else.
end
Rails.application.routes.draw do
get "/users", to: 'users#index'
end
RUBY
end
end
end