Skip to content

Commit 7976f0c

Browse files
committed
Use constant array
1 parent 770f141 commit 7976f0c

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

lib/sass/compiler/host/importer_registry.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def initialize(importers, load_paths, alert_color:)
2525
@highlight = alert_color
2626
end
2727

28+
IMPORTER_ATTRS = %i[non_canonical_scheme].freeze
29+
30+
IMPORTER_METHODS = %i[canonicalize load find_file_url].freeze
31+
32+
private_constant :IMPORTER_ATTRS, :IMPORTER_METHODS
33+
2834
def register(importer)
2935
if importer.is_a?(Sass::NodePackageImporter)
3036
EmbeddedProtocol::InboundMessage::CompileRequest::Importer.new(
@@ -33,8 +39,7 @@ def register(importer)
3339
)
3440
)
3541
else
36-
importer = Struct.from_hash(importer, attrs: %i[non_canonical_scheme],
37-
methods: %i[canonicalize load find_file_url])
42+
importer = Struct.new(importer, attrs: IMPORTER_ATTRS, methods: IMPORTER_METHODS) if importer.is_a?(::Hash)
3843

3944
is_importer = importer.respond_to?(:canonicalize) && importer.respond_to?(:load)
4045
is_file_importer = importer.respond_to?(:find_file_url)
@@ -80,10 +85,14 @@ def canonicalize(canonicalize_request)
8085
)
8186
end
8287

88+
IMPORTER_RESULT_ATTRS = %i[contents syntax source_map_url].freeze
89+
90+
private_constant :IMPORTER_RESULT_ATTRS
91+
8392
def import(import_request)
8493
importer = @importers_by_id[import_request.importer_id]
85-
importer_result = Struct.from_hash(importer.load(import_request.url),
86-
attrs: %i[contents syntax source_map_url])
94+
importer_result = importer.load(import_request.url)
95+
importer_result = Struct.new(importer_result, attrs: IMPORTER_RESULT_ATTRS) if importer_result.is_a?(::Hash)
8796

8897
EmbeddedProtocol::InboundMessage::ImportResponse.new(
8998
id: import_request.id,

lib/sass/compiler/host/logger_registry.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ class Host
77
#
88
# It stores logger and handles log events.
99
class LoggerRegistry
10+
LOGGER_METHODS = %i[warn debug].freeze
11+
12+
private_constant :LOGGER_METHODS
13+
1014
def initialize(logger)
11-
@logger = Struct.from_hash(logger, methods: %i[debug warn])
12-
@respond_to_debug = @logger.respond_to?(:debug)
13-
@respond_to_warn = @logger.respond_to?(:warn)
15+
@logger = logger.is_a?(::Hash) ? Struct.new(logger, methods: LOGGER_METHODS) : logger
16+
@logger_respond_to_debug = @logger.respond_to?(:debug)
17+
@logger_respond_to_warn = @logger.respond_to?(:warn)
1418
end
1519

1620
def log(event)
1721
case event.type
1822
when :DEBUG
19-
if @respond_to_debug
23+
if @logger_respond_to_debug
2024
@logger.debug(event.message, DebugContext.new(event))
2125
else
2226
Kernel.warn(event.formatted)
2327
end
2428
when :DEPRECATION_WARNING, :WARNING
25-
if @respond_to_warn
29+
if @logger_respond_to_warn
2630
@logger.warn(event.message, WarnContext.new(event))
2731
else
2832
Kernel.warn(event.formatted)

lib/sass/compiler/host/struct.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
module Sass
44
class Compiler
55
class Host
6-
# The {Struct} module.
6+
# The {Struct} class.
77
#
8-
# It creates {Struct}-like objects from {::Hash}.
8+
# It creates {::Struct}-like objects from {::Hash}.
99
class Struct
1010
def initialize(hash, attrs: nil, methods: nil)
1111
@hash = hash
@@ -28,16 +28,6 @@ def method_missing(symbol, ...)
2828
def respond_to_missing?(symbol, _include_all)
2929
@hash.key?(symbol) && (@attrs&.include?(symbol) || @methods&.include?(symbol))
3030
end
31-
32-
class << self
33-
def from_hash(object, ...)
34-
if object.is_a?(Hash)
35-
new(object, ...)
36-
else
37-
object
38-
end
39-
end
40-
end
4131
end
4232

4333
private_constant :Struct

lib/sass/value/number.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def initialize(value, unit = nil)
2525
denominator_units = []
2626
when ::Hash
2727
numerator_units = unit.fetch(:numerator_units, [])
28-
unless numerator_units.is_a?(Array)
28+
unless numerator_units.is_a?(::Array)
2929
raise Sass::ScriptError, "invalid numerator_units #{numerator_units.inspect}"
3030
end
3131

3232
denominator_units = unit.fetch(:denominator_units, [])
33-
unless denominator_units.is_a?(Array)
33+
unless denominator_units.is_a?(::Array)
3434
raise Sass::ScriptError, "invalid denominator_units #{denominator_units.inspect}"
3535
end
3636
else

0 commit comments

Comments
 (0)