Class: RuboCop::Cop::Lint::MisplacedMagicComment
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/lint/misplaced_magic_comment.rb
Overview
Checks for magic comments placed where Ruby silently ignores them.
The encoding magic comment is only honored on the first line of a file,
or on the second line when the first line is a shebang. Anywhere else it
is silently ignored - even directly below another comment or a blank line.
Other magic comments (such as frozen_string_literal) are honored
anywhere before the first token of code, but are ignored after any code,
with a warning emitted only when running Ruby with -w.
A magic comment misplaced ahead of a shebang also renders the shebang ineffective, since a shebang is only recognized on the first line.
NOTE: An encoding comment that is only preceded by other magic
comments is not flagged by this cop; that case is handled by
Lint/OrderedMagicComments. shareable_constant_value is never
flagged, as Ruby intentionally allows it mid-file with block scoping.
Constant Summary collapse
- MSG_ENCODING =
'The `encoding` magic comment is ignored unless placed on the first ' \ 'line (or below a shebang on the first line).'
- MSG_AFTER_CODE =
'The `%<directive>s` magic comment is ignored after any code.'- MSG_ABOVE_SHEBANG =
'A magic comment above a shebang renders the shebang ineffective.'
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Constants inherited from Base
Base::DEPARTMENT_SEVERITIES, Base::RESTRICT_ON_SEND
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source, #project_index
Instance Method Summary collapse
Methods included from AutoCorrector
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #parser_engine, #preview?, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
cop_dir_for, #exclude_limit, read_limits
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?, #skipped_unsafe_correction_with_disable_uncorrectable?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#on_new_investigation ⇒ Object
66 67 68 69 70 71 |
# File 'lib/rubocop/cop/lint/misplaced_magic_comment.rb', line 66 def on_new_investigation return if processed_source.buffer.source.empty? check_magic_comment_above_shebang processed_source.comments.each { |comment| check_comment(comment) } end |