Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Add line numbers and update tests #43

Closed
wants to merge 1 commit into from
Closed
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/csscss/parser/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Transformer < Parslet::Transform
selector: simple(:selector),
properties: sequence(:properties)
}) {
Ruleset.new(Selector.from_parser(selector), properties.compact)
Ruleset.new(Selector.from_parser(selector), properties.compact, Metadata.from_parser(selector))
}

rule({
Expand Down
8 changes: 7 additions & 1 deletion lib/csscss/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def inspect
end
end

class Metadata < Struct.new(:line_number)
def self.from_parser(selectors)
new(selectors.line_and_column[0])
end
end

class Selector < Struct.new(:selectors)
def self.from_parser(selectors)
new(selectors.to_s.strip)
Expand All @@ -87,6 +93,6 @@ def inspect
end
end

class Ruleset < Struct.new(:selectors, :declarations)
class Ruleset < Struct.new(:selectors, :declarations, :metadata)
end
end
24 changes: 12 additions & 12 deletions test/csscss/parser/css_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ module Css
$

trans(css).must_equal([
rs(sel("h1, h2"), [dec("display", "none"), dec("position", "relative"), dec("outline", "none")]),
rs(sel(".foo"), [dec("display", "none"), dec("width", "1px")]),
rs(sel(".bar"), [dec("border", "1px solid black")]),
rs(sel(".baz"), [dec("background-color", "black"), dec("background-style", "solid")])
rs(sel("h1, h2"), [dec("display", "none"), dec("position", "relative"), dec("outline", "none")], md(2)),
rs(sel(".foo"), [dec("display", "none"), dec("width", "1px")], md(3)),
rs(sel(".bar"), [dec("border", "1px solid black")], md(4)),
rs(sel(".baz"), [dec("background-color", "black"), dec("background-style", "solid")], md(5))
])
end

Expand All @@ -66,8 +66,8 @@ module Css
$

trans(css).must_equal([
rs(sel(".bar"), [dec("border", "1px solid black /* sdflk */")]),
rs(sel(".baz"), [dec("background", "white /* sdflk */")])
rs(sel(".bar"), [dec("border", "1px solid black /* sdflk */")], md(5)),
rs(sel(".baz"), [dec("background", "white /* sdflk */")], md(6))
])
end

Expand All @@ -86,7 +86,7 @@ module Css
$

trans(css).must_equal([
rs(sel(".baz"), [dec("background", "white"), dec("border", "1px")])
rs(sel(".baz"), [dec("background", "white"), dec("border", "1px")], md(7))
])
end

Expand All @@ -100,7 +100,7 @@ module Css
$

trans(css).must_equal([
rs(sel(".foo"), [])
rs(sel(".foo"), [], md(2))
])
end

Expand All @@ -123,15 +123,15 @@ module Css
$

trans(css).must_equal([
rs(sel("#foo"), [dec("background-color", "black")]),
rs(sel("#bar"), [dec("display", "none")]),
rs(sel("h1"), [dec("outline", "1px")])
rs(sel("#foo"), [dec("background-color", "black")], md(4)),
rs(sel("#bar"), [dec("display", "none")], md(8)),
rs(sel("h1"), [dec("outline", "1px")], md(13))
])
end

it "ignores double semicolons" do
trans("h1 { display:none;;}").must_equal([
rs(sel("h1"), [dec("display", "none")])
rs(sel("h1"), [dec("display", "none")], md(1))
])
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def dec(p, v)
Csscss::Declaration.new(p, v)
end

def rs(selectors, decs)
Csscss::Ruleset.new(selectors, decs)
def rs(selectors, decs, metadata=nil)
Csscss::Ruleset.new(selectors, decs, metadata)
end

def md(ln)
Csscss::Metadata.new(ln)
end
end

Expand Down