summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelog.md2
-rw-r--r--script/core/diagnostics/undefined-field.lua33
-rw-r--r--script/vm/compiler.lua35
3 files changed, 70 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md
index fcc471ce..590d38ee 100644
--- a/changelog.md
+++ b/changelog.md
@@ -2,6 +2,8 @@
## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
+* `NEW` using `enum (partial)`, it suggests all fields with the same `enum` type rather than just the fields from the current table.
+* `NEW` When using `enum["<key>" or <index>]`, undefined fields will raise an 'undefined' error.
* `FIX` Renaming files in the directory leads to the auto-correction in "require" adding extra characters.
## 3.10.4
diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua
index ab5d4d54..edb65909 100644
--- a/script/core/diagnostics/undefined-field.lua
+++ b/script/core/diagnostics/undefined-field.lua
@@ -52,6 +52,39 @@ return function (uri, callback)
}
end
end
+ ---@async
+ local function checkUndefinedFieldByIndexEnum(src)
+ await.delay()
+ local isEnum = false
+ for _, node in ipairs(vm.compileNode(src.node)) do
+ local docs = node.bindDocs
+ if docs then
+ for _, doc in ipairs(docs) do
+ if doc.type == "doc.enum" then
+ isEnum = true
+ break
+ end
+ end
+ end
+ end
+ if not isEnum then
+ return
+ end
+ if vm.hasDef(src) then
+ return
+ end
+ local keyName = guide.getKeyName(src)
+ if not keyName then
+ return
+ end
+ local message = lang.script('DIAG_UNDEF_FIELD', guide.getKeyName(src))
+ callback {
+ start = src.index.start,
+ finish = src.index.finish,
+ message = message,
+ }
+ end
guide.eachSourceType(ast.ast, 'getfield', checkUndefinedField)
guide.eachSourceType(ast.ast, 'getmethod', checkUndefinedField)
+ guide.eachSourceType(ast.ast, 'getindex', checkUndefinedFieldByIndexEnum)
end
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 54390450..e0cb54c7 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -189,6 +189,41 @@ local searchFieldSwitch = util.switch()
end
end
end
+ local docs = source.bindDocs
+ if docs then
+ for _, doc in ipairs(docs) do
+ if doc.type == 'doc.enum' then
+ if not vm.docHasAttr(doc, 'partial') then
+ return
+ end
+ for _, def in ipairs(vm.getDefs(doc)) do
+ if def.type ~= 'doc.enum' then
+ goto CONTINUE
+ end
+ local tbl = def.bindSource
+ if not tbl then
+ return
+ end
+ for _, field in ipairs(tbl) do
+ if field.type == 'tablefield'
+ or field.type == 'tableindex' then
+ if not field.value then
+ goto CONTINUE
+ end
+ local fieldKey = guide.getKeyName(field)
+ if key == vm.ANY
+ or key == fieldKey then
+ hasFiled = true
+ pushResult(field)
+ end
+ ::CONTINUE::
+ end
+ end
+ ::CONTINUE::
+ end
+ end
+ end
+ end
end)
: case 'string'
: case 'doc.type.string'