Skip to content

Commit

Permalink
Support first class mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Sep 29, 2023
1 parent 6478471 commit ad670fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/sass/embedded/host/value_protofier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def to_proto(obj)
)
)
end
when Sass::Value::Mixin
EmbeddedProtocol::Value.new(
compiler_mixin: EmbeddedProtocol::Value::CompilerMixin.new(
id: obj.id
)
)
when Sass::Value::Calculation
EmbeddedProtocol::Value.new(
calculation: Calculation.to_proto(obj)
Expand Down Expand Up @@ -176,6 +182,8 @@ def from_proto(proto)
Sass::Value::Function.new(obj.id)
when :host_function
raise Sass::ScriptError, 'The compiler may not send Value.host_function to host'
when :compiler_mixin
Sass::Value::Mixin.new(obj.id)
when :calculation
Calculation.from_proto(obj)
when :singleton
Expand Down
7 changes: 7 additions & 0 deletions lib/sass/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def assert_map(name = nil)
raise Sass::ScriptError.new("#{self} is not a map", name)
end

# @return [Mixin]
# @raise [ScriptError]
def assert_mixin(name = nil)
raise Sass::ScriptError.new("#{self} is not a mixin", name)
end

# @return [Number]
# @raise [ScriptError]
def assert_number(name = nil)
Expand Down Expand Up @@ -132,6 +138,7 @@ def to_a_length
require_relative 'value/function'
require_relative 'value/fuzzy_math'
require_relative 'value/map'
require_relative 'value/mixin'
require_relative 'value/null'
require_relative 'value/number'
require_relative 'value/string'
35 changes: 35 additions & 0 deletions lib/sass/value/mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Sass
module Value
# Sass's mixin type.
#
# @see https://sass-lang.com/documentation/js-api/classes/sassmixin/
class Mixin
include Value

# @!visibility private
def initialize(id)
@id = id
end

# @return [Integer]
attr_reader :id

# @return [::Boolean]
def ==(other)
other.is_a?(Sass::Value::Mixin) && other.id == id
end

# @return [Integer]
def hash
@hash ||= id.hash
end

# @return [Mixin]
def assert_mixin(_name = nil)
self
end
end
end
end

0 comments on commit ad670fa

Please sign in to comment.