diff options
-rw-r--r-- | script/core/hover/return.lua | 4 | ||||
-rw-r--r-- | script/core/hover/table.lua | 2 | ||||
-rw-r--r-- | script/parser/guide.lua | 51 | ||||
-rw-r--r-- | script/vm/getClass.lua | 10 |
4 files changed, 26 insertions, 41 deletions
diff --git a/script/core/hover/return.lua b/script/core/hover/return.lua index 3829dbed..0ad39970 100644 --- a/script/core/hover/return.lua +++ b/script/core/hover/return.lua @@ -66,12 +66,12 @@ local function asFunction(source) for _, value in ipairs(values) do if value.type then for tp in value.type:gmatch '[^|]+' do - types[#types+1] = tp + types[tp] = true end end end end - if #types > 0 or rtn[1] then + if next(types) or rtn[1] then local tp = mergeTypes(types) or 'any' if rtn[1].name then line[#line+1] = ('%s%s: %s'):format( diff --git a/script/core/hover/table.lua b/script/core/hover/table.lua index 58e64951..f46e722e 100644 --- a/script/core/hover/table.lua +++ b/script/core/hover/table.lua @@ -189,7 +189,7 @@ local function mergeTypes(types) for tp in tv:gmatch '[^|]+' do if not mark[tp] then mark[tp] = true - results[#results+1] = tp + results[tp] = true end end end diff --git a/script/parser/guide.lua b/script/parser/guide.lua index cbafe66a..4673b475 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -3160,23 +3160,22 @@ function m.allocInfer(o) end function m.mergeTypes(types) - local results = {} - local mark = {} - local hasAny - -- 这里把 any 去掉 - for i = 1, #types do - local tp = types[i] - if tp == 'any' then - hasAny = true - end - if not mark[tp] and tp ~= 'any' then - mark[tp] = true - results[#results+1] = tp - end - end - if #results == 0 then + local hasAny = types['any'] + + types['any'] = nil + + if not next(types) then return 'any' end + -- 同时包含 number 与 integer 时,去掉 integer + if types['number'] and types['integer'] then + types['integer'] = nil + end + + local results = {} + for tp in pairs(types) do + results[#results+1] = tp + end -- 只有显性的 nil 与 any 时,取 any if #results == 1 then if results[1] == 'nil' and hasAny then @@ -3185,15 +3184,7 @@ function m.mergeTypes(types) return results[1] end end - -- 同时包含 number 与 integer 时,去掉 integer - if mark['number'] and mark['integer'] then - for i = 1, #results do - if results[i] == 'integer' then - tableRemove(results, i) - break - end - end - end + tableSort(results, function (a, b) local sa = TypeSort[a] or 100 local sb = TypeSort[b] or 100 @@ -3203,6 +3194,7 @@ function m.mergeTypes(types) return sa < sb end end) + return tableConcat(results, '|') end @@ -3210,7 +3202,6 @@ function m.viewInferType(infers) if not infers then return 'any' end - local mark = {} local types = {} local hasDoc local hasDocTable @@ -3246,10 +3237,7 @@ function m.viewInferType(infers) if hasDocTable and tp == 'table' then goto CONTINUE end - if not mark[tp] then - types[#types+1] = tp - end - mark[tp] = true + types[tp] = true end ::CONTINUE:: end @@ -3260,10 +3248,7 @@ function m.viewInferType(infers) goto CONTINUE end local tp = infer.type or 'any' - if not mark[tp] then - types[#types+1] = tp - end - mark[tp] = true + types[tp] = true ::CONTINUE:: end end diff --git a/script/vm/getClass.lua b/script/vm/getClass.lua index c4e39523..b9ff8c60 100644 --- a/script/vm/getClass.lua +++ b/script/vm/getClass.lua @@ -15,7 +15,7 @@ end local function getClass(source, classes, depth, deep) local docClass = lookUpDocClass(source) if docClass then - classes[#classes+1] = docClass + classes[docClass] = true return end if depth > 3 then @@ -24,7 +24,7 @@ local function getClass(source, classes, depth, deep) local value = guide.getObjectValue(source) or source if not deep then if value and value.type == 'string' then - classes[#classes+1] = value[1] + classes[value[1]] = true end else for _, src in ipairs(vm.getDefFields(value)) do @@ -39,13 +39,13 @@ local function getClass(source, classes, depth, deep) or lkey == 'class' then local value = guide.getObjectValue(src) if value and value.type == 'string' then - classes[#classes+1] = value[1] + classes[value[1]] = true end end ::CONTINUE:: end end - if #classes ~= 0 then + if next(classes) then return end vm.eachMeta(source, function (mt) @@ -56,7 +56,7 @@ end function vm.getClass(source, deep) local classes = {} getClass(source, classes, 1, deep) - if #classes == 0 then + if not next(classes) then return nil end return guide.mergeTypes(classes) |