Skip to content

Use frozen_string_literal: true #551

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

Merged
merged 4 commits into from
Nov 27, 2017
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
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ parsed_files = PARSER_FILES.map do |parser_file|
racc = Gem.bin_path 'racc', 'racc'
rb_file = parser_file.gsub(/\.ry\z/, ".rb")
ruby "#{racc} -l -o #{rb_file} #{parser_file}"
open(rb_file, 'r+') do |f|
newtext = "# frozen_string_literal: true\n#{f.read}"
f.rewind
f.write newtext
end
elsif parser_file =~ /\.kpeg\z/ # need kpeg
kpeg = Gem.bin_path 'kpeg', 'kpeg'
rb_file = parser_file.gsub(/\.kpeg\z/, ".rb")
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
$DEBUG_RDOC = nil

# :main: README.rdoc
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/alias.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# Represent an alias, which is an old_name/new_name pair associated with a
# particular context
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/anon_class.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# An anonymous class like:
#
Expand Down
10 changes: 5 additions & 5 deletions lib/rdoc/any_method.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# AnyMethod is the base class for objects representing methods

Expand Down Expand Up @@ -244,9 +244,9 @@ def param_list
if @block_params then
# If this method has explicit block parameters, remove any explicit
# &block
params.sub!(/,?\s*&\w+/, '')
params = params.sub(/,?\s*&\w+/, '')
else
params.sub!(/\&(\w+)/, '\1')
params = params.sub(/\&(\w+)/, '\1')
end

params = params.gsub(/\s+/, '').split(',').reject(&:empty?)
Expand Down Expand Up @@ -274,11 +274,11 @@ def param_seq
if @block_params then
# If this method has explicit block parameters, remove any explicit
# &block
params.sub!(/,?\s*&\w+/, '')
params = params.sub(/,?\s*&\w+/, '')

block = @block_params.tr_s("\n ", " ")
if block[0] == ?(
block.sub!(/^\(/, '').sub!(/\)/, '')
block = block.sub(/^\(/, '').sub(/\)/, '')
end
params << " { |#{block}| ... }"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/attr.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# An attribute created by \#attr, \#attr_reader, \#attr_writer or
# \#attr_accessor
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/class_module.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# ClassModule is the base class for objects representing either a class or a
# module.
Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/code_object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# Base class for the RDoc code tree.
#
Expand Down Expand Up @@ -144,7 +144,7 @@ def comment=(comment)
# HACK correct fix is to have #initialize create @comment
# with the correct encoding
if String === @comment and @comment.empty? then
@comment.force_encoding comment.encoding
@comment = RDoc::Encoding.change_encoding @comment, comment.encoding
end
@comment
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/code_objects.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
# This file was used to load all the RDoc::CodeObject subclasses at once. Now
# autoload handles this.

Expand Down
30 changes: 20 additions & 10 deletions lib/rdoc/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# A comment holds the text comment for a RDoc::CodeObject and provides a
# unified way of cleaning it up and parsing it into an RDoc::Markup::Document.
Expand Down Expand Up @@ -45,7 +45,7 @@ class RDoc::Comment

def initialize text = nil, location = nil
@location = location
@text = text
@text = text.nil? ? nil : text.dup

@document = nil
@format = 'rdoc'
Expand Down Expand Up @@ -114,10 +114,14 @@ def extract_call_seq method

method.call_seq = seq.chomp

elsif @text.sub!(/^\s*:?call-seq:(.*?)(^\s*$|\z)/m, '') then
seq = $1
seq.gsub!(/^\s*/, '')
method.call_seq = seq
else
regexp = /^\s*:?call-seq:(.*?)(^\s*$|\z)/m
if regexp =~ @text then
@text = @text.sub(regexp, '')
seq = $1
seq.gsub!(/^\s*/, '')
method.call_seq = seq
end
end

method
Expand All @@ -133,8 +137,14 @@ def empty?
##
# HACK dubious

def force_encoding encoding
@text.force_encoding encoding
def encode! encoding
# TODO: Remove this condition after Ruby 2.2 EOL
if RUBY_VERSION < '2.3.0'
@text = @text.force_encoding encoding
else
@text = String.new @text, encoding: encoding
end
self
end

##
Expand Down Expand Up @@ -200,7 +210,7 @@ def parse
def remove_private
# Workaround for gsub encoding for Ruby 1.9.2 and earlier
empty = ''
empty.force_encoding @text.encoding
empty = RDoc::Encoding.change_encoding empty, @text.encoding

@text = @text.gsub(%r%^\s*([#*]?)--.*?^\s*(\1)\+\+\n?%m, empty)
@text = @text.sub(%r%^\s*[#*]?--.*%m, '')
Expand All @@ -216,7 +226,7 @@ def text= text
@text.nil? and @document

@document = nil
@text = text
@text = text.nil? ? nil : text.dup
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/constant.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# A constant

Expand Down
6 changes: 3 additions & 3 deletions lib/rdoc/context.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
require 'cgi'

##
Expand Down Expand Up @@ -239,7 +239,7 @@ def add_attribute attribute

if known then
known.comment = attribute.comment if known.comment.empty?
elsif registered = @methods_hash[attribute.pretty_name << '='] and
elsif registered = @methods_hash[attribute.pretty_name + '='] and
RDoc::Attr === registered then
registered.rw = 'RW'
else
Expand All @@ -249,7 +249,7 @@ def add_attribute attribute
end

if attribute.rw.index 'W' then
key = attribute.pretty_name << '='
key = attribute.pretty_name + '='
known = @methods_hash[key]

if known then
Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/context/section.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# A section of documentation like:
#
Expand Down Expand Up @@ -43,7 +43,7 @@ def initialize parent, title, comment
@parent = parent
@title = title ? title.strip : title

@@sequence.succ!
@@sequence = @@sequence.succ
@sequence = @@sequence.dup

@comments = []
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/cross_reference.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# RDoc::CrossReference is a reusable way to create cross references for names.

Expand Down
58 changes: 40 additions & 18 deletions lib/rdoc/encoding.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: US-ASCII
# frozen_string_literal: false
# frozen_string_literal: true

##
# This class is a wrapper around File IO and Encoding that helps RDoc load
Expand All @@ -23,26 +23,26 @@ def self.read_file filename, encoding, force_transcode = false

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

RDoc::Encoding.set_encoding content
content = RDoc::Encoding.set_encoding content

begin
encoding ||= Encoding.default_external
orig_encoding = content.encoding

if not orig_encoding.ascii_compatible? then
content.encode! encoding
content = content.encode encoding
elsif utf8 then
content.force_encoding Encoding::UTF_8
content.encode! encoding
content = RDoc::Encoding.change_encoding content, Encoding::UTF_8
content = content.encode encoding
else
# assume the content is in our output encoding
content.force_encoding encoding
content = RDoc::Encoding.change_encoding content, encoding
end

unless content.valid_encoding? then
# revert and try to transcode
content.force_encoding orig_encoding
content.encode! encoding
content = RDoc::Encoding.change_encoding content, orig_encoding
content = content.encode encoding
end

unless content.valid_encoding? then
Expand All @@ -52,10 +52,11 @@ def self.read_file filename, encoding, force_transcode = false
rescue Encoding::InvalidByteSequenceError,
Encoding::UndefinedConversionError => e
if force_transcode then
content.force_encoding orig_encoding
content.encode!(encoding,
:invalid => :replace, :undef => :replace,
:replace => '?')
content = RDoc::Encoding.change_encoding content, orig_encoding
content = content.encode(encoding,
:invalid => :replace,
:undef => :replace,
:replace => '?')
return content
else
warn "unable to convert #{e.message} for #{filename}, skipping"
Expand All @@ -77,15 +78,17 @@ def self.remove_frozen_string_literal string
first_line = $1

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

string
end

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

def self.set_encoding string
remove_frozen_string_literal string
string = remove_frozen_string_literal string

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

Expand All @@ -94,15 +97,34 @@ def self.set_encoding string
name = case first_line
when /^<\?xml[^?]*encoding=(["'])(.*?)\1/ then $2
when /\b(?:en)?coding[=:]\s*([^\s;]+)/i then $1
else return
else return string
end

string.sub! first_line, ''
string = string.sub first_line, ''

remove_frozen_string_literal string
string = remove_frozen_string_literal string

enc = Encoding.find name
string.force_encoding enc if enc
string = RDoc::Encoding.change_encoding string, enc if enc

string
end

##
# Changes encoding based on +encoding+ without converting and returns new
# string

def self.change_encoding text, encoding
if text.kind_of? RDoc::Comment
text.encode! encoding
else
# TODO: Remove this condition after Ruby 2.2 EOL
if RUBY_VERSION < '2.3.0'
text.force_encoding encoding
else
String.new text, encoding: encoding
end
end
end

end
2 changes: 1 addition & 1 deletion lib/rdoc/erb_partial.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# Allows an ERB template to be rendered in the context (binding) of an
# existing ERB template evaluation.
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/erbio.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
require 'erb'

##
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/extend.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# A Module extension to a class with \#extend
#
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# RDoc uses generators to turn parsed source code in the form of an
# RDoc::CodeObject tree into some form of output. RDoc comes with the HTML
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
# -*- mode: ruby; ruby-indent-level: 2; tab-width: 2 -*-

require 'erb'
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/json_index.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
require 'json'
begin
require 'zlib'
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/markup.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# Handle common RDoc::Markup tasks for various CodeObjects
#
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/pot.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# Generates a POT file.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/pot/message_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
##
# Extracts message from RDoc::Store

Expand Down
Loading