Skip to content

Commit

Permalink
Merge pull request #295 from repinel/fix-new-line-after-multi-line-co…
Browse files Browse the repository at this point in the history
…mment

Remove extra new line after multi-line comments
  • Loading branch information
schneems committed Jun 8, 2016
2 parents 697cc28 + 6012c92 commit 068614b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/sprockets/directive_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def process_source(source)

data = String.new("")
data.force_encoding(body.encoding)
data << header << "\n" unless header.empty?
data << header unless header.empty?
data << body
# Ensure body ends in a new line
data << "\n" if data.length > 0 && data[-1] != "\n"
Expand Down Expand Up @@ -146,7 +146,11 @@ def extract_directives(header)
processed_header << line
end

return processed_header.chomp, directives
processed_header.chomp!
# Ensure header ends in a new line like before it was processed
processed_header << "\n" if processed_header.length > 0 && header[-1] == "\n"

return processed_header, directives
end

# Gathers comment directives in the source and processes them.
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/asset/multi_line_comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/******/ function foo() {
}
7 changes: 6 additions & 1 deletion test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ def setup
end

test "require_self inserts the current file's body at the specified point" do
assert_equal "/* b.css */\n\nb { display: none }\n/*\n\n\n\n */\n\n\nbody {}\n.project {}\n", asset("require_self.css").to_s
assert_equal "/* b.css */\nb { display: none }\n/*\n\n\n\n */\n\nbody {}\n.project {}\n", asset("require_self.css").to_s
end

test "multiple require_self directives raises and error" do
Expand Down Expand Up @@ -1054,6 +1054,11 @@ def setup
asset("semicolons.js").to_s
end

test 'keeps code in same line after multi-line comments' do
assert_equal "/******/ function foo() {\n}\n",
asset('multi_line_comment.js').to_s
end

test "should not fail if home is not set in environment" do
begin
home, ENV["HOME"] = ENV["HOME"], nil
Expand Down
2 changes: 1 addition & 1 deletion test/test_bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TestStylesheetBundle < Sprockets::TestCase
metadata: {}
}

data = "/* b.css */\n\nb { display: none }\n/*\n\n\n\n */\n\n\nbody {}\n.project {}\n"
data = "/* b.css */\nb { display: none }\n/*\n\n\n\n */\n\nbody {}\n.project {}\n"
result = Sprockets::Bundle.call(input)
assert_equal data, result[:data]
assert_equal [
Expand Down
2 changes: 1 addition & 1 deletion test/test_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def self.test(name, &block)
end

test "find index.css in directory" do
assert_equal ".c {}\n.d {}\n/*\n\n */\n\n", @env["mobile.css"].to_s
assert_equal ".c {}\n.d {}\n/*\n\n */\n", @env["mobile.css"].to_s
end

test "ignore index.min.js in directory" do
Expand Down

0 comments on commit 068614b

Please sign in to comment.