diff options
author | Peter Melnichenko <mpeterval@gmail.com> | 2018-09-21 15:22:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-21 15:22:54 +0300 |
commit | b9cc7822c11288704b8757c2d18244eccd3393d4 (patch) | |
tree | 836bcb271089264dde0276ea2150887a3b89d6fb | |
parent | 6bf03c5eb9895c9a10d6cac53f7de0ad5f03901e (diff) | |
parent | 10dd7332c39b731a6dc7fa6394527b8bacdf316a (diff) |
Merge pull request #273 from Tieske/linter
added linter to the test setup and fixed warnings
42 files changed, 129 insertions, 122 deletions
diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..b549334 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,22 @@ +unused_args = false +redefined = false +max_line_length = false + + +not_globals = { + "string.len", + "table.getn", +} + + +exclude_files = { + "tests/*.lua", + "tests/**/*.lua", + "here/*.lua", + "here/**/*.lua", + + -- TODO: fix these files + "examples/symbols.lua", + "examples/test-symbols.lua", +} + diff --git a/.travis.yml b/.travis.yml index f0dd90f..8ebb248 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,12 +14,14 @@ before_install: - pip install hererocks - hererocks here -r^ --$LUA - source here/bin/activate + - luarocks install luacheck install: - luarocks make - luarocks install luacov-coveralls script: + - luacheck . - lua run.lua tests --luacov after_success: diff --git a/examples/seesubst.lua b/examples/seesubst.lua index e13a5fb..a2d0f18 100644 --- a/examples/seesubst.lua +++ b/examples/seesubst.lua @@ -4,10 +4,11 @@ -- or 'pl.seq.map' (a function reference); these cases must be distinguished -- and a Markdown link generated pointing to the LuaDoc file. -require 'pl' +local sip = require 'pl.sip' +local stringx = require 'pl.stringx' local res = {} -s = [[ +local s = [[ (@see pl.bonzo.dog) remember about @see pl.bonzo @@ -15,7 +16,7 @@ remember about @see pl.bonzo local _gsub_patterns = {} -function gsub (s,pat,subst,start) +local function gsub (s,pat,subst,start) local fpat = _gsub_patterns[pat] if not fpat then -- use SIP to generate a proper string pattern. diff --git a/examples/sipscan.lua b/examples/sipscan.lua index 37f712e..78ac75b 100644 --- a/examples/sipscan.lua +++ b/examples/sipscan.lua @@ -3,9 +3,10 @@ -- pattern generated: -- SYNC%s*%[([+%-%d]%d*)%]%s*([+%-%d]%d*)%s*([+%-%d]%d*) -require 'pl' +local sip = require 'pl.sip' +local stringx = require 'pl.stringx' -s = [[ +local s = [[ SYNC [1] 0 547 (14679 sec) SYNC [2] 0 555 (14679 sec) SYNC [3] 0 563 (14679 sec) @@ -16,7 +17,7 @@ SYNC [6] 0 587 (14679 sec) local first = true -local start +local expected local res = {} local pat = 'SYNC [$i{seq}] $i{diff} $i{val}' print(sip.create_pattern(pat)) diff --git a/examples/test-cmp.lua b/examples/test-cmp.lua index e3748a3..cbab394 100644 --- a/examples/test-cmp.lua +++ b/examples/test-cmp.lua @@ -1,4 +1,3 @@ -A = require 'pl.tablex' -ops = require 'pl.operator' +local A = require 'pl.tablex' print(A.compare_no_order({1,2,3},{2,1,3})) print(A.compare_no_order({1,2,3},{2,1,3},'==')) diff --git a/examples/test-listcallbacks.lua b/examples/test-listcallbacks.lua index 6bcdad3..a9a31c3 100644 --- a/examples/test-listcallbacks.lua +++ b/examples/test-listcallbacks.lua @@ -1,7 +1,8 @@ -- demonstrates how to use a list of callbacks -require 'pl' -actions = List() -L = utils.string_lambda +local List = require 'pl.List' +local utils = require 'pl.utils' +local actions = List() +local L = utils.string_lambda actions:append(function() print 'hello' end) actions:append(L '|| print "yay"') diff --git a/examples/test-pretty.lua b/examples/test-pretty.lua index 1423c1c..7b2b553 100644 --- a/examples/test-pretty.lua +++ b/examples/test-pretty.lua @@ -1,6 +1,6 @@ local pretty = require 'pl.pretty' -tb = { +local tb = { 'one','two','three',{1,2,3}, alpha=1,beta=2,gamma=3,['&']=true,[0]=false, _fred = {true,true}, diff --git a/examples/testapp.lua b/examples/testapp.lua index 650ac4e..b0dbf5f 100644 --- a/examples/testapp.lua +++ b/examples/testapp.lua @@ -1,5 +1,5 @@ -- shows how a script can get a private file path -- the output on my Windows machine is: -- C:\Documents and Settings\steve\.testapp\test.txt -require 'pl' +local app = require 'pl.app' print(app.appfile 'test.txt') diff --git a/examples/testclone.lua b/examples/testclone.lua index c211bc4..b0d948f 100644 --- a/examples/testclone.lua +++ b/examples/testclone.lua @@ -1,8 +1,10 @@ --cloning a directory tree. local lfs = require 'lfs' -require 'pl' -p1 = [[examples]] -p2 = [[copy/of/examples]] +local path = require 'pl.path' +local dir = require 'pl.dir' + +local p1 = [[examples]] +local p2 = [[copy/of/examples]] if not path.isfile 'examples/testclone.lua' then return print 'please run this in the penlight folder (below examples)' @@ -14,7 +16,7 @@ dir.clonetree(p1,p2,dir.copyfile) assert(path.isdir 'copy') print '---' -t = os.time() +local t = os.time() print(lfs.touch('examples/testclone.lua',t,t+10)) -- this should only update this file diff --git a/examples/testconfig.lua b/examples/testconfig.lua index 03349d6..4712747 100644 --- a/examples/testconfig.lua +++ b/examples/testconfig.lua @@ -1,7 +1,7 @@ local stringio = require 'pl.stringio' local config = require 'pl.config' -function dump(t,indent) +local function dump(t,indent) if type(t) == 'table' then io.write(indent,'{\n') local newindent = indent..' ' @@ -17,7 +17,7 @@ function dump(t,indent) end -function testconfig(test) +local function testconfig(test) local f = stringio.open(test) local c = config.read(f) f:close() diff --git a/examples/testglobal.lua b/examples/testglobal.lua index 053b6bf..0baaaef 100644 --- a/examples/testglobal.lua +++ b/examples/testglobal.lua @@ -15,7 +15,7 @@ local path = require 'pl.path' utils.on_error 'quit' -local txt,err = file.read(arg[1] or path.normpath('examples/testglobal.lua')) +local txt = file.read(arg[1] or path.normpath('examples/testglobal.lua')) local globals = List() for t,v in lexer.lua(txt) do if t == 'iden' and rawget(_G,v) then diff --git a/examples/testinputfields.lua b/examples/testinputfields.lua index 37b3a8c..9269488 100644 --- a/examples/testinputfields.lua +++ b/examples/testinputfields.lua @@ -1,4 +1,4 @@ -require 'pl' +local input = require 'pl.input' local sum = 0.0 local count = 0 local text = [[ diff --git a/examples/testinputfields2.lua b/examples/testinputfields2.lua index fd7b0cd..fc649eb 100644 --- a/examples/testinputfields2.lua +++ b/examples/testinputfields2.lua @@ -1,4 +1,5 @@ -require 'pl' +local input = require 'pl.input' +local seq = require 'pl.seq' local text = [[ 981124001 2.0 18988.4 10047.1 4149.7 981125001 0.8 19104.0 9970.4 5088.7 diff --git a/examples/testxml.lua b/examples/testxml.lua index e7c7476..2528020 100644 --- a/examples/testxml.lua +++ b/examples/testxml.lua @@ -3,12 +3,13 @@ -- This is (clearly) not a professional XML parser, so don't use it -- on your homework! -require 'pl' +local lexer = require 'pl.lexer' +local pretty = require 'pl.pretty' local append = table.insert local skipws,expecting = lexer.skipws,lexer.expecting -function parse_element (tok,tag) +local function parse_element (tok,tag) local tbl,t,v,attrib tbl = {} tbl.tag = tag -- LOM 'tag' is the element tag @@ -51,21 +52,22 @@ function parse_element (tok,tag) end end -function parse_xml (tok) - local t,v = skipws(tok) +local function parse_xml (tok) + local t = skipws(tok) + local v while t == '<' do t,v = tok() if t == '?' or t == '!' then -- skip meta stuff and commentary repeat t = tok() until t == '>' - t,v = expecting(tok,'<') + t = expecting(tok,'<') else return parse_element(tok,v) end end end -s = [[ +local s = [[ <?xml version="1.0" encoding="UTF-8"?> <sensor name="closure-meter-2" id="7D7D0600006F0D00" loc="100,100,0" device="closure-meter" init="true"> <detector name="closure-meter" phenomenon="closure" units="mm" id="1" diff --git a/examples/which.lua b/examples/which.lua index 37f7649..0544cfc 100644 --- a/examples/which.lua +++ b/examples/which.lua @@ -7,7 +7,7 @@ local app = require 'pl.app' local pathl = List.split(os.getenv 'PATH',path.dirsep) -function which (file) +local function which (file) local res = pathl:map(path.join,file) res = res:filter(path.exists) if res then return res[1] end diff --git a/lua/pl/Date.lua b/lua/pl/Date.lua index 44c4b54..9c878d6 100644 --- a/lua/pl/Date.lua +++ b/lua/pl/Date.lua @@ -510,7 +510,7 @@ Allowed patterns: ]] -local function looks_like_a_month(w) +local function looks_like_a_month(w) return w:match '^%a+,*$' ~= nil end local is_number = stringx.isdigit @@ -537,7 +537,7 @@ local function parse_iso_end(p,ns,sec) -- (we're working with the date as lower case, hence 'z') if p:match 'z$' then -- we're UTC! return sec, {h=0,m=0} - end + end p = p:gsub(':','') -- turn 00:30 to 0030 local _,_,sign,offs = p:find('^([%+%-])(%d+)') if not sign then return sec, nil end -- not UTC diff --git a/lua/pl/List.lua b/lua/pl/List.lua index e19e702..b66c251 100644 --- a/lua/pl/List.lua +++ b/lua/pl/List.lua @@ -346,7 +346,7 @@ function List:__eq(L) return true end ---- Join the elements of a list using a delimiter. +--- Join the elements of a list using a delimiter. -- This method uses tostring on all elements. -- @string[opt=''] delim a delimiter string, can be empty. -- @return a string diff --git a/lua/pl/OrderedMap.lua b/lua/pl/OrderedMap.lua index 3e49af5..379c44f 100644 --- a/lua/pl/OrderedMap.lua +++ b/lua/pl/OrderedMap.lua @@ -32,7 +32,7 @@ end local assert_arg,raise = utils.assert_arg,utils.raise --- update an OrderedMap using a table. --- If the table is itself an OrderedMap, then its entries will be appended. +-- If the table is itself an OrderedMap, then its entries will be appended. -- if it s a table of the form `{{key1=val1},{key2=val2},...}` these will be appended. -- -- Otherwise, it is assumed to be a map-like table, and order of extra entries is arbitrary. @@ -132,7 +132,7 @@ end function OrderedMap:iter () local i = 0 local keys = self._keys - local n,idx = #keys + local idx return function() i = i + 1 if i > #keys then return nil end diff --git a/lua/pl/app.lua b/lua/pl/app.lua index 21b6a7b..bf9621b 100644 --- a/lua/pl/app.lua +++ b/lua/pl/app.lua @@ -50,7 +50,7 @@ end -- @return 'cannot create' error function app.appfile (file) local sname = path.basename(check_script_name()) - local name,ext = path.splitext(sname) + local name = path.splitext(sname) local dir = path.join(path.expanduser('~'),'.'..name) if not path.isdir(dir) then local ret = path.mkdir(dir) diff --git a/lua/pl/array2d.lua b/lua/pl/array2d.lua index b15e39e..40b4eca 100644 --- a/lua/pl/array2d.lua +++ b/lua/pl/array2d.lua @@ -4,8 +4,8 @@ -- Dependencies: `pl.utils`, `pl.tablex`, `pl.types` -- @module pl.array2d -local type,tonumber,assert,tostring,io,ipairs,string,table = - _G.type,_G.tonumber,_G.assert,_G.tostring,_G.io,_G.ipairs,_G.string,_G.table +local tonumber,assert,tostring,io,ipairs,string,table = + _G.tonumber,_G.assert,_G.tostring,_G.io,_G.ipairs,_G.string,_G.table local setmetatable,getmetatable = setmetatable,getmetatable local tablex = require 'pl.tablex' @@ -16,6 +16,8 @@ local remove = table.remove local splitv,fprintf,assert_arg = utils.splitv,utils.fprintf,utils.assert_arg local byte = string.byte local stdout = io.stdout +local min = math.min + local array2d = {} @@ -96,10 +98,6 @@ function array2d.reduce2 (opc,opr,a) return reduce(opc,tmp) end -local function dimension (t) - return type(t[1])=='table' and 2 or 1 -end - --- map a function over two arrays. -- They can be both or either 2D arrays -- @func f function of at least two arguments @@ -253,8 +251,6 @@ function array2d.remove_col (t,j) end end -local Ai = byte 'A' - local function _parse (s) local c,r if s:sub(1,1) == 'R' then @@ -401,8 +397,6 @@ function array2d.forall (t,row_op,end_row_op,i1,j1,i2,j2) end end -local min, max = math.min, math.max - ---- move a block from the destination to the source. -- @array2d dest a 2D array -- @int di start row in dest @@ -440,7 +434,7 @@ function array2d.iter (a,indices,i1,j1,i2,j2) assert_arg(1,a,'table') local norowset = not (i2 and j2) i1,j1,i2,j2 = default_range(a,i1,j1,i2,j2) - local n,i,j = i2-i1+1,i1-1,j1-1 + local i,j = i1-1,j1-1 local row,nr = nil,0 local onr = j2 - j1 + 1 return function() diff --git a/lua/pl/class.lua b/lua/pl/class.lua index a57ac71..847f442 100644 --- a/lua/pl/class.lua +++ b/lua/pl/class.lua @@ -60,7 +60,7 @@ end -- if pussycat:is_a(Cat) then -- -- it's true, it is a Lion, but also a Cat -- end --- +-- -- if pussycat:is_a() == Lion then -- -- It's true -- end @@ -131,7 +131,7 @@ local function _class(base,c_arg,c) else c = c or {} end - + if type(base) == 'table' then -- our new class is a shallow copy of the base class! -- but be careful not to wipe out any methods we have been given at this point! diff --git a/lua/pl/compat.lua b/lua/pl/compat.lua index 58cf6b2..f3832b8 100644 --- a/lua/pl/compat.lua +++ b/lua/pl/compat.lua @@ -131,7 +131,7 @@ end -- @return the length -- @function table.pack if not table.pack then - function table.pack (...) + function table.pack (...) -- luacheck: ignore return {n=select('#',...); ...} end end @@ -144,7 +144,7 @@ end -- @function package.searchpath if not package.searchpath then local sep = package.config:sub(1,1) - function package.searchpath (mod,path) + function package.searchpath (mod,path) -- luacheck: ignore mod = mod:gsub('%.',sep) for m in path:gmatch('[^;]+') do local nm = m:gsub('?',mod) diff --git a/lua/pl/comprehension.lua b/lua/pl/comprehension.lua index 9d8aa59..39be7c5 100644 --- a/lua/pl/comprehension.lua +++ b/lua/pl/comprehension.lua @@ -73,7 +73,6 @@ local ops = { -- @usage "(x+y)^2 for x for y if x > y" -- nested -- local function parse_comprehension(expr) - local t = {} local pos = 1 -- extract opname (if exists) diff --git a/lua/pl/config.lua b/lua/pl/config.lua index a71c472..b23c07b 100644 --- a/lua/pl/config.lua +++ b/lua/pl/config.lua @@ -103,7 +103,7 @@ end -- @return a table containing items, or `nil` -- @return error message (same as @{config.lines} function config.read(file,cnfg) - local f,openf,err,auto + local auto local iter,err = config.lines(file) if not iter then return nil,err end diff --git a/lua/pl/data.lua b/lua/pl/data.lua index 84f26bd..a565ebc 100644 --- a/lua/pl/data.lua +++ b/lua/pl/data.lua @@ -189,10 +189,6 @@ local function open_file (f,mode) return f,nil,opened end -local function all_n () - -end - --- read a delimited file in a Lua table. -- By default, attempts to treat first line as separated list of fieldnames. -- @param file a filename or a file-like object @@ -210,7 +206,7 @@ end -- @return error message. May be a file error, 'not a file-like object' -- or a conversion error function data.read(file,cnfg) - local err,opened,count,line + local count,line local D = {} if not cnfg then cnfg = {} end local f,err,opened = open_file(file,'r') @@ -473,7 +469,6 @@ end local function process_select (data,parms) --- preparing fields ---- - local res,ret field_error = nil local fields = parms.fields local numfields = fields:find '%$' or #data.fieldnames == 0 @@ -495,7 +490,7 @@ local function process_select (data,parms) fields = rstrip(fields):gsub('[^,%w]','_') end local massage_fields = utils.bind1(massage_fields,data) - ret = gsub(fields,idpat,massage_fields) + local ret = gsub(fields,idpat,massage_fields) if field_error then return nil,field_error end parms.fields = fields parms.proc_fields = ret @@ -555,7 +550,7 @@ function data.query(data,condn,context,return_row) else return nil, "condition must be a string or a table" end - local query, k + local query if condn.sort_by then -- use sorted_query query = sorted_query else @@ -565,7 +560,7 @@ function data.query(data,condn,context,return_row) if return_row then fields = '{'..fields..'}' end - query,k = query:gsub('FIELDLIST',fields) + query = query:gsub('FIELDLIST',fields) if is_string(condn.where) then query = query:gsub('CONDITION',condn.where) condn.where = nil @@ -636,7 +631,6 @@ end -- @param outfile filename or file-like object -- @bool dont_fail true if you want to return an error, not just fail function data.filter (Q,infile,outfile,dont_fail) - local err local d = data.read(infile or 'stdin') local out = open_file(outfile or 'stdout') local iter,err = d:select(Q) diff --git a/lua/pl/dir.lua b/lua/pl/dir.lua index 01b3be8..df37e84 100644 --- a/lua/pl/dir.lua +++ b/lua/pl/dir.lua @@ -198,7 +198,6 @@ local function file_op (is_copy,src,dest,flag) if not CopyFile then src = path.normcase(src) dest = path.normcase(dest) - local cmd = is_copy and 'copy' or 'rename' local res, err = execute_command('copy',two_arguments(src,dest)) if not res then return false,err end if not is_copy then @@ -379,7 +378,7 @@ function dir.clonetree (path1,path2,file_fun,verbose) if verbose then verbose('normalized:',path1,path2) end -- particularly NB that the new path isn't fully contained in the old path if path1 == path2 then return raise "paths are the same" end - local i1,i2 = path2:find(path1,1,true) + local _,i2 = path2:find(path1,1,true) if i2 == #path1 and path2:sub(i2+1,i2+1) == path.sep then return raise 'destination is a subdirectory of the source' end diff --git a/lua/pl/func.lua b/lua/pl/func.lua index 28c01d8..e2a0787 100644 --- a/lua/pl/func.lua +++ b/lua/pl/func.lua @@ -26,6 +26,7 @@ local tablex = require 'pl.tablex' local map = tablex.map local _DEBUG = rawget(_G,'_DEBUG') local assert_arg = utils.assert_arg +local pack = table.pack or function(...) return { n = select("#", ...), ... } end local func = {} @@ -165,9 +166,9 @@ local operators = { ['and'] = 1, ['=='] = 2, ['~='] = 2, ['<'] = 2, ['>'] = 2, ['<='] = 2, ['>='] = 2, ['..'] = 3, - ['+'] = 4, ['-'] = 4, + ['+'] = 4, ['-'] = 4, -- luacheck: ignore ['*'] = 5, ['/'] = 5, ['%'] = 5, - ['not'] = 6, ['#'] = 6, ['-'] = 6, + ['not'] = 6, ['#'] = 6, ['-'] = 6, -- TODO: fix the LuaCheck ignore above! ['^'] = 7 } @@ -335,7 +336,7 @@ end -- @usage (bind(f,_1,a))(b) == f(a,b) -- @usage (bind(f,_2,_1))(a,b) == f(b,a) function func.bind(fn,...) - local args = table.pack(...) + local args = pack(...) local holders,parms,bvalues,values = {},{},{'fn'},{} local nv,maxplace,varargs = 1,0,false for i = 1,args.n do @@ -365,7 +366,7 @@ return function (%s) end ]]):format(bvalues,parms,holders) if _DEBUG then print(fstr) end - local res,err = utils.load(fstr) + local res = utils.load(fstr) res = res() return res(fn,unpack(values)) end diff --git a/lua/pl/lapp.lua b/lua/pl/lapp.lua index 5ef42e9..56d1d9d 100644 --- a/lua/pl/lapp.lua +++ b/lua/pl/lapp.lua @@ -33,11 +33,10 @@ local function lines(s) return s:gmatch('([^\n]*)\n') end local function lstrip(str) return str:gsub('^%s+','') end local function strip(str) return lstrip(str):gsub('%s+$','') end local function at(s,k) return s:sub(k,k) end -local function isdigit(s) return s:find('^%d+$') == 1 end local lapp = {} -local open_files,parms,aliases,parmlist,usage,windows,script +local open_files,parms,aliases,parmlist,usage,script lapp.callback = false -- keep Strict happy @@ -182,7 +181,6 @@ end -- @return a table with parameter-value pairs function lapp.process_options_string(str,args) local results = {} - local opts = {at_start=true} local varargs local arg = args or _G.arg open_files = {} @@ -219,7 +217,7 @@ function lapp.process_options_string(str,args) for line in lines(str) do local res = {} - local optspec,optparm,i1,i2,defval,vtype,constraint,rest + local optparm,defval,vtype,constraint,rest line = lstrip(line) local function check(str) return match(str,line,res) @@ -314,7 +312,7 @@ function lapp.process_options_string(str,args) end ps.constraint = types[vtype].constraint elseif not builtin_types[vtype] and vtype then - lapp.error(vtype.." is unknown type") + lapp.error(vtype.." is unknown type") end parms[optparm] = ps end diff --git a/lua/pl/lexer.lua b/lua/pl/lexer.lua index 071a62b..68a0874 100644 --- a/lua/pl/lexer.lua +++ b/lua/pl/lexer.lua @@ -267,7 +267,7 @@ end -- @param tok a token stream -- @return a string function lexer.getline (tok) - local t,v = tok('.-\n') + local _,v = tok('.-\n') return v end @@ -284,7 +284,7 @@ end -- @param tok a token stream -- @return a string function lexer.getrest (tok) - local t,v = tok('.+') + local _,v = tok('.+') return v end diff --git a/lua/pl/luabalanced.lua b/lua/pl/luabalanced.lua index a75f6fd..a1f7dc6 100644 --- a/lua/pl/luabalanced.lua +++ b/lua/pl/luabalanced.lua @@ -130,6 +130,7 @@ local wordop = {['and']=true, ['or']=true, ['not']=true} local is_compare = {['>']=true, ['<']=true, ['~']=true} local function match_expression(s, pos) pos = pos or 1 + local _ local posa = pos local lastident local poscs, posce @@ -149,7 +150,7 @@ local function match_expression(s, pos) posce = pos end elseif c == '(' or c == '{' or c == '[' then - local part; part, pos = match_bracketed(s, pos) + _, pos = match_bracketed(s, pos) elseif c == '=' and s:sub(pos+1,pos+1) == '=' then pos = pos + 2 -- skip over two-char op containing '=' elseif c == '=' and is_compare[s:sub(pos-1,pos-1)] then diff --git a/lua/pl/path.lua b/lua/pl/path.lua index f8f3d78..5979e8e 100644 --- a/lua/pl/path.lua +++ b/lua/pl/path.lua @@ -128,7 +128,7 @@ else path.sep = '/' path.dirsep = ':' end -local sep,dirsep = path.sep,path.dirsep +local sep = path.sep --- are we running Windows? -- @class field @@ -205,7 +205,7 @@ end -- @string P A file path function path.dirname(P) assert_string(1,P) - local p1,p2 = path.splitpath(P) + local p1 = path.splitpath(P) return p1 end @@ -213,7 +213,7 @@ end -- @string P A file path function path.basename(P) assert_string(1,P) - local p1,p2 = path.splitpath(P) + local _,p2 = path.splitpath(P) return p2 end @@ -221,7 +221,7 @@ end -- @string P A file path function path.extension(P) assert_string(1,P) - local p1,p2 = path.splitext(P) + local _,p2 = path.splitext(P) return p2 end @@ -326,13 +326,6 @@ function path.normpath(P) return P end -local function ATS (P) - if at(P,#P) ~= path.sep then - P = P..path.sep - end - return path.normcase(P) -end - --- relative path from current directory or optional start point -- @string P a path -- @string[opt] start optional start point (default current directory) diff --git a/lua/pl/permute.lua b/lua/pl/permute.lua index 56d1a8d..732500b 100644 --- a/lua/pl/permute.lua +++ b/lua/pl/permute.lua @@ -43,7 +43,7 @@ function permute.iter (a) local n = #a local co = coroutine.create(function () permgen(a, n, coroutine.yield) end) return function () -- iterator - local code, res = resume(co) + local _, res = resume(co) return res end end diff --git a/lua/pl/pretty.lua b/lua/pl/pretty.lua index 7365ad3..a9abe0e 100644 --- a/lua/pl/pretty.lua +++ b/lua/pl/pretty.lua @@ -7,7 +7,8 @@ local append = table.insert local concat = table.concat -local mfloor, mhuge, mtype = math.floor, math.huge, math.type +local mfloor, mhuge = math.floor, math.huge +local mtype = math.type local utils = require 'pl.utils' local lexer = require 'pl.lexer' local debug = require 'debug' @@ -46,12 +47,12 @@ local pretty = {} local function save_global_env() local env = {} env.hook, env.mask, env.count = debug.gethook() - + -- env.hook is "external hook" if is a C hook function if env.hook~="external hook" then debug.sethook() end - + env.string_mt = getmetatable("") debug.setmetatable("", nil) return env @@ -210,7 +211,7 @@ function pretty.write (tbl,space,not_clever) end local function eat_last_comma () - local n,lastch = #lines + local n = #lines local lastch = lines[n]:sub(-1,-1) if lastch == ',' then lines[n] = lines[n]:sub(1,-2) diff --git a/lua/pl/seq.lua b/lua/pl/seq.lua index c867add..1c08d20 100644 --- a/lua/pl/seq.lua +++ b/lua/pl/seq.lua @@ -79,9 +79,9 @@ end -- @return iterator over keys function seq.keys(t) assert_arg(1,t,'table') - local key,value + local key return function() - key,value = next(t,key) + key = next(t,key) return key end end @@ -511,7 +511,6 @@ setmetatable(seq,{ -- @param ... for Lua 5.2 only, optional format specifiers, as in `io.read`. -- @return a sequence wrapper function seq.lines (f,...) - local n = select('#',...) local iter,obj if f == 'STDIN' then f = io.stdin diff --git a/lua/pl/strict.lua b/lua/pl/strict.lua index d4e0fc7..ab7875f 100644 --- a/lua/pl/strict.lua +++ b/lua/pl/strict.lua @@ -28,10 +28,9 @@ end -- @tab[opt] predeclared - table of variables that are to be considered predeclared. -- @return the given table, or a new table function strict.module (name,mod,predeclared) - local mt, old_newindex, old_index, old_index_type, global, closed + local mt, old_newindex, old_index, old_index_type, global if predeclared then global = predeclared.__global - closed = predeclared.__closed end if type(mod) == 'table' then mt = getmetatable(mod) @@ -71,13 +70,13 @@ function strict.module (name,mod,predeclared) local fallback = old_index[n] if fallback ~= nil then return fallback - end + end else local res = old_index(t, n) if res ~= nil then return res end - end + end end local msg = "variable '"..n.."' is not declared" if name then diff --git a/lua/pl/stringio.lua b/lua/pl/stringio.lua index 10fd102..666f415 100644 --- a/lua/pl/stringio.lua +++ b/lua/pl/stringio.lua @@ -72,7 +72,7 @@ function SR:_read(fmt) res = str:sub(i) self.i = sz elseif fmt == '*n' then - local _,i2,i2,idx + local _,i2,idx _,idx = str:find ('%s*%d+',i) _,i2 = str:find ('^%.%d+',idx+1) if i2 then idx = i2 end diff --git a/lua/pl/stringx.lua b/lua/pl/stringx.lua index adf1070..8bcc6a2 100644 --- a/lua/pl/stringx.lua +++ b/lua/pl/stringx.lua @@ -274,7 +274,7 @@ end -- @string sub substring function stringx.count(s,sub) assert_string(1,s) - local i,k = _find_all(s,sub,1) + local _,k = _find_all(s,sub,1) return k end @@ -499,17 +499,16 @@ end -- @return 'nil' if not found. If found, the maximum number of equal signs found within all matches. local function has_lquote(s) local lstring_pat = '([%[%]])(=*)%1' - local equals - local start, finish, bracket, new_equals = nil, 1, nil, nil - + local equals, new_equals, _ + local finish = 1 repeat - start, finish, bracket, new_equals = s:find(lstring_pat, finish) + _, finish, _, new_equals = s:find(lstring_pat, finish) if new_equals then equals = max(equals or 0, #new_equals) end until not new_equals - return equals + return equals end --- Quote the given string and preserve any control or escape characters, such that reloading the string in Lua returns the same result. @@ -530,8 +529,8 @@ function stringx.quote_string(s) equal_signs = ("="):rep((equal_signs or -1) + 1) -- Long strings strip out leading newline. We want to retain that, when quoting. if s:find("^\n") then s = "\n" .. s end - local lbracket, rbracket = - "[" .. equal_signs .. "[", + local lbracket, rbracket = + "[" .. equal_signs .. "[", "]" .. equal_signs .. "]" s = lbracket .. s .. rbracket else diff --git a/lua/pl/template.lua b/lua/pl/template.lua index 2f97e8f..9961a1b 100644 --- a/lua/pl/template.lua +++ b/lua/pl/template.lua @@ -60,8 +60,9 @@ local function parseHashLines(chunk,inline_escape,brackets,esc,newline) local esc_pat1, esc_pat2 = "^"..esc_pat, "\n"..esc_pat local pieces, s = {"return function()\nlocal __R_size, __R_table, __tostring = 0, {}, __tostring", n = 1}, 1 while true do - local ss, e, lua = strfind(chunk,esc_pat1, s) + local _, e, lua = strfind(chunk,esc_pat1, s) if not e then + local ss ss, e, lua = strfind(chunk,esc_pat2, s) parseDollarParen(pieces, strsub(chunk,s, ss), exec_pat, newline) if not e then break end diff --git a/lua/pl/test.lua b/lua/pl/test.lua index 87f88f0..ddf8345 100644 --- a/lua/pl/test.lua +++ b/lua/pl/test.lua @@ -16,6 +16,7 @@ local type,unpack = type,utils.unpack local clock = os.clock local debug = require 'debug' local io = io +local pack = table.pack or function(...) return { select("#", ...), ...} end local function dump(x) if type(x) == 'table' and not (getmetatable(x) and getmetatable(x).__tostring) then @@ -85,11 +86,11 @@ end -- @param e a string to match the error against -- @param where extra level offset function test.assertraise(fn,e,where) - local ok, err + local _, err if type(fn) == 'table' then - ok, err = pcall(unpack(fn)) + _, err = pcall(unpack(fn)) else - ok, err = pcall(fn) + _, err = pcall(fn) end if not err or err:match(e)==nil then complain (err,e,"these errors did not match",where) @@ -112,7 +113,7 @@ end -- tuple type -- local tuple_mt = { - unpack = table.unpack + unpack = unpack } tuple_mt.__index = tuple_mt @@ -146,7 +147,7 @@ end -- and there is an `unpack` method. -- @usage asserteq(tuple( ('ab'):find 'a'), tuple(1,1)) function test.tuple(...) - return setmetatable(table.pack(...), tuple_mt) + return setmetatable(pack(...), tuple_mt) end --- Time a function. Call the function a given number of times, and report the number of seconds taken, diff --git a/lua/pl/text.lua b/lua/pl/text.lua index d18fcc0..341e2bf 100644 --- a/lua/pl/text.lua +++ b/lua/pl/text.lua @@ -68,7 +68,7 @@ end function text.dedent (s) assert_arg(1,s,'string') local sl = split(s,'\n') - local i1,i2 = (#sl>0 and sl[1] or ''):find('^%s*') + local _,i2 = (#sl>0 and sl[1] or ''):find('^%s*') sl = imap(string.sub,sl,i2+1) return concat(sl,'\n')..'\n' end diff --git a/lua/pl/utils.lua b/lua/pl/utils.lua index da3e327..bcd0e01 100644 --- a/lua/pl/utils.lua +++ b/lua/pl/utils.lua @@ -268,7 +268,6 @@ end -- @return stdout output (string) -- @return errout output (string) function utils.executeex(cmd, bin) - local mode local outfile = os.tmpname() local errfile = os.tmpname() diff --git a/lua/pl/xml.lua b/lua/pl/xml.lua index ae26f5e..4650a32 100644 --- a/lua/pl/xml.lua +++ b/lua/pl/xml.lua @@ -186,7 +186,6 @@ end -- doc = parent {child 'one', child 'two'} function _M.tags(list) local ctors = {} - local elem = _M.elem if is_text(list) then list = split(list,'%s*,%s*') end for _,tag in ipairs(list) do local ctor = function(items) return _M.elem(tag,items) end @@ -572,9 +571,10 @@ function _M.basic_parse(s,all_text,html) t_insert(stack, top) local ni,c,label,xarg, empty, _, istart - local i, j = 1, 1 + local i = 1 + local j -- we're not interested in <?xml version="1.0"?> - _,istart = s_find(s,'^%s*<%?[^%?]+%?>%s*') + _,istart = s_find(s,'^%s*<%?[^%?]+%?>%s*') if not istart then -- or <!DOCTYPE ...> _,istart = s_find(s,'^%s*<!DOCTYPE.->%s*') end @@ -596,8 +596,6 @@ function _M.basic_parse(s,all_text,html) if html then label = label:lower() if html_empty_elements[label] then empty = "/" end - if label == 'script' then - end end if all_text or not s_find(text, "^%s*$") then t_insert(top, unescape(text)) @@ -619,7 +617,7 @@ function _M.basic_parse(s,all_text,html) t_insert(top, toclose) end end - i = j+1 + i = j+1 end local text = s_sub(s, i) if all_text or not s_find(text, "^%s*$") then |