Skip to content

Commit

Permalink
chore: fix some TypeScript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Sep 20, 2023
1 parent 36d7934 commit 2790526
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
10 changes: 5 additions & 5 deletions src/lib/components/modals/JSONRepairModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import { onEscape } from '$lib/actions/onEscape.js'
import type { Context } from 'svelte-simple-modal'
export let text
export let onParse
export let onRepair
export let onApply
export let text: string
export let onParse: (text: string) => void
export let onRepair: (text: string) => string
export let onApply: (repairedText: string) => void
const { close } = getContext<Context>('simple-modal')
function handleApply(repairedText) {
function handleApply(repairedText: string) {
close()
onApply(repairedText)
}
Expand Down
30 changes: 14 additions & 16 deletions src/lib/components/modals/repair/JSONRepairComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,31 @@
export let text = ''
export let readOnly = false
export let onParse
export let onRepair
export let onChange = null
export let onApply
export let onCancel
export let onParse: (text: string) => void
export let onRepair: (text: string) => string
export let onChange: ((updatedText: string) => void) | null = null
export let onApply: (repairedText: string) => void
export let onCancel: () => void
const debug = createDebug('jsoneditor:JSONRepair')
let domJsonRepair
let domTextArea
let domTextArea: HTMLTextAreaElement
$: error = getErrorMessage(text)
$: repairable = isRepairable(text)
$: debug('error', error)
function getErrorMessage(jsonText) {
function getErrorMessage(jsonText: string) {
try {
onParse(jsonText)
return null
} catch (err) {
return normalizeJsonParseError(jsonText, err.message)
return normalizeJsonParseError(jsonText, (err as Error).message)
}
}
function isRepairable(jsonText) {
function isRepairable(jsonText: string) {
try {
onRepair(jsonText)
return true
Expand All @@ -60,10 +59,10 @@
}
}
function handleChange(event) {
function handleChange(event: Event & { currentTarget: EventTarget & HTMLTextAreaElement }) {
debug('handleChange')
const value = event.target.value
const value = (event.target as HTMLTextAreaElement).value
if (text === value) {
return
Expand Down Expand Up @@ -134,7 +133,7 @@
]
</script>

<div class="jse-json-repair-component" bind:this={domJsonRepair}>
<div class="jse-json-repair-component">
<Menu {items}>
<div slot="left" class="jse-info">Repair invalid JSON, then click apply</div>
</Menu>
Expand All @@ -155,14 +154,13 @@
{/if}
<textarea
bind:this={domTextArea}
value={text}
on:input={handleChange}
readonly={readOnly}
class="jse-json-text"
autocomplete="off"
autocapitalize="off"
spellcheck="false"
/>
spellcheck="false">{text}</textarea
>
</div>

<style src="./JSONRepairComponent.scss"></style>
44 changes: 26 additions & 18 deletions src/lib/components/modes/tablemode/TableMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
debug('onSortByHeader', newSortedColumn)
const rootPath = []
const rootPath: JSONPath = []
const direction = newSortedColumn.sortDirection === SortDirection.desc ? -1 : 1
const operations = sortJson(json, rootPath, newSortedColumn.path, direction)
handlePatch(operations, (patchedJson, patchedState) => {
Expand Down Expand Up @@ -399,7 +399,7 @@
textIsRepaired = false
parseError =
text !== undefined && text !== ''
? normalizeJsonParseError(text, err.message || err.toString())
? normalizeJsonParseError(text, (err as Error).message || String(err))
: undefined
}
}
Expand Down Expand Up @@ -540,7 +540,7 @@
newValidationErrors = [
{
path: [],
message: 'Failed to validate: ' + err.message,
message: 'Failed to validate: ' + (err as Error).message,
severity: ValidationSeverity.warning
}
]
Expand Down Expand Up @@ -665,18 +665,20 @@
// make sure we cannot send an invalid contents like having both
// json and text defined, or having none defined
if (text !== undefined) {
const content = { text, json: undefined }
onChange(content, previousContent, {
contentErrors: validate(),
patchResult
})
} else if (json !== undefined) {
const content = { text: undefined, json }
onChange(content, previousContent, {
contentErrors: validate(),
patchResult
})
if (onChange) {
if (text !== undefined) {
const content = { text, json: undefined }
onChange(content, previousContent, {
contentErrors: validate(),
patchResult
})
} else if (json !== undefined) {
const content = { text: undefined, json }
onChange(content, previousContent, {
contentErrors: validate(),
patchResult
})
}
}
}
Expand Down Expand Up @@ -963,7 +965,7 @@
})
}
function handleContextMenu(event) {
function handleContextMenu(event: MouseEvent) {
if (readOnly || isEditingSelection(documentState.selection)) {
return
}
Expand Down Expand Up @@ -1081,6 +1083,10 @@
async function handleParsePastedJson() {
debug('apply pasted json', pastedJson)
if (!pastedJson) {
return
}
const { path, contents } = pastedJson
// exit edit mode
Expand Down Expand Up @@ -1439,7 +1445,9 @@
text = updatedText
textIsRepaired = false
parseError =
text !== '' ? normalizeJsonParseError(text, err.message || err.toString()) : undefined
text !== ''
? normalizeJsonParseError(text, (err as Error).message || String(err))
: undefined
}
}
Expand Down Expand Up @@ -1593,7 +1601,7 @@
}
function handleSortAll() {
const rootPath = []
const rootPath: JSONPath = []
openSortModal(rootPath)
}
Expand Down
12 changes: 11 additions & 1 deletion src/lib/utils/jsonUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
parsePartialJson,
toJSONContent,
toTextContent,
validateContentType
validateContentType,
parseAndRepairOrUndefined
} from './jsonUtils.js'

const LosslessJSONParser = { parse, stringify }
Expand Down Expand Up @@ -421,4 +422,13 @@ describe('jsonUtils', () => {
expect(needsFormatting('{"a":1, "b":2}')).toBe(true)
})
})

describe('parseAndRepairOrUndefined', () => {
test('repair partial JSON', () => {
expect(parseAndRepairOrUndefined('[1,2', JSON)).toEqual([1, 2])
expect(parseAndRepairOrUndefined('[1,2}', JSON)).toEqual(undefined)
expect(parseAndRepairOrUndefined('hello world', JSON)).toEqual('hello world')
// expect(parseAndRepairOrUndefined('0123', JSON)).toEqual('0123') // FIXME
})
})
})

0 comments on commit 2790526

Please sign in to comment.