Skip to content
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

Refactor symbol quoting into Symbol.quote_for_named_argument #13595

Merged
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
6 changes: 1 addition & 5 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1048,11 +1048,7 @@ module Crystal

private def to_double_splat(trailing_string = "")
MacroId.new(entries.join(", ") do |entry|
if Symbol.needs_quotes_for_named_argument?(entry.key)
"#{entry.key.inspect}: #{entry.value}"
else
"#{entry.key}: #{entry.value}"
end
"#{Symbol.quote_for_named_argument(entry.key)}: #{entry.value}"
end + trailing_string)
end
end
Expand Down
12 changes: 2 additions & 10 deletions src/compiler/crystal/semantic/call_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,7 @@ class Crystal::Call
next if arg_type.entries.empty?
io << ", " unless first
arg_type.entries.join(io, ", ") do |entry|
if Symbol.needs_quotes_for_named_argument?(entry.name)
entry.name.inspect(io)
else
io << entry.name
end
Symbol.quote_for_named_argument(io, entry.name)
io << ": " << entry.type
end
else
Expand All @@ -1049,11 +1045,7 @@ class Crystal::Call
if named_args = @named_args
io << ", " unless first
named_args.join(io, ", ") do |named_arg|
if Symbol.needs_quotes_for_named_argument?(named_arg.name)
named_arg.name.inspect(io)
else
io << named_arg.name
end
Symbol.quote_for_named_argument(io, named_arg.name)
io << ": " << named_arg.value.type
end
end
Expand Down
12 changes: 2 additions & 10 deletions src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,7 @@ module Crystal
end

def visit_named_arg_name(name)
if Symbol.needs_quotes_for_named_argument?(name)
name.inspect(@str)
else
@str << name
end
Symbol.quote_for_named_argument(@str, name)
end

def visit(node : Underscore)
Expand Down Expand Up @@ -1123,11 +1119,7 @@ module Crystal
else
@str << node.name
@str << " = "
if Symbol.needs_quotes_for_named_argument?(node.real_name)
node.real_name.inspect(@str)
else
@str << node.real_name
end
Symbol.quote_for_named_argument(@str, node.real_name)
end
if node.args.size > 0
@str << '('
Expand Down
11 changes: 4 additions & 7 deletions src/compiler/crystal/tools/doc/macro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,11 @@ class Crystal::Doc::Macro
def arg_to_html(arg : Arg, io, html : HTMLOption = :all)
if arg.external_name != arg.name
if name = arg.external_name.presence
if Symbol.needs_quotes_for_named_argument? name
if html.none?
name.inspect io
else
HTML.escape name.inspect, io
end
else
name = Symbol.quote_for_named_argument(name)
if html.none?
io << name
else
HTML.escape name, io
end
else
io << "_"
Expand Down
11 changes: 4 additions & 7 deletions src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,11 @@ class Crystal::Doc::Method
def arg_to_html(arg : Arg, io, html : HTMLOption = :all)
if arg.external_name != arg.name
if name = arg.external_name.presence
if Symbol.needs_quotes_for_named_argument? name
if html.none?
name.inspect io
else
HTML.escape name.inspect, io
end
else
name = Symbol.quote_for_named_argument(name)
if html.none?
io << name
else
HTML.escape name, io
end
else
io << "_"
Expand Down
12 changes: 2 additions & 10 deletions src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,7 @@ class Crystal::Doc::Type
if (named_args = node.named_args) && !named_args.empty?
io << ", " unless node.type_vars.empty?
named_args.join(io, ", ") do |entry|
if Symbol.needs_quotes_for_named_argument?(entry.name)
entry.name.inspect(io)
else
io << entry.name
end
Symbol.quote_for_named_argument(io, entry.name)
io << ": "
node_to_html entry.value, io, html: html
end
Expand Down Expand Up @@ -636,11 +632,7 @@ class Crystal::Doc::Type
def type_to_html(type : Crystal::NamedTupleInstanceType, io, text = nil, html : HTMLOption = :all)
io << '{'
type.entries.join(io, ", ") do |entry|
if Symbol.needs_quotes_for_named_argument?(entry.name)
entry.name.inspect(io)
else
io << entry.name
end
Symbol.quote_for_named_argument(io, entry.name)
io << ": "
type_to_html entry.type, io, html: html
end
Expand Down
6 changes: 1 addition & 5 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2628,11 +2628,7 @@ module Crystal
def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen : Bool = false) : Nil
io << "NamedTuple("
@entries.join(io, ", ") do |entry|
if Symbol.needs_quotes_for_named_argument?(entry.name)
entry.name.inspect(io)
else
io << entry.name
end
Symbol.quote_for_named_argument(io, entry.name)
io << ": "
entry_type = entry.type
entry_type = entry_type.devirtualize unless codegen
Expand Down
14 changes: 2 additions & 12 deletions src/named_tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,7 @@ struct NamedTuple
{% if i > 0 %}
io << ", "
{% end %}
key = {{key.stringify}}
if Symbol.needs_quotes_for_named_argument?(key)
key.inspect(io)
else
io << key
end
Symbol.quote_for_named_argument io, {{key.stringify}}
io << ": "
self[{{key.symbolize}}].inspect(io)
{% end %}
Expand All @@ -468,12 +463,7 @@ struct NamedTuple
pp.comma
{% end %}
pp.group do
key = {{key.stringify}}
if Symbol.needs_quotes_for_named_argument?(key)
pp.text key.inspect
else
pp.text key
end
pp.text Symbol.quote_for_named_argument({{key.stringify}})
pp.text ": "
pp.nest do
pp.breakable ""
Expand Down
20 changes: 20 additions & 0 deletions src/symbol.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ struct Symbol
end
end

# :nodoc:
# Appends *string* to *io* and quotes it if necessary.
def self.quote_for_named_argument(io : IO, string : String) : Nil
if needs_quotes_for_named_argument?(string)
string.inspect(io)
else
io << string
end
end

# :nodoc:
# Returns *string* and quotes it if necessary.
def self.quote_for_named_argument(string : String) : String
if needs_quotes_for_named_argument?(string)
string.inspect
else
string
end
end

def clone
self
end
Expand Down