Skip to content

Guard against divide by zero error #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2016
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
8 changes: 6 additions & 2 deletions lib/cc/engine/analyzers/javascript/minification_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ def initialize(path)
end

def minified?
ratio = content.chars.count / content.lines.count
ratio >= MINIFIED_AVG_LINE_LENGTH_CUTOFF
if content.lines.count.nonzero?
ratio = content.chars.count / content.lines.count
ratio >= MINIFIED_AVG_LINE_LENGTH_CUTOFF
else
false
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a comment on another PR this morning suggesting that a ? method should probably return a bool, which made sense to me. In which case an else... false branch here might make sense.

end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module Javascript
path = fixture_path("normal_js_file.js")
expect(MinificationChecker.new(path)).to_not be_minified
end

it "returns false for empty files" do
path = fixture_path("empty_file.js")
expect(MinificationChecker.new(path)).to_not be_minified
end
end
end
end
Expand Down
Empty file added spec/fixtures/empty_file.js
Empty file.