Skip to content

Use a refinement for Symbol#name instead of a polyfill #172

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 1 commit into from
Oct 17, 2022
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
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ AllCops:
Layout/LineLength:
Max: 80

Lint/AmbiguousBlockAssociation:
Enabled: false

Lint/DuplicateBranch:
Enabled: false

Expand Down
10 changes: 0 additions & 10 deletions lib/syntax_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@

require_relative "syntax_tree/parser"

# We rely on Symbol#name being available, which is only available in Ruby 3.0+.
# In case we're running on an older Ruby version, we polyfill it here.
unless :+.respond_to?(:name)
class Symbol # rubocop:disable Style/Documentation
def name
to_s.freeze
end
end
end

# Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
# provides the ability to generate a syntax tree from source, as well as the
# tools necessary to inspect and manipulate that syntax tree. It can be used to
Expand Down
14 changes: 14 additions & 0 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,20 @@ def format(q)
# array << value
#
class Binary < Node
# Since Binary's operator is a symbol, it's better to use the `name` method
# than to allocate a new string every time. This is a tiny performance
# optimization, but enough that it shows up in the profiler. Adding this in
# for older Ruby versions.
unless :+.respond_to?(:name)
using Module.new {
refine Symbol do
def name
to_s.freeze
end
end
}
end

# [untyped] the left-hand side of the expression
attr_reader :left

Expand Down