Skip to content

Commit

Permalink
Implement --ranges option
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Jul 14, 2015
1 parent b25311d commit ea6680f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 23 additions & 0 deletions spec/cli_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,29 @@ Total: 5 warnings / 0 errors in 1 file
]], get_output "spec/samples/read_globals.lua --std=lua52 --globals foo --read-globals bar --codes --no-config")
end)

it("allows showing token ranges", function()
assert.equal([[
Checking spec/samples/inline_options.lua 8 warnings / 2 errors
spec/samples/inline_options.lua:6:16-16: unused function f
spec/samples/inline_options.lua:12:4-5: accessing undefined variable qu
spec/samples/inline_options.lua:15:1-3: accessing undefined variable baz
spec/samples/inline_options.lua:24:10-10: unused variable g
spec/samples/inline_options.lua:26:7-7: unused variable f
spec/samples/inline_options.lua:26:10-10: unused variable g
spec/samples/inline_options.lua:28:1-17: unpaired push directive
spec/samples/inline_options.lua:30:4-19: unpaired pop directive
spec/samples/inline_options.lua:36:1-2: empty do..end block
spec/samples/inline_options.lua:37:10-13: empty if branch
Checking spec/samples/python_code.lua 1 error
spec/samples/python_code.lua:1:6-15: expected '=' near '__future__'
Total: 8 warnings / 3 errors in 2 files
]], get_output "spec/samples/inline_options.lua spec/samples/python_code.lua --ranges --no-config")
end)

it("applies inline options", function()
assert.equal([[
Checking spec/samples/inline_options.lua 8 warnings / 2 errors
Expand Down
12 changes: 9 additions & 3 deletions src/luacheck/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ local function format_file_report_header(report, file_name, opts)
return label .. (" "):rep(math.max(50 - #label, 1)) .. status
end

local function format_location(file, location)
return ("%s:%d:%d"):format(file, location.line, location.column)
local function format_location(file, location, opts)
local res = ("%s:%d:%d"):format(file, location.line, location.column)

if opts.ranges then
res = ("%s-%d"):format(res, location.end_column)
end

return res
end

local function event_code(event)
Expand All @@ -157,7 +163,7 @@ local function format_event(file_name, event, opts)
message = ("(%s) %s"):format(event_code(event), message)
end

return format_location(file_name, event) .. ": " .. message
return format_location(file_name, event, opts) .. ": " .. message
end

local function format_file_report(report, file_name, opts)
Expand Down

0 comments on commit ea6680f

Please sign in to comment.