diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-12-20 00:47:15 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-20 05:47:15 +0000 |
commit | 8b943cc97fab890085448122e7c13dd035d0e238 (patch) | |
tree | a7a749a44771c6a82b1b72bb35cc0c81d57ddb54 /types | |
parent | b13fbd47941fc20cf623871396e34a6dfe9b4dba (diff) |
JSON validation (#394)
* Set up JSON testing
* Add schema validation
* Use parseJson
* Finish types
* Disambiguate ext/json-schema from node dependency with the same name
* Add support for specifying the jsconfig file
* Don't expose types
* Update types
* Use dictionary map type
* Fix types
* Fix AJV warnings
* Move types
* Move anb rename file
* Move common mocks
* Simplify types
Diffstat (limited to 'types')
-rw-r--r-- | types/dev/manifest.d.ts | 2 | ||||
-rw-r--r-- | types/ext/dictionary-data.d.ts | 20 | ||||
-rw-r--r-- | types/ext/structured-content.d.ts | 8 | ||||
-rw-r--r-- | types/ext/translation.d.ts | 2 | ||||
-rw-r--r-- | types/test/json.d.ts | 42 | ||||
-rw-r--r-- | types/test/mocks.d.ts (renamed from types/dev/vm.d.ts) | 14 | ||||
-rw-r--r-- | types/test/translator.d.ts | 45 |
7 files changed, 108 insertions, 25 deletions
diff --git a/types/dev/manifest.d.ts b/types/dev/manifest.d.ts index e455208f..af475ee9 100644 --- a/types/dev/manifest.d.ts +++ b/types/dev/manifest.d.ts @@ -30,7 +30,7 @@ export type ManifestVariant = { fileName?: string; fileCopies?: string[]; excludeFiles?: string[]; - modifications: Modification[]; + modifications?: Modification[]; }; export type Modification = ( diff --git a/types/ext/dictionary-data.d.ts b/types/ext/dictionary-data.d.ts index f594f913..b194c190 100644 --- a/types/ext/dictionary-data.d.ts +++ b/types/ext/dictionary-data.d.ts @@ -44,6 +44,8 @@ export type IndexTag = { score: number; }; +export type TermV1Array = TermV1[]; + export type TermV1 = [ expression: string, reading: string, @@ -53,6 +55,8 @@ export type TermV1 = [ ...glossary: string[], ]; +export type TermV3Array = TermV3[]; + export type TermV3 = [ expression: string, reading: string, @@ -64,6 +68,8 @@ export type TermV3 = [ termTags: string, ]; +export type KanjiV1Array = KanjiV1[]; + export type KanjiV1 = [ character: string, onyomi: string, @@ -72,6 +78,8 @@ export type KanjiV1 = [ ...meanings: string[], ]; +export type KanjiV3Array = KanjiV3[]; + export type KanjiV3 = [ character: string, onyomi: string, @@ -93,7 +101,13 @@ export type TermGlossaryText = {type: 'text', text: string}; export type TermGlossaryImage = {type: 'image'} & TermImage; export type TermGlossaryStructuredContent = {type: 'structured-content', content: StructuredContent.Content}; -export type TermImage = StructuredContent.ImageElementBase; +export type TermImage = StructuredContent.ImageElementBase & { + // Compatibility properties + verticalAlign?: undefined; + sizeUnits?: undefined; +}; + +export type TagArray = Tag[]; export type Tag = [ name: string, @@ -109,6 +123,8 @@ export type GenericFrequencyData = string | number | { reading?: undefined; // Used for type disambiguation, field does not actually exist }; +export type TermMetaArray = TermMeta[]; + export type TermMeta = TermMetaFrequency | TermMetaPitch; export type TermMetaFrequencyDataWithReading = { @@ -138,6 +154,8 @@ export type TermMetaPitch = [ data: TermMetaPitchData, ]; +export type KanjiMetaArray = KanjiMeta[]; + export type KanjiMeta = KanjiMetaFrequency; export type KanjiMetaFrequency = [ diff --git a/types/ext/structured-content.d.ts b/types/ext/structured-content.d.ts index 09755c88..572b827a 100644 --- a/types/ext/structured-content.d.ts +++ b/types/ext/structured-content.d.ts @@ -161,14 +161,6 @@ export type ImageElementBase = { * Whether or not the image can be collapsed. */ collapsible?: boolean; - /** - * This property is not defined on the base class. - */ - verticalAlign?: undefined; - /** - * This property is not defined on the base class. - */ - sizeUnits?: undefined; }; export type ImageElement = ImageElementBase & { diff --git a/types/ext/translation.d.ts b/types/ext/translation.d.ts index 3adfc673..595a5a35 100644 --- a/types/ext/translation.d.ts +++ b/types/ext/translation.d.ts @@ -28,7 +28,7 @@ export type FindKanjiOptions = { * The mapping of dictionaries to search for kanji in. * The key is the dictionary name. */ - enabledDictionaryMap: Map<string, FindKanjiDictionary>; + enabledDictionaryMap: KanjiEnabledDictionaryMap; /** * Whether or not non-Japanese characters should be searched. */ diff --git a/types/test/json.d.ts b/types/test/json.d.ts new file mode 100644 index 00000000..9ae21b0f --- /dev/null +++ b/types/test/json.d.ts @@ -0,0 +1,42 @@ +/* + * 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 <https://www.gnu.org/licenses/>. + */ + +import type {Schema} from 'ajv'; + +export type JsonInfo = { + files: JsonFileInfo[]; +}; + +export type JsonFileInfo = JsonFileIgnoreInfo | JsonFileParseInfo; + +export type JsonFileIgnoreInfo = { + path: string; + ignore: true; +}; + +export type JsonFileParseInfo = { + path: string; + ignore?: undefined; + typeFile: string; + type: string; + schema?: string; + jsconfig?: JsconfigType; +}; + +export type AjvSchema = Schema; + +export type JsconfigType = 'main' | 'dev' | 'test'; diff --git a/types/dev/vm.d.ts b/types/test/mocks.d.ts index 3eb0949f..13b56ac6 100644 --- a/types/dev/vm.d.ts +++ b/types/test/mocks.d.ts @@ -15,24 +15,18 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import type * as Translation from '../ext/translation'; - -export type PseudoChrome = { +export type ChromeMock = { runtime: { getURL(path: string): string; }; }; -export type PseudoFetchResponse = { +export type FetchMock = (url: string) => Promise<FetchResponseMock>; + +export type FetchResponseMock = { ok: boolean; status: number; statusText: string; text(): Promise<string>; json(): Promise<unknown>; }; - -export type OptionsPresetObject = { - [key: string]: OptionsPreset; -}; - -export type OptionsPreset = Partial<Translation.FindTermsOptions>; diff --git a/types/test/translator.d.ts b/types/test/translator.d.ts index 3e4c8b9d..7dff116e 100644 --- a/types/test/translator.d.ts +++ b/types/test/translator.d.ts @@ -15,12 +15,49 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import type {OptionsPresetObject} from 'dev/vm'; +import type {FindTermsMatchType, FindTermsSortOrder, FindTermsVariantMode, FindTermsEmphaticSequencesMode, FindKanjiDictionary, FindTermDictionary} from '../ext/translation'; import type {FindTermsMode} from 'translator'; import type {DictionaryEntry} from 'dictionary'; import type {NoteData} from 'anki-templates'; import type {NoteFields} from 'anki'; +export type OptionsPresetObject = { + [key: string]: OptionsPreset; +}; + +export type OptionsList = string | (string | OptionsPreset)[]; + +export type OptionsPreset = FindKanjiOptionsPreset | FindTermsOptionsPreset; + +export type FindKanjiOptionsPreset = { + enabledDictionaryMap?: [key: string, value: FindKanjiDictionary][]; + removeNonJapaneseCharacters?: boolean; +}; + +export type FindTermsOptionsPreset = { + matchType?: FindTermsMatchType; + deinflect?: boolean; + mainDictionary?: string; + sortFrequencyDictionary?: string | null; + sortFrequencyDictionaryOrder?: FindTermsSortOrder; + removeNonJapaneseCharacters?: boolean; + convertHalfWidthCharacters?: FindTermsVariantMode; + convertNumericCharacters?: FindTermsVariantMode; + convertAlphabeticCharacters?: FindTermsVariantMode; + convertHiraganaToKatakana?: FindTermsVariantMode; + convertKatakanaToHiragana?: FindTermsVariantMode; + collapseEmphaticSequences?: FindTermsEmphaticSequencesMode; + textReplacements?: (FindTermsTextReplacement[] | null)[]; + enabledDictionaryMap?: [key: string, value: FindTermDictionary][]; + excludeDictionaryDefinitions?: string[] | null; +}; + +export type FindTermsTextReplacement = { + pattern: string; + flags: string; + replacement: string; +}; + export type TranslatorTestInputs = { optionsPresets: OptionsPresetObject; tests: TestInput[]; @@ -32,7 +69,7 @@ export type TestInputFindKanji = { func: 'findKanji'; name: string; text: string; - options: string; + options: OptionsList; }; export type TestInputFindTerm = { @@ -40,7 +77,7 @@ export type TestInputFindTerm = { name: string; mode: FindTermsMode; text: string; - options: string; + options: OptionsList; }; export type TranslatorTestResults = TranslatorTestResult[]; @@ -55,7 +92,7 @@ export type TranslatorTestNoteDataResults = TranslatorTestNoteDataResult[]; export type TranslatorTestNoteDataResult = { name: string; - noteDataList: NoteData[]; + noteDataList: Omit<NoteData, 'dictionaryEntry'>[] | null; }; export type AnkiNoteBuilderTestResults = AnkiNoteBuilderTestResult[]; |