Can you try to improve this function that removes comments from the lua file? Here is the function:
---@param code string
---@return string
local function removeComments(code)
    ---@type "code"|"comment"|"string"|"mstring"|"mcomment"
    local state = "code"
    local new_code = ""
    for i = 1, #code do
        local char = code:sub(i, i)
        if state == "code" then
            if (char == "-" and code:sub(i+1, i+1) == "-") then
                if char == "\"" then
                    state = "string"
                end
                if char == "-" and code:sub(i+1, i+1) == "-" then
                    state = "comment"
                end
                if char == "[" and code:sub(i+1, i+1) == "[" then
                    state = "mstring"
                end
            else
                new_code = new_code..char
            end
        elseif state == "comment" then
            if char == "\n" then
                state = "code"
            elseif char == "[" and code:sub(i+1, i+1) == "[" then
                state = "mcomment"
            end
        elseif state == "mcomment" then
            if char == "]" and code:sub(i+1, i+1) == "]" then
                state = "code"
            end
        elseif state == "string" or state == "mstring" then
            new_code = new_code..char
            if (char == "\"" and state == "string") or (char == "]" and code:sub(i-1, i-1) == "]" and state == "mstring") then
                state = "code"
            end
        end
    end
    return new_code
end