Skip to content
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
2 changes: 1 addition & 1 deletion lib/lightningcss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.bundle(path, **options)

#: (String, ?filename: String?, ?minify: bool, ?error_recovery: bool, ?targets: browsers?) -> LightningCSS::Result
def self.transform_style_attribute(code, **options)
serialized = Options.serialize(options, allowed: Options::STYLE_ATTRIBUTE, subject: "a style attribute")
serialized = Options.serialize(options, Options::STYLE_ATTRIBUTE, "a style attribute")

Result.from_json(Backend.transform_style_attribute(code.to_s, serialized))
end
Expand Down
10 changes: 5 additions & 5 deletions lib/lightningcss/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class Options

attr_reader :to_h #: Hash[Symbol, untyped]

#: (Hash[Symbol, untyped], ?allowed: Array[Symbol], ?subject: String) -> String
def self.serialize(options, allowed: KNOWN, subject: "a transform")
new(allowed: allowed, subject: subject, **options).to_json
#: (Hash[Symbol, untyped], ?Array[Symbol], ?String) -> String
def self.serialize(options, allowed = KNOWN, subject = "a transform")
new(options, allowed, subject).to_json
end

#: (?allowed: Array[Symbol], ?subject: String, **untyped) -> void
def initialize(allowed: KNOWN, subject: "a transform", **options)
#: (Hash[Symbol, untyped], ?Array[Symbol], ?String) -> void
def initialize(options, allowed = KNOWN, subject = "a transform")
given = options.transform_keys(&:to_sym)

validate!(given.keys, allowed, subject)
Expand Down
8 changes: 4 additions & 4 deletions sig/lightningcss/options.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ module LightningCSS

attr_reader to_h: Hash[Symbol, untyped]

# : (Hash[Symbol, untyped], ?allowed: Array[Symbol], ?subject: String) -> String
def self.serialize: (Hash[Symbol, untyped], ?allowed: Array[Symbol], ?subject: String) -> String
# : (Hash[Symbol, untyped], ?Array[Symbol], ?String) -> String
def self.serialize: (Hash[Symbol, untyped], ?Array[Symbol], ?String) -> String

# : (?allowed: Array[Symbol], ?subject: String, **untyped) -> void
def initialize: (?allowed: Array[Symbol], ?subject: String, **untyped) -> void
# : (Hash[Symbol, untyped], ?Array[Symbol], ?String) -> void
def initialize: (Hash[Symbol, untyped], ?Array[Symbol], ?String) -> void

# : (?untyped) -> String
def to_json: (?untyped) -> String
Expand Down
34 changes: 20 additions & 14 deletions test/lightningcss/options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
module LightningCSS
class OptionsTest < Minitest::Spec
test "writes what it was given as JSON" do
assert_equal '{"minify":true}', Options.new(minify: true).to_json
assert_equal '{"minify":true}', Options.new({ minify: true }).to_json
end

test "writes nothing when it was given nothing" do
assert_equal "{}", Options.new.to_json
assert_equal "{}", Options.new({}).to_json
end

test "reads a string key as the option it names" do
assert_equal({ minify: true }, Options.new("minify" => true).to_h)
assert_equal({ minify: true }, Options.new({ "minify" => true }).to_h)
end

test "drops an option that was given as nil" do
assert_equal "{}", Options.new(scope: nil).to_json
assert_equal "{}", Options.new({ scope: nil }).to_json
end

test "reads css_modules true as the settings it already has" do
assert_equal({ css_modules: {} }, Options.new(css_modules: true).to_h)
assert_equal({ css_modules: {} }, Options.new({ css_modules: true }).to_h)
end

test "reads css_modules false as not at all" do
assert_equal({}, Options.new(css_modules: false).to_h)
assert_equal({}, Options.new({ css_modules: false }).to_h)
end

test "keeps css_modules settings as written" do
assert_equal({ css_modules: { pattern: "[local]" } }, Options.new(css_modules: { pattern: "[local]" }).to_h)
assert_equal({ css_modules: { pattern: "[local]" } }, Options.new({ css_modules: { pattern: "[local]" } }).to_h)
end

test "refuses an option nobody reads" do
error = assert_raises(OptionError) { Options.new(nonsense: true) }
error = assert_raises(OptionError) { Options.new({ nonsense: true }) }

assert_equal "Unknown option: nonsense", error.message
end

test "names every option nobody reads" do
error = assert_raises(OptionError) { Options.new(nope: 1, nah: 2) }
error = assert_raises(OptionError) { Options.new({ nope: 1, nah: 2 }) }

assert_equal "Unknown options: nope, nah", error.message
end
Expand All @@ -48,15 +48,21 @@ class OptionsTest < Minitest::Spec
assert_equal [:filename, :minify, :error_recovery, :targets, :css_modules, :scope], Options::KNOWN
end

test "takes what it is allowed to read as an argument, so no option name can collide with it" do
error = assert_raises(OptionError) { Options.new({ subject: "x" }) }

assert_equal "Unknown option: subject", error.message
end

test "keeps what it read to itself" do
options = Options.new(minify: true)
options = Options.new({ minify: true })

assert_predicate options, :frozen?
assert_predicate options.to_h, :frozen?
end

test "prints what it read" do
assert_equal "#<LightningCSS::Options {minify: true}>", Options.new(minify: true).inspect
assert_equal "#<LightningCSS::Options {minify: true}>", Options.new({ minify: true }).inspect
end

test "serializes in one step" do
Expand All @@ -69,23 +75,23 @@ class OptionsTest < Minitest::Spec

test "refuses an option that has nothing to act on" do
error = assert_raises(OptionError) do
Options.serialize({ scope: "[s]" }, allowed: Options::STYLE_ATTRIBUTE, subject: "a style attribute")
Options.serialize({ scope: "[s]" }, Options::STYLE_ATTRIBUTE, "a style attribute")
end

assert_equal "scope is not an option for a style attribute", error.message
end

test "names every option that has nothing to act on" do
error = assert_raises(OptionError) do
Options.serialize({ scope: "[s]", css_modules: true }, allowed: Options::STYLE_ATTRIBUTE, subject: "a style attribute")
Options.serialize({ scope: "[s]", css_modules: true }, Options::STYLE_ATTRIBUTE, "a style attribute")
end

assert_equal "scope, css_modules are not options for a style attribute", error.message
end

test "refuses an unknown option ahead of an unsupported one" do
error = assert_raises(OptionError) do
Options.serialize({ nonsense: 1, scope: "[s]" }, allowed: Options::STYLE_ATTRIBUTE, subject: "a style attribute")
Options.serialize({ nonsense: 1, scope: "[s]" }, Options::STYLE_ATTRIBUTE, "a style attribute")
end

assert_equal "Unknown option: nonsense", error.message
Expand Down