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

[#8827] Change Style/FormatStringToken style to template internally #11349

Closed
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
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Style/AccessorGrouping:
- lib/rubocop/cop/offense.rb

Style/FormatStringToken:
EnforcedStyle: template
# Because we parse a lot of source codes from strings. Percent arrays
# look like unannotated format string tokens to this cop.
Exclude:
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config_obsoletion/changed_enforced_styles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ConfigObsoletion
# Encapsulation of a ConfigObsoletion rule for changing a parameter
# @api private
class ChangedEnforcedStyles < ParameterRule
BASE_MESSAGE = 'obsolete `%<parameter>s: %<value>s` (for `%<cop>s`) found in %<path>s'
BASE_MESSAGE = 'obsolete `%{parameter}: %{value}` (for `%{cop}`) found in %{path}'

def violated?
super && config[cop][parameter] == value
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config_obsoletion/changed_parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ConfigObsoletion
# Encapsulation of a ConfigObsoletion rule for changing a parameter
# @api private
class ChangedParameter < ParameterRule
BASE_MESSAGE = 'obsolete parameter `%<parameter>s` (for `%<cop>s`) found in %<path>s'
BASE_MESSAGE = 'obsolete parameter `%{parameter}` (for `%{cop}`) found in %{path}'

def message
base = format(BASE_MESSAGE, parameter: parameter, cop: cop, path: smart_loaded_path)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config_obsoletion/extracted_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def violated?
end

def rule_message
msg = '%<name>s been extracted to the `%<gem>s` gem.'
msg = '%{name} been extracted to the `%{gem}` gem.'
format(msg,
name: affected_cops.size > 1 ? "`#{department}` cops have" : "`#{old_name}` has",
gem: gem)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config_obsoletion/removed_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ConfigObsoletion
class RemovedCop < CopRule
attr_reader :old_name, :metadata

BASE_MESSAGE = 'The `%<old_name>s` cop has been removed'
BASE_MESSAGE = 'The `%{old_name}` cop has been removed'

def initialize(config, old_name, metadata)
super(config, old_name)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/bundler/duplicated_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module Bundler
class DuplicatedGem < Base
include RangeHelp

MSG = 'Gem `%<gem_name>s` requirements already given on line ' \
'%<line_of_first_occurrence>d of the Gemfile.'
MSG = 'Gem `%{gem_name}` requirements already given on line ' \
'%{line_of_first_occurrence} of the Gemfile.'

def on_new_investigation
return if processed_source.blank?
Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/bundler/gem_filename.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class GemFilename < Base
include RangeHelp

MSG_GEMFILE_REQUIRED = '`gems.rb` file was found but `Gemfile` is required ' \
'(file path: %<file_path>s).'
'(file path: %{file_path}).'
MSG_GEMS_RB_REQUIRED = '`Gemfile` was found but `gems.rb` file is required ' \
'(file path: %<file_path>s).'
'(file path: %{file_path}).'
MSG_GEMFILE_MISMATCHED = 'Expected a `Gemfile.lock` with `Gemfile` but found ' \
'`gems.locked` file (file path: %<file_path>s).'
'`gems.locked` file (file path: %{file_path}).'
MSG_GEMS_RB_MISMATCHED = 'Expected a `gems.locked` file with `gems.rb` but found ' \
'`Gemfile.lock` (file path: %<file_path>s).'
'`Gemfile.lock` (file path: %{file_path}).'
GEMFILE_FILES = %w[Gemfile Gemfile.lock].freeze
GEMS_RB_FILES = %w[gems.rb gems.locked].freeze

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/bundler/insecure_protocol_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class InsecureProtocolSource < Base
include RangeHelp
extend AutoCorrector

MSG = 'The source `:%<source>s` is deprecated because HTTP requests ' \
MSG = 'The source `:%{source}` is deprecated because HTTP requests ' \
'are insecure. ' \
"Please change your source to 'https://rubygems.org' " \
"if possible, or 'http://rubygems.org' if not."
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/bundler/ordered_gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OrderedGems < Base

MSG = 'Gems should be sorted in an alphabetical order within their ' \
'section of the Gemfile. ' \
'Gem `%<previous>s` should appear before `%<current>s`.'
'Gem `%{previous}` should appear before `%{current}`.'

def on_new_investigation
return if processed_source.blank?
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/correctors/each_to_for_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Cop
class EachToForCorrector
extend NodePattern::Macros

CORRECTION_WITH_ARGUMENTS = 'for %<variables>s in %<collection>s do'
CORRECTION_WITHOUT_ARGUMENTS = 'for _ in %<enumerable>s do'
CORRECTION_WITH_ARGUMENTS = 'for %{variables} in %{collection} do'
CORRECTION_WITHOUT_ARGUMENTS = 'for _ in %{enumerable} do'

def initialize(block_node)
@block_node = block_node
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/for_to_each_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Cop
class ForToEachCorrector
extend NodePattern::Macros

CORRECTION = '%<collection>s.each do |%<argument>s|'
CORRECTION = '%{collection}.each do |%{argument}|'

def initialize(for_node)
@for_node = for_node
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DeprecatedAttributeAssignment < Base
include RangeHelp
extend AutoCorrector

MSG = 'Do not set `%<attribute>s` in gemspec.'
MSG = 'Do not set `%{attribute}` in gemspec.'

# @!method gem_specification(node)
def_node_matcher :gem_specification, <<~PATTERN
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/gemspec/duplicated_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class DuplicatedAssignment < Base
include RangeHelp
include GemspecHelp

MSG = '`%<assignment>s` method calls already given on line ' \
'%<line_of_first_occurrence>d of the gemspec.'
MSG = '`%{assignment}` method calls already given on line ' \
'%{line_of_first_occurrence} of the gemspec.'

# @!method assignment_method_declarations(node)
def_node_search :assignment_method_declarations, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/gemspec/ordered_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class OrderedDependencies < Base

MSG = 'Dependencies should be sorted in an alphabetical order within ' \
'their section of the gemspec. ' \
'Dependency `%<previous>s` should appear before `%<current>s`.'
'Dependency `%{previous}` should appear before `%{current}`.'

def on_new_investigation
return if processed_source.blank?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/gemspec/required_ruby_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RequiredRubyVersion < Base

RESTRICT_ON_SEND = %i[required_ruby_version=].freeze
NOT_EQUAL_MSG = '`required_ruby_version` and `TargetRubyVersion` ' \
'(%<target_ruby_version>s, which may be specified in ' \
'(%{target_ruby_version}, which may be specified in ' \
'.rubocop.yml) should be equal.'
MISSING_MSG = '`required_ruby_version` should be specified.'

Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Generator

module RuboCop
module Cop
module %<department>s
module %{department}
# TODO: Write cop description and example of bad / good code. For every
# `SupportedStyle` and unique configuration, there needs to be examples.
# Examples must have valid Ruby syntax. Do not use upticks.
Expand Down Expand Up @@ -53,7 +53,7 @@ module %<department>s
# # good
# good_foo_method(args)
#
class %<cop_name>s < Base
class %{cop_name} < Base
# TODO: Implement the cop in here.
#
# In many cases, you can use a node matcher for matching node pattern.
Expand Down Expand Up @@ -85,7 +85,7 @@ def on_send(node)
SPEC_TEMPLATE = <<~SPEC
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::%<department>s::%<cop_name>s, :config do
RSpec.describe RuboCop::Cop::%{department}::%{cop_name}, :config do
let(:config) { RuboCop::Config.new }

# TODO: Write test code
Expand All @@ -108,7 +108,7 @@ def on_send(node)

CONFIGURATION_ADDED_MESSAGE =
'[modify] A configuration for the cop is added into ' \
'%<configuration_file_path>s.'
'%{configuration_file_path}.'

def initialize(name, output: $stdout)
@badge = Badge.parse(name)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/generator/configuration_injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class Generator
# namespace and injects the provided one in alpha
class ConfigurationInjector
TEMPLATE = <<~YAML
%<badge>s:
%{badge}:
Description: 'TODO: Write a description of the cop.'
Enabled: pending
VersionAdded: '%<version_added>s'
VersionAdded: '%{version_added}'
YAML

def initialize(configuration_file_path:, badge:, version_added: '<<next>>')
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/cop_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module InternalAffairs
class CopDescription < Base
extend AutoCorrector

MSG = 'Description should be started with %<suggestion>s instead of `This cop ...`.'
MSG = 'Description should be started with %{suggestion} instead of `This cop ...`.'

SPECIAL_WORDS = %w[is can could should will would must may].freeze
COP_DESC_OFFENSE_REGEX = \
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/create_empty_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module InternalAffairs
class CreateEmptyFile < Base
extend AutoCorrector

MSG = 'Use `%<replacement>s`.'
MSG = 'Use `%{replacement}`.'
RESTRICT_ON_SEND = %i[create_file].freeze

def on_send(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module InternalAffairs
class EmptyLineBetweenExpectOffenseAndCorrection < Base
extend AutoCorrector

MSG = 'Add empty line between `expect_offense` and `%<expect_correction>s`.'
MSG = 'Add empty line between `expect_offense` and `%{expect_correction}`.'
RESTRICT_ON_SEND = %i[expect_offense].freeze
CORRECTION_EXPECTATION_METHODS = %i[expect_correction expect_no_corrections].freeze

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/example_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class << self
attr_accessor :descriptions
end

MSG = 'Description does not match use of `%<method_name>s`.'
MSG = 'Description does not match use of `%{method_name}`.'

RESTRICT_ON_SEND = %i[
expect_offense
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module InternalAffairs
class LambdaOrProc < Base
extend AutoCorrector

MSG = 'Use `%<prefer>s`.'
MSG = 'Use `%{prefer}`.'

# @!method lambda_or_proc(node)
def_node_matcher :lambda_or_proc, <<~PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module InternalAffairs
class LocationLineEqualityComparison < Base
extend AutoCorrector

MSG = 'Use `%<preferred>s`.'
MSG = 'Use `%{preferred}`.'

# @!method line_send(node)
def_node_matcher :line_send, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/method_name_end_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MethodNameEndWith < Base
include RangeHelp
extend AutoCorrector

MSG = 'Use `%<method_name>s` instead of `%<method_suffix>s`.'
MSG = 'Use `%{method_name}` instead of `%{method_suffix}`.'
SUGGEST_METHOD_FOR_SUFFIX = {
'=' => 'assignment_method?',
'!' => 'bang_method?',
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/method_name_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MethodNameEqual < Base
include RangeHelp
extend AutoCorrector

MSG = 'Use `method?(%<method_name>s)` instead of `method_name == %<method_name>s`.'
MSG = 'Use `method?(%{method_name})` instead of `method_name == %{method_name}`.'
RESTRICT_ON_SEND = %i[==].freeze

# @!method method_name?(node)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/internal_affairs/node_matcher_directive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class NodeMatcherDirective < Base
extend AutoCorrector
include RangeHelp

MSG = 'Precede `%<method>s` with a `@!method` YARD directive.'
MSG = 'Precede `%{method}` with a `@!method` YARD directive.'
MSG_WRONG_NAME = '`@!method` YARD directive has invalid method name, ' \
'use `%<expected>s` instead of `%<actual>s`.'
'use `%{expected}` instead of `%{actual}`.'
MSG_TOO_MANY = 'Multiple `@!method` YARD directives found for this matcher.'

RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].to_set.freeze
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/node_type_predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module InternalAffairs
class NodeTypePredicate < Base
extend AutoCorrector

MSG = 'Use `#%<type>s_type?` to check node type.'
MSG = 'Use `#%{type}_type?` to check node type.'
RESTRICT_ON_SEND = %i[==].freeze

# @!method node_type_check(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module InternalAffairs
class OffenseLocationKeyword < Base
extend AutoCorrector

MSG = 'Use `:%<keyword>s` as the location argument to `#add_offense`.'
MSG = 'Use `:%{keyword}` as the location argument to `#add_offense`.'
RESTRICT_ON_SEND = %i[add_offense].freeze

def on_send(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RedundantDescribedClassAsSubject < Base
include RangeHelp
extend AutoCorrector

MSG = 'Remove the redundant `subject`%<additional_message>s.'
MSG = 'Remove the redundant `subject`%{additional_message}.'

# @!method described_class_subject?(node)
def_node_matcher :described_class_subject?, <<~PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RedundantLetRuboCopConfigNew < Base
include RangeHelp
extend AutoCorrector

MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'
MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%{additional_message}.'

# @!method let_rubocop_config_new?(node)
def_node_matcher :let_rubocop_config_new?, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/single_line_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module InternalAffairs
class SingleLineComparison < Base
extend AutoCorrector

MSG = 'Use `%<preferred>s`.'
MSG = 'Use `%{preferred}`.'
RESTRICT_ON_SEND = %i[== !=].freeze

# @!method single_line_comparison(node)
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/internal_affairs/undefined_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class UndefinedConfig < Base
Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude
].freeze
RESTRICT_ON_SEND = %i[[] fetch].freeze
MSG = '`%<name>s` is not defined in the configuration for `%<cop>s` ' \
'in `config/default.yml`.'
MSG = '`%{name}` is not defined in the configuration for `%{cop}` in `config/default.yml`.'

# @!method cop_class_def(node)
def_node_search :cop_class_def, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/access_modifier_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AccessModifierIndentation < Base
include RangeHelp
extend AutoCorrector

MSG = '%<style>s access modifiers like `%<node>s`.'
MSG = '%{style} access modifiers like `%{node}`.'

def on_class(node)
return unless node.body&.begin_type?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/block_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class BlockAlignment < Base
include RangeHelp
extend AutoCorrector

MSG = '%<current>s is not aligned with %<prefer>s%<alt_prefer>s.'
MSG = '%{current} is not aligned with %{prefer}%{alt_prefer}.'

# @!method block_end_align_target?(node, child)
def_node_matcher :block_end_align_target?, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/block_end_newline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BlockEndNewline < Base
include Alignment
extend AutoCorrector

MSG = 'Expression at %<line>d, %<column>d should be on its own line.'
MSG = 'Expression at %{line}, %{column} should be on its own line.'

def on_block(node)
return if node.single_line?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/case_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CaseIndentation < Base
include RangeHelp
extend AutoCorrector

MSG = 'Indent `%<branch_type>s` %<depth>s `%<base>s`.'
MSG = 'Indent `%{branch_type}` %{depth} `%{base}`.'

def on_case(case_node)
return if case_node.single_line?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/class_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ClassStructure < Base
sclass: :class_singleton
}.freeze

MSG = '`%<category>s` is supposed to appear before `%<previous>s`.'
MSG = '`%{category}` is supposed to appear before `%{previous}`.'

# Validates code style on class declaration.
# Add offense when find a node out of expected order.
Expand Down
5 changes: 2 additions & 3 deletions lib/rubocop/cop/layout/closing_heredoc_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ class ClosingHeredocIndentation < Base
extend AutoCorrector

SIMPLE_HEREDOC = '<<'
MSG = '`%<closing>s` is not aligned with `%<opening>s`.'
MSG_ARG = '`%<closing>s` is not aligned with `%<opening>s` or ' \
'beginning of method definition.'
MSG = '`%{closing}` is not aligned with `%{opening}`.'
MSG_ARG = '`%{closing}` is not aligned with `%{opening}` or beginning of method definition.'

def on_heredoc(node)
return if heredoc_type(node) == SIMPLE_HEREDOC ||
Expand Down