From 1ced9aafc00c10992bab8bd3f1b6b1397f05b7b9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 19 Dec 2023 00:33:38 -0500 Subject: Make JSON.parse usage safer (#373) * Make JSON.parse usage safer * Fix any type * Add readResponseJson * Use readResponseJson * Additional updates * Rename files * Add types --- types/ext/api.d.ts | 2 +- types/ext/audio-downloader.d.ts | 10 ++++++ types/ext/cross-frame-api.d.ts | 13 ++++++++ types/test/anki-note-builder.d.ts | 41 ------------------------ types/test/dom-text-scanner.d.ts | 32 +++++++++++++++++++ types/test/translator.d.ts | 66 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 42 deletions(-) delete mode 100644 types/test/anki-note-builder.d.ts create mode 100644 types/test/dom-text-scanner.d.ts create mode 100644 types/test/translator.d.ts (limited to 'types') diff --git a/types/ext/api.d.ts b/types/ext/api.d.ts index 6b7b4b19..2d78cc06 100644 --- a/types/ext/api.d.ts +++ b/types/ext/api.d.ts @@ -467,6 +467,6 @@ export type RequestBackendReadySignalResult = boolean; export type CreateActionPortDetails = Record; export type CreateActionPortResult = { - name: string; + name: 'action-port'; id: string; }; diff --git a/types/ext/audio-downloader.d.ts b/types/ext/audio-downloader.d.ts index b8e812f8..dfda8cb9 100644 --- a/types/ext/audio-downloader.d.ts +++ b/types/ext/audio-downloader.d.ts @@ -42,3 +42,13 @@ export type AudioBinaryBase64 = { data: string; contentType: string | null; }; + +export type CustomAudioList = { + type: 'audioSourceList'; + audioSources: CustomAudioListSource[]; +}; + +export type CustomAudioListSource = { + url: string; + name?: string; +}; diff --git a/types/ext/cross-frame-api.d.ts b/types/ext/cross-frame-api.d.ts index 88ce59a7..e31079b7 100644 --- a/types/ext/cross-frame-api.d.ts +++ b/types/ext/cross-frame-api.d.ts @@ -52,3 +52,16 @@ export type Invocation = { ack: boolean; timer: Core.Timeout | null; }; + +export type PortDetails = CrossFrameCommunicationPortDetails | ActionPortDetails; + +export type CrossFrameCommunicationPortDetails = { + name: 'cross-frame-communication-port'; + otherTabId: number; + otherFrameId: number; +}; + +export type ActionPortDetails = { + name: 'action-port'; + id: string; +}; diff --git a/types/test/anki-note-builder.d.ts b/types/test/anki-note-builder.d.ts deleted file mode 100644 index 0ccb25e9..00000000 --- a/types/test/anki-note-builder.d.ts +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2023 Yomitan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import type {OptionsPresetObject} from 'dev/vm'; -import type {FindTermsMode} from 'translator'; - -export type TranslatorTestInputs = { - optionsPresets: OptionsPresetObject; - tests: TestInput[]; -}; - -export type TestInput = TestInputFindKanji | TestInputFindTerm; - -export type TestInputFindKanji = { - func: 'findKanji'; - name: string; - text: string; - options: string; -}; - -export type TestInputFindTerm = { - func: 'findTerms'; - name: string; - mode: FindTermsMode; - text: string; - options: string; -}; diff --git a/types/test/dom-text-scanner.d.ts b/types/test/dom-text-scanner.d.ts new file mode 100644 index 00000000..362a7eb5 --- /dev/null +++ b/types/test/dom-text-scanner.d.ts @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Yomitan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +export type TestData = { + node: string; + offset: number; + length: number; + forcePreserveWhitespace?: boolean; + generateLayoutContent?: boolean; + reversible?: boolean; + expected: { + node: string; + offset: number; + content: string; + remainder?: number; + }; +}; + diff --git a/types/test/translator.d.ts b/types/test/translator.d.ts new file mode 100644 index 00000000..3e4c8b9d --- /dev/null +++ b/types/test/translator.d.ts @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2023 Yomitan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import type {OptionsPresetObject} from 'dev/vm'; +import type {FindTermsMode} from 'translator'; +import type {DictionaryEntry} from 'dictionary'; +import type {NoteData} from 'anki-templates'; +import type {NoteFields} from 'anki'; + +export type TranslatorTestInputs = { + optionsPresets: OptionsPresetObject; + tests: TestInput[]; +}; + +export type TestInput = TestInputFindKanji | TestInputFindTerm; + +export type TestInputFindKanji = { + func: 'findKanji'; + name: string; + text: string; + options: string; +}; + +export type TestInputFindTerm = { + func: 'findTerms'; + name: string; + mode: FindTermsMode; + text: string; + options: string; +}; + +export type TranslatorTestResults = TranslatorTestResult[]; + +export type TranslatorTestResult = { + name: string; + originalTextLength?: number; + dictionaryEntries: DictionaryEntry[]; +}; + +export type TranslatorTestNoteDataResults = TranslatorTestNoteDataResult[]; + +export type TranslatorTestNoteDataResult = { + name: string; + noteDataList: NoteData[]; +}; + +export type AnkiNoteBuilderTestResults = AnkiNoteBuilderTestResult[]; + +export type AnkiNoteBuilderTestResult = { + name: string; + results: NoteFields[] | null; +}; -- cgit v1.2.3