commit e892b7b3830f44bc8ab62e993bf07f7bf03d0029
parent df4709ddf6d1ed524adae9373ecb762b9db11814
Author: Simon Wachter <svvac@users.noreply.github.com>
Date: Tue, 23 Aug 2022 13:02:55 +0200
fix(inspect): escape identifiers that are lua keywords (#19898)
A lua keyword is not a valid table identifier
Diffstat:
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/runtime/lua/vim/inspect.lua b/runtime/lua/vim/inspect.lua
@@ -89,8 +89,38 @@ local function escape(str)
)
end
+-- List of lua keywords
+local luaKeywords = {
+ ['and'] = true,
+ ['break'] = true,
+ ['do'] = true,
+ ['else'] = true,
+ ['elseif'] = true,
+ ['end'] = true,
+ ['false'] = true,
+ ['for'] = true,
+ ['function'] = true,
+ ['goto'] = true,
+ ['if'] = true,
+ ['in'] = true,
+ ['local'] = true,
+ ['nil'] = true,
+ ['not'] = true,
+ ['or'] = true,
+ ['repeat'] = true,
+ ['return'] = true,
+ ['then'] = true,
+ ['true'] = true,
+ ['until'] = true,
+ ['while'] = true,
+}
+
local function isIdentifier(str)
- return type(str) == 'string' and not not str:match('^[_%a][_%a%d]*$')
+ return type(str) == 'string'
+ -- identifier must start with a letter and underscore, and be followed by letters, numbers, and underscores
+ and not not str:match('^[_%a][_%a%d]*$')
+ -- lua keywords are not valid identifiers
+ and not luaKeywords[str]
end
local flr = math.floor