fix(inspect): escape identifiers that are lua keywords (#19898)

A lua keyword is not a valid table identifier
This commit is contained in:
Simon Wachter 2022-08-23 13:02:55 +02:00 committed by GitHub
parent df4709ddf6
commit e892b7b383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -89,8 +89,38 @@ local function escape(str)
) )
end 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) 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 end
local flr = math.floor local flr = math.floor