Shopify / erb-lint

Lint your ERB or HTML files
MIT License
593 stars 114 forks source link

TrailingWhitespace throws an exception when parsing a file with DOS line endings #267

Open krzygorz opened 1 year ago

krzygorz commented 1 year ago

When I try to lint a file containing DOS line endings on a Linux machine, erb-lint throws an exception:

> cat crlf.html.erb
<div>
  Hello
</div>
> unix2dos crlf.html.erb
unix2dos: converting file crlf.html.erb to DOS format...
> bundle exec erblint crlf.html.erb
.erb-lint.yml not found: using default config
Linting 1 files with 13 linters...
Exception occurred when processing: crlf.html.erb
If this file cannot be processed by erb-lint, you can exclude it in your configuration file.
begin_pos location 22 is out of range for document of size 21
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/better_html-1.0.16/lib/better_html/tokenizer/location.rb:9:in `initialize'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/processed_source.rb:27:in `new'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/processed_source.rb:27:in `to_source_range'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/linters/trailing_whitespace.rb:20:in `block in run'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/linters/trailing_whitespace.rb:14:in `each'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/linters/trailing_whitespace.rb:14:in `run'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/runner.rb:24:in `block in run'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/runner.rb:23:in `each'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/runner.rb:23:in `run'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/cli.rb:107:in `block in run_with_corrections'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/cli.rb:105:in `times'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/cli.rb:105:in `run_with_corrections'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/cli.rb:64:in `block in run'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/cli.rb:61:in `each'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/lib/erb_lint/cli.rb:61:in `run'
/tmp/crlf/vendor/bundle/ruby/3.0.0/gems/erb_lint-0.1.3/exe/erblint:9:in `<top (required)>'
/tmp/crlf/vendor/bundle/ruby/3.0.0/bin/erblint:25:in `load'
/tmp/crlf/vendor/bundle/ruby/3.0.0/bin/erblint:25:in `<top (required)>'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/cli/exec.rb:58:in `load'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/cli/exec.rb:23:in `run'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/cli.rb:483:in `exec'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/cli.rb:31:in `dispatch'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/cli.rb:25:in `start'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/exe/bundle:48:in `block in <top (required)>'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/lib/bundler/friendly_errors.rb:120:in `with_friendly_errors'
/usr/lib/ruby/gems/3.0.0/gems/bundler-2.3.18/exe/bundle:36:in `<top (required)>'
/usr/bin/bundle:25:in `load'
/usr/bin/bundle:25:in `<main>'

No errors were found in ERB files

When I first encountered this issue it took me a while to realize that it was caused by DOS newlines. Running dos2unix on the file made the problem disappear.

The problem also stops when disabling the TrailingWhitespace linter.