Avoid detecting encoding comments in documentation text#1727
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens RDoc::Encoding’s header parsing so encoding “magic comments” are only detected at the start of a file and only in recognized magic-comment forms, preventing documentation prose from being misinterpreted as an encoding directive during RDoc::RDoc#parse_file.
Changes:
- Anchor header detection to the beginning of the string (
\A) to avoid matching encoding directives mid-file. - Restrict the
coding:/encoding:branch to magic-comment forms (including Emacs-*- ... -*-style) rather than arbitrary comment prose containingcoding:later in the line. - Add a regression test ensuring documentation text mentioning
#encoding:/Encoding::.../ later# coding:is not treated as an encoding declaration.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/rdoc/encoding.rb |
Refines HEADER_REGEXP to match encoding directives only in the file header and only in magic-comment formats. |
test/rdoc/rdoc_encoding_test.rb |
Adds a regression test for documentation-like comment text that previously could be misdetected as an encoding directive. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
428bb8c to
b06087f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RDoc currently reads file content with
RDoc::Encoding.read_fileduringRDoc::RDoc#parse_file, before parser selection. The current header regexp starts with^and permits arbitrary comment prose beforecoding:, so documentation text can be interpreted as an encoding directive before the selected parser handles the file (current regexp).Ruby documents
^as the beginning of a line and\Aas the beginning of a string, and documents magic comments as top-level directives with plainencoding/codingsyntax or Emacs-style-*- ... -*-syntax (Regexp anchors, magic comments). This PR updates RDoc header detection to stay at the file header and limits thecoding:branch to those magic-comment forms (updated regexp).The regression test covers documentation text that mentions
#encoding: Returns,Encoding::US_ASCII.name, and a later prose# coding:line so those examples are treated as documentation text instead of encoding declarations (test fixture).