Skip to content
Merged
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
13 changes: 13 additions & 0 deletions lib/rdoc/encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,21 @@ def self.read_file filename, encoding, force_transcode = false
nil
end

def self.remove_frozen_string_literal string
string =~ /\A(?:#!.*\n)?(.*\n)/
first_line = $1

if first_line =~ /\A# +frozen[-_]string[-_]literal[=:].+$/i
string.sub! first_line, ''
end
end

##
# Sets the encoding of +string+ based on the magic comment

def self.set_encoding string
remove_frozen_string_literal string

string =~ /\A(?:#!.*\n)?(.*\n)/

first_line = $1
Expand All @@ -90,6 +101,8 @@ def self.set_encoding string

string.sub! first_line, ''

remove_frozen_string_literal string

return unless Object.const_defined? :Encoding

enc = Encoding.find name
Expand Down
39 changes: 39 additions & 0 deletions test/test_rdoc_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,45 @@ def test_class_set_encoding_bad
end
end

def test_skip_frozen_string_literal
skip "Encoding not implemented" unless Object.const_defined? :Encoding

expected = "# frozen_string_literal: false\nhi everybody"

@tempfile.write expected
@tempfile.flush

contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
assert_equal "hi everybody", contents
assert_equal Encoding::UTF_8, contents.encoding
end

def test_skip_frozen_string_literal_after_coding
skip "Encoding not implemented" unless Object.const_defined? :Encoding

expected = "# coding: utf-8\n# frozen-string-literal: false\nhi everybody"

@tempfile.write expected
@tempfile.flush

contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
assert_equal "hi everybody", contents
assert_equal Encoding::UTF_8, contents.encoding
end

def test_skip_frozen_string_literal_before_coding
skip "Encoding not implemented" unless Object.const_defined? :Encoding

expected = "# frozen_string_literal: false\n# coding: utf-8\nhi everybody"

@tempfile.write expected
@tempfile.flush

contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8
assert_equal "hi everybody", contents
assert_equal Encoding::UTF_8, contents.encoding
end

def test_sanity
skip "Encoding not implemented" unless Object.const_defined? :Encoding

Expand Down