Skip to content

Commit 7f66f34

Browse files
authored
Merge pull request #580 from aycabta/fix-method-line-no
Fix method line number
2 parents 071e2ec + 8b3b256 commit 7f66f34

File tree

4 files changed

+49
-91
lines changed

4 files changed

+49
-91
lines changed

lib/rdoc/encoding.rb

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
module RDoc::Encoding
99

10+
HEADER_REGEXP = /^
11+
(?:
12+
\A\#!.*\n
13+
|
14+
^\#\s+frozen[-_]string[-_]literal[=:].+\n
15+
|
16+
^\#[^\n]+\b(?:en)?coding[=:]\s*(?<name>[^\s;]+).*\n
17+
|
18+
<\?xml[^?]*encoding=(?<quote>["'])(?<name>.*?)\k<quote>.*\n
19+
)+
20+
/xi # :nodoc:
21+
1022
##
1123
# Reads the contents of +filename+ and handles any encoding directives in
1224
# the file.
@@ -23,7 +35,8 @@ def self.read_file filename, encoding, force_transcode = false
2335

2436
utf8 = content.sub!(/\A\xef\xbb\xbf/, '')
2537

26-
content = RDoc::Encoding.set_encoding content
38+
enc = RDoc::Encoding.detect_encoding content
39+
content = RDoc::Encoding.change_encoding content, enc if enc
2740

2841
begin
2942
encoding ||= Encoding.default_external
@@ -85,29 +98,22 @@ def self.remove_frozen_string_literal string
8598
end
8699

87100
##
88-
# Sets the encoding of +string+ based on the magic comment
89-
90-
def self.set_encoding string
91-
string = remove_frozen_string_literal string
92-
93-
string =~ /\A(?:#!.*\n)?(.*\n)/
94-
95-
first_line = $1
101+
# Detects the encoding of +string+ based on the magic comment
96102

97-
name = case first_line
98-
when /^<\?xml[^?]*encoding=(["'])(.*?)\1/ then $2
99-
when /\b(?:en)?coding[=:]\s*([^\s;]+)/i then $1
100-
else return string
101-
end
103+
def self.detect_encoding string
104+
result = HEADER_REGEXP.match string
105+
name = result && result[:name]
102106

103-
string = string.sub first_line, ''
104-
105-
string = remove_frozen_string_literal string
107+
name ? Encoding.find(name) : nil
108+
end
106109

107-
enc = Encoding.find name
108-
string = RDoc::Encoding.change_encoding string, enc if enc
110+
##
111+
# Removes magic comments and shebang
109112

110-
string
113+
def self.remove_magic_comment string
114+
string.sub HEADER_REGEXP do |s|
115+
s.gsub(/[^\n]/, '')
116+
end
111117
end
112118

113119
##

lib/rdoc/markup/pre_process.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def include_file name, indent, encoding
266266
end
267267

268268
content = RDoc::Encoding.read_file full_name, encoding, true
269+
content = RDoc::Encoding.remove_magic_comment content
269270

270271
# strip magic comment
271272
content = content.sub(/\A# .*coding[=:].*$/, '').lstrip

lib/rdoc/parser/ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def initialize(top_level, file_name, content, options, stats)
177177

178178
@size = 0
179179
@token_listeners = nil
180+
content = RDoc::Encoding.remove_magic_comment content
180181
@scanner = RDoc::RipperStateLex.parse(content)
181182
@content = content
182183
@scanner_point = 0

test/test_rdoc_encoding.rb

Lines changed: 21 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_class_read_file_encoding
3131
@tempfile.flush
3232

3333
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
34-
assert_equal "hi everybody", contents
34+
assert_equal "# coding: utf-8\nhi everybody", contents
3535
assert_equal Encoding::UTF_8, contents.encoding
3636
end
3737

@@ -45,7 +45,7 @@ def test_class_read_file_encoding_convert
4545

4646
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
4747
assert_equal Encoding::UTF_8, contents.encoding
48-
assert_equal "hi \u00e9verybody", contents.sub("\r", '')
48+
assert_equal "# coding: ISO-8859-1\nhi \u00e9verybody", contents.sub("\r", '')
4949
end
5050

5151
def test_class_read_file_encoding_fail
@@ -71,7 +71,7 @@ def test_class_read_file_encoding_fancy
7171
@tempfile.flush
7272

7373
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
74-
assert_equal "hi everybody", contents
74+
assert_equal "# -*- coding: utf-8; fill-column: 74 -*-\nhi everybody", contents
7575
assert_equal Encoding::UTF_8, contents.encoding
7676
end
7777

@@ -81,7 +81,7 @@ def test_class_read_file_encoding_force_transcode
8181

8282
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::US_ASCII, true
8383

84-
assert_equal '?', contents
84+
assert_equal "# coding: utf-8\n?", contents
8585
assert_equal Encoding::US_ASCII, contents.encoding
8686
end
8787

@@ -124,108 +124,58 @@ def test_class_read_file_encoding_iso_2022_jp
124124

125125
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
126126

127-
expected = ":\xe3\x82\xb3\xe3\x83\x9e\xe3\x83\xb3\xe3\x83\x89:"
127+
expected = "# coding: ISO-2022-JP\n:\xe3\x82\xb3\xe3\x83\x9e\xe3\x83\xb3\xe3\x83\x89:"
128128
expected = RDoc::Encoding.change_encoding expected, Encoding::UTF_8
129129

130130
assert_equal expected, contents
131131
assert_equal Encoding::UTF_8, contents.encoding
132132
end
133133

134-
def test_class_set_encoding
134+
def test_class_detect_encoding
135135
s = "# coding: UTF-8\n"
136-
s = RDoc::Encoding.set_encoding s
136+
encoding = RDoc::Encoding.detect_encoding s
137137

138138
# sanity check for 1.8
139139

140-
assert_equal Encoding::UTF_8, s.encoding
140+
assert_equal Encoding::UTF_8, encoding
141141

142142
s = "#!/bin/ruby\n# coding: UTF-8\n"
143-
s = RDoc::Encoding.set_encoding s
143+
encoding = RDoc::Encoding.detect_encoding s
144144

145-
assert_equal Encoding::UTF_8, s.encoding
145+
assert_equal Encoding::UTF_8, encoding
146146

147147
s = "<?xml version='1.0' encoding='UTF-8'?>\n"
148-
s = RDoc::Encoding.set_encoding s
148+
encoding = RDoc::Encoding.detect_encoding s
149149

150-
assert_equal Encoding::UTF_8, s.encoding
150+
assert_equal Encoding::UTF_8, encoding
151151

152152
s = "<?xml version='1.0' encoding=\"UTF-8\"?>\n"
153-
s = RDoc::Encoding.set_encoding s
153+
encoding = RDoc::Encoding.detect_encoding s
154154

155-
assert_equal Encoding::UTF_8, s.encoding
156-
end
157-
158-
def test_class_set_encoding_strip
159-
s = "# coding: UTF-8\n# more comments"
160-
161-
s = RDoc::Encoding.set_encoding s
162-
163-
assert_equal "# more comments", s
164-
165-
s = "#!/bin/ruby\n# coding: UTF-8\n# more comments"
166-
167-
s = RDoc::Encoding.set_encoding s
168-
169-
assert_equal "#!/bin/ruby\n# more comments", s
155+
assert_equal Encoding::UTF_8, encoding
170156
end
171157

172158
def test_class_set_encoding_bad
173159
s = ""
174-
expected = s.encoding
175-
s = RDoc::Encoding.set_encoding s
160+
encoding = RDoc::Encoding.detect_encoding s
176161

177-
assert_equal expected, s.encoding
162+
assert_equal nil, encoding
178163

179164
s = "# vim:set fileencoding=utf-8:\n"
180-
expected = s.encoding
181-
s = RDoc::Encoding.set_encoding s
165+
encoding = RDoc::Encoding.detect_encoding s
182166

183-
assert_equal expected, s.encoding
167+
assert_equal nil, encoding
184168

185169
s = "# vim:set fileencoding=utf-8:\n"
186-
expected = s.encoding
187-
s = RDoc::Encoding.set_encoding s
170+
encoding = RDoc::Encoding.detect_encoding s
188171

189-
assert_equal expected, s.encoding
172+
assert_equal nil, encoding
190173

191174
assert_raises ArgumentError do
192-
s = RDoc::Encoding.set_encoding "# -*- encoding: undecided -*-\n"
175+
s = RDoc::Encoding.detect_encoding "# -*- encoding: undecided -*-\n"
193176
end
194177
end
195178

196-
def test_skip_frozen_string_literal
197-
expected = "# frozen_string_literal: true\nhi everybody"
198-
199-
@tempfile.write expected
200-
@tempfile.flush
201-
202-
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
203-
assert_equal "hi everybody", contents
204-
assert_equal Encoding::UTF_8, contents.encoding
205-
end
206-
207-
def test_skip_frozen_string_literal_after_coding
208-
expected = "# coding: utf-8\n# frozen-string-literal: false\nhi everybody"
209-
210-
@tempfile.write expected
211-
@tempfile.flush
212-
213-
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
214-
assert_equal "hi everybody", contents
215-
assert_equal Encoding::UTF_8, contents.encoding
216-
end
217-
218-
def test_skip_frozen_string_literal_before_coding
219-
expected = "# frozen_string_literal: true\n# coding: utf-8\nhi everybody"
220-
221-
@tempfile.write expected
222-
@tempfile.flush
223-
224-
contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
225-
assert_equal "hi everybody", contents
226-
assert_equal Encoding::UTF_8, contents.encoding
227-
end
228-
229179
def test_sanity
230180
assert_equal Encoding::US_ASCII, ''.encoding,
231181
'If this file is not ASCII tests may incorrectly pass'

0 commit comments

Comments
 (0)