Skip to content

Commit

Permalink
Add support for SassCalculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Jul 20, 2023
1 parent e0d2757 commit a0a6931
Show file tree
Hide file tree
Showing 33 changed files with 884 additions and 55 deletions.
1 change: 1 addition & 0 deletions ext/sass/win32_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'fiddle'

# @!visibility private
module SassConfig
# @see https://learn.microsoft.com/en-us/windows/win32/api/
module Win32API
Expand Down
17 changes: 17 additions & 0 deletions lib/sass/calculation_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Sass
# The type of values that can be arguments to a SassCalculation.
#
# @see https://sass-lang.com/documentation/js-api/types/calculationvalue/
module CalculationValue
# @return [CalculationValue]
# @raise [ScriptError]
def assert_calculation_value(_name = nil)
self
end
end
end

require_relative 'calculation_value/calculation_interpolation'
require_relative 'calculation_value/calculation_operation'
31 changes: 31 additions & 0 deletions lib/sass/calculation_value/calculation_interpolation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Sass
module CalculationValue
# A string injected into a SassCalculation using interpolation.
#
# @see https://sass-lang.com/documentation/js-api/classes/calculationinterpolation/
class CalculationInterpolation
include CalculationValue

# @param value [::String]
def initialize(value)
@value = value
end

# @return [::String]
attr_reader :value

# @return [::Boolean]
def ==(other)
other.is_a?(Sass::CalculationValue::CalculationInterpolation) &&
other.value == value
end

# @return [Integer]
def hash
@hash ||= value.hash
end
end
end
end
52 changes: 52 additions & 0 deletions lib/sass/calculation_value/calculation_operation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module Sass
module CalculationValue
# A binary operation that can appear in a SassCalculation.
#
# @see https://sass-lang.com/documentation/js-api/classes/calculationoperation/
class CalculationOperation
include CalculationValue

OPERATORS = ['+', '-', '*', '/'].freeze

private_constant :OPERATORS

# @param operator [::String]
# @param left [CalculationValue]
# @param right [CalculationValue]
def initialize(operator, left, right)
raise Sass::ScriptError, "Invalid operator: #{operator}" unless OPERATORS.include?(operator)

left.assert_calculation_value
right.assert_calculation_value

@operator = operator.freeze
@left = left.freeze
@right = right.freeze
end

# @return [::String]
attr_reader :operator

# @return [CalculationValue]
attr_reader :left

# @return [CalculationValue]
attr_reader :right

# @return [::Boolean]
def ==(other)
other.is_a?(Sass::CalculationValue::CalculationOperation) &&
other.operator == operator &&
other.left == left &&
other.right == right
end

# @return [Integer]
def hash
@hash ||= [operator, left, right].hash
end
end
end
end
2 changes: 1 addition & 1 deletion lib/sass/compile_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Sass
# The result of compiling Sass to CSS. Returned by {Sass.compile} and {Sass.compile_string}.
#
# @see https://sass-lang.com/documentation/js-api/interfaces/CompileResult
# @see https://sass-lang.com/documentation/js-api/interfaces/compileresult/
class CompileResult
# @return [String]
attr_reader :css
Expand Down
14 changes: 7 additions & 7 deletions lib/sass/embedded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def initialize
# Compiles the Sass file at +path+ to CSS.
# @param path [String]
# @param load_paths [Array<String>] Paths in which to look for stylesheets loaded by rules like
# {@use}[https://sass-lang.com/documentation/at-rules/use] and {@import}[https://sass-lang.com/documentation/at-rules/import].
# {@use}[https://sass-lang.com/documentation/at-rules/use/] and {@import}[https://sass-lang.com/documentation/at-rules/import/].
# @param charset [Boolean] By default, if the CSS document contains non-ASCII characters, Sass adds a +@charset+
# declaration (in expanded output mode) or a byte-order mark (in compressed mode) to indicate its encoding to
# browsers or other consumers. If +charset+ is +false+, these annotations are omitted.
Expand All @@ -104,7 +104,7 @@ def initialize
# @param style [String, Symbol] The OutputStyle of the compiled CSS.
# @param functions [Hash<String, Proc>] Additional built-in Sass functions that are available in all stylesheets.
# @param importers [Array<Object>] Custom importers that control how Sass resolves loads from rules like
# {@use}[https://sass-lang.com/documentation/at-rules/use] and {@import}[https://sass-lang.com/documentation/at-rules/import].
# {@use}[https://sass-lang.com/documentation/at-rules/use/] and {@import}[https://sass-lang.com/documentation/at-rules/import/].
# @param alert_ascii [Boolean] If this is +true+, the compiler will exclusively use ASCII characters in its error
# and warning messages. Otherwise, it may use non-ASCII Unicode characters as well.
# @param alert_color [Boolean] If this is +true+, the compiler will use ANSI color escape codes in its error and
Expand All @@ -119,7 +119,7 @@ def initialize
# deprecation warning it encounters.
# @return [CompileResult]
# @raise [CompileError]
# @see https://sass-lang.com/documentation/js-api/modules#compile
# @see https://sass-lang.com/documentation/js-api/functions/compile/
def compile(path,
load_paths: [],

Expand Down Expand Up @@ -163,7 +163,7 @@ def compile(path,
# @param source [String]
# @param importer [Object] The importer to use to handle loads that are relative to the entrypoint stylesheet.
# @param load_paths [Array<String>] Paths in which to look for stylesheets loaded by rules like
# {@use}[https://sass-lang.com/documentation/at-rules/use] and {@import}[https://sass-lang.com/documentation/at-rules/import].
# {@use}[https://sass-lang.com/documentation/at-rules/use/] and {@import}[https://sass-lang.com/documentation/at-rules/import/].
# @param syntax [String, Symbol] The Syntax to use to parse the entrypoint stylesheet.
# @param url [String] The canonical URL of the entrypoint stylesheet. If this is passed along with +importer+, it's
# used to resolve relative loads in the entrypoint stylesheet.
Expand All @@ -175,7 +175,7 @@ def compile(path,
# @param style [String, Symbol] The OutputStyle of the compiled CSS.
# @param functions [Hash<String, Proc>] Additional built-in Sass functions that are available in all stylesheets.
# @param importers [Array<Object>] Custom importers that control how Sass resolves loads from rules like
# {@use}[https://sass-lang.com/documentation/at-rules/use] and {@import}[https://sass-lang.com/documentation/at-rules/import].
# {@use}[https://sass-lang.com/documentation/at-rules/use/] and {@import}[https://sass-lang.com/documentation/at-rules/import/].
# @param alert_ascii [Boolean] If this is +true+, the compiler will exclusively use ASCII characters in its error
# and warning messages. Otherwise, it may use non-ASCII Unicode characters as well.
# @param alert_color [Boolean] If this is +true+, the compiler will use ANSI color escape codes in its error and
Expand All @@ -190,7 +190,7 @@ def compile(path,
# deprecation warning it encounters.
# @return [CompileResult]
# @raise [CompileError]
# @see https://sass-lang.com/documentation/js-api/modules#compileString
# @see https://sass-lang.com/documentation/js-api/functions/compilestring/
def compile_string(source,
importer: nil,
load_paths: [],
Expand Down Expand Up @@ -234,7 +234,7 @@ def compile_string(source,
end

# @return [String] Information about the Sass implementation.
# @see https://sass-lang.com/documentation/js-api/modules#info
# @see https://sass-lang.com/documentation/js-api/variables/info/
def info
@info ||= Host.new(@dispatcher).version_request
end
Expand Down
8 changes: 7 additions & 1 deletion lib/sass/embedded/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def version_response(message)
end

def error(message)
@error = message
if message.is_a?(EmbeddedProtocol::ProtocolError)
return if message.id != id

@error = Errno::EPROTO.new(message.message)
else
@error = message
end
@queue.close
end

Expand Down
Loading

0 comments on commit a0a6931

Please sign in to comment.