Skip to content

Commit

Permalink
Refactor error context line option handling
Browse files Browse the repository at this point in the history
Use same terminology in options and code. Drop support for environment variable as that is not available for any other option.
  • Loading branch information
lautis committed Aug 24, 2019
1 parent 5e544f5 commit 4f65798
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
16 changes: 6 additions & 10 deletions lib/uglifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,11 @@ def harmony_error_message(message)
end
end

def populate_surround_size
surround_size = ENV['ERROR_CONTEXT_LINES'].to_i
surround_size = @options[:error_context_lines].to_i if surround_size <= 0
surround_size = DEFAULTS[:error_context_lines] if surround_size <= 0
surround_size
def error_context_lines
@options.fetch(:error_context_lines, DEFAULTS[:error_context_lines]).to_i
end

def populate_format_options(low, high, line_index, col)
def error_context_format_options(low, high, line_index, col)
line_width = high.to_s.size
{
:line_index => line_index,
Expand Down Expand Up @@ -268,11 +265,10 @@ def format_lines(lines, options)
def context_lines_message(source, line_no, col)
line_index = line_no - 1
lines = source.split("\n")
surround_size = populate_surround_size

base_index = [line_index - surround_size, 0].max
high_no = [line_no + surround_size, lines.size].min
options = populate_format_options(base_index, high_no, line_index, col)
base_index = [line_index - error_context_lines, 0].max
high_no = [line_no + error_context_lines, lines.size].min
options = error_context_format_options(base_index, high_no, line_index, col)
context_lines = lines[base_index...high_no]

"--\n#{format_lines(context_lines, options).join("\n")}\n=="
Expand Down
15 changes: 1 addition & 14 deletions spec/uglifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,6 @@
end

it 'contains harmony error message and follows error_context_lines option' do
ENV['ERROR_CONTEXT_LINES'] = nil
expect { Uglifier.compile(code, :harmony => false, :error_context_lines => 4) }
.to raise_error(Uglifier::Error, %r{
harmony\smode [^\n]+ Uglifier\.new # harmony error mesage
Expand All @@ -759,20 +758,8 @@
}xm)
end

it 'follows ENV.ERROR_CONTEXT_LINES instead of error_context_lines option' do
ENV['ERROR_CONTEXT_LINES'] = '2'
expect { Uglifier.compile(code, :harmony => false, :error_context_lines => 4) }
.to raise_error(Uglifier::Error, %r{
harmony\smode [^\n]+ Uglifier\.new # harmony error mesage
.+ --\n [^\n]+ //_1\n # 1 should be the first line
.+ => [^\n]+ bar \e\[\d+m \(\) # should point to () at line 3
.+ //_5\n ==\z # 5 should be the last line
}xm)
end

it 'shows lines surrounded syntax error when harmony mode is on' do
ENV['ERROR_CONTEXT_LINES'] = '2'
expect { Uglifier.compile(code, :harmony => true) }
expect { Uglifier.compile(code, :harmony => true, :error_context_lines => 2) }
.to raise_error(Uglifier::Error, %r{
Unexpect[^\n]+ ; [^\n]+expect [^\n]+ , # syntax error mesage
.+ --\n [^\n]+ //_16\n # 16 should be the first line
Expand Down

0 comments on commit 4f65798

Please sign in to comment.