Skip to content

fix: parse JSON string only from output #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
Session.vim
.direnv
.pre-commit-config.yaml
package-lock.json
61 changes: 59 additions & 2 deletions lua/neotest-playwright/playwright.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,41 @@ do
TypeError = createErrorClass(nil, "TypeError")
URIError = createErrorClass(nil, "URIError")
end

local __TS__Symbol, Symbol
do
local symbolMetatable = {__tostring = function(self)
return ("Symbol(" .. (self.description or "")) .. ")"
end}
function __TS__Symbol(description)
return setmetatable({description = description}, symbolMetatable)
end
Symbol = {
iterator = __TS__Symbol("Symbol.iterator"),
hasInstance = __TS__Symbol("Symbol.hasInstance"),
species = __TS__Symbol("Symbol.species"),
toStringTag = __TS__Symbol("Symbol.toStringTag")
}
end

local function __TS__InstanceOf(obj, classTbl)
if type(classTbl) ~= "table" then
error("Right-hand side of 'instanceof' is not an object", 0)
end
if classTbl[Symbol.hasInstance] ~= nil then
return not not classTbl[Symbol.hasInstance](classTbl, obj)
end
if type(obj) == "table" then
local luaClass = obj.constructor
while luaClass ~= nil do
if luaClass == classTbl then
return true
end
luaClass = luaClass.____super
end
end
return false
end
-- End of Lua Library inline imports
local ____exports = {}
local run
Expand Down Expand Up @@ -222,7 +257,29 @@ run = function(cmd)
emitError("No output from command: " .. cmd)
return
end
local decoded = vim.fn.json_decode(output)
return decoded
local jsonMatch = {string.match(output, "%b{}")}
if not jsonMatch then
emitError("Failed to parse JSON output: " .. output)
return
end
local jsonString = jsonMatch[1]
do
local function ____catch(err)
if __TS__InstanceOf(err, Error) then
emitError("Failed to decode JSON: " .. err.message)
end
return true
end
local ____try, ____hasReturned, ____returnValue = pcall(function()
local decoded = vim.fn.json_decode(jsonString)
return true, decoded
end)
if not ____try then
____hasReturned, ____returnValue = ____catch(____hasReturned)
end
if ____hasReturned then
return ____returnValue
end
end
end
return ____exports
19 changes: 17 additions & 2 deletions src/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,22 @@ const run = (cmd: string) => {
return;
}

const decoded = vim.fn.json_decode(output) as P.JSONReport;
const jsonMatch = string.match(output, "%b{}");

return decoded;
if (!jsonMatch) {
emitError(`Failed to parse JSON output: ${output}`);
return;
}

const jsonString = jsonMatch[0];

try {
const decoded = vim.fn.json_decode(jsonString) as P.JSONReport;
return decoded;
} catch (err) {
if (err instanceof Error) {
emitError(`Failed to decode JSON: ${err.message}`);
}
return;
}
};