Skip to content
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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ function parse (text, reviver, options) {
function safeParse (text, reviver) {
const { stackTraceLimit } = Error
Error.stackTraceLimit = 0
let result
try {
return _parse(text, reviver, { safe: true })
result = _parse(text, reviver, { safe: true })
} catch {
return undefined
result = undefined
} finally {
Error.stackTraceLimit = stackTraceLimit
}
return result
}

module.exports = parse
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"benchmark": "cd benchmarks && npm install && npm run all",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "nyc npm run test:unit && npm run test:typescript",
"test": "c8 --all --include index.js --check-coverage --100 npm run test:unit && npm run test:typescript",
"test:unit": "tape \"test/*.test.js\"",
"test:typescript": "tstyche",
"test:browser": "airtap test/*.test.js"
Expand Down Expand Up @@ -64,9 +64,9 @@
"devDependencies": {
"airtap": "^5.0.0",
"airtap-playwright": "^1.0.1",
"c8": "^12.0.0",
"eslint": "^9.17.0",
"neostandard": "^0.13.0",
"nyc": "^18.0.0",
"playwright": "^1.43.1",
"tape": "^5.7.5",
"tstyche": "^7.1.0"
Expand Down
4 changes: 3 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,13 @@ test('safeParse', t => {
t.end()
})

t.test('returns undefined on invalid object string', t => {
t.test('returns undefined on invalid object string and resets stackTraceLimit', t => {
Error.stackTraceLimit = 42
t.strictEqual(
j.safeParse('{"a": 5, "b": 6'),
undefined
)
t.strictEqual(Error.stackTraceLimit, 42)
t.end()
})

Expand Down