Skip to content

Commit

Permalink
feat: update to jsonrepair@3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Nov 1, 2023
1 parent 31d3401 commit 9ede6ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
38 changes: 22 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"immutable-json-patch": "^5.1.3",
"jmespath": "^0.16.0",
"json-source-map": "^0.6.1",
"jsonrepair": "^3.2.4",
"jsonrepair": "^3.4.0",
"lodash-es": "^4.17.21",
"memoize-one": "^6.0.0",
"natural-compare-lite": "^1.4.0",
Expand Down Expand Up @@ -131,7 +131,7 @@
"eslint-plugin-svelte": "2.34.0",
"husky": "8.0.3",
"jsdom": "22.1.0",
"lossless-json": "2.0.11",
"lossless-json": "3.0.0",
"npm-run-all": "4.1.5",
"prettier": "3.0.3",
"prettier-plugin-svelte": "3.0.3",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/logic/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ describe('validation', () => {
})

test('should validateText with a non-repairable parse error', () => {
const invalidText = '{\n "name": "Joe" ]'
const invalidText = '{\n "name": "Joe" }[]'

deepStrictEqual(validateText(invalidText, null, LosslessJSONParser, JSON), {
isRepairable: false,
parseError: {
column: 16,
column: 17,
line: 1,
message: "Comma ',' expected after value but got ']' at line 2 column 17",
position: 18
message: "Expected end of input but got '[' at line 2 column 18",
position: 19
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/jsonUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ describe('jsonUtils', () => {
describe('parseAndRepairOrUndefined', () => {
test('repair partial JSON', () => {
expect(parseAndRepairOrUndefined('[1,2', JSON)).toEqual([1, 2])
expect(parseAndRepairOrUndefined('[1,2}', JSON)).toEqual(undefined)
expect(parseAndRepairOrUndefined('[1,2][]', JSON)).toEqual(undefined)
expect(parseAndRepairOrUndefined('hello world', JSON)).toEqual('hello world')
// expect(parseAndRepairOrUndefined('0123', JSON)).toEqual('0123') // FIXME
})
Expand Down
10 changes: 3 additions & 7 deletions src/lib/utils/jsonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import type {
TextLocation
} from '../types'
import { int } from './numberUtils.js'
import type { JavaScriptValue } from 'lossless-json'

/**
* Parse the JSON. if this fails, try to repair and parse.
* Throws an exception when the JSON is invalid and could not be parsed.
*/
export function parseAndRepair(jsonText: string, parser: JSONParser): JavaScriptValue {
export function parseAndRepair(jsonText: string, parser: JSONParser): unknown {
try {
return parser.parse(jsonText)
} catch (err) {
Expand All @@ -37,7 +36,7 @@ export function parseAndRepair(jsonText: string, parser: JSONParser): JavaScript
export function parseAndRepairOrUndefined(
partialJson: string,
parser: JSONParser
): JavaScriptValue | undefined {
): unknown | undefined {
try {
return parseAndRepair(partialJson, parser)
} catch (err) {
Expand All @@ -46,10 +45,7 @@ export function parseAndRepairOrUndefined(
}

// TODO: deduplicate the logic in repairPartialJson and parseAndRepairPartialJson ?
export function parsePartialJson(
partialJson: string,
parse: (text: string) => JavaScriptValue
): JavaScriptValue {
export function parsePartialJson(partialJson: string, parse: (text: string) => unknown): unknown {
// for now: dumb brute force approach: simply try out a few things...

// remove trailing comma
Expand Down

0 comments on commit 9ede6ea

Please sign in to comment.