diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-02-24 23:47:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-25 04:47:57 +0000 |
commit | 73169f06dff767020718a5715eba97d3575ba7e1 (patch) | |
tree | 99d458f9d2ca74e67dbb4bccd148ef549f7ce2cf /ext/js/templates/sandbox | |
parent | a21948daf6210f67955ae4f98a81e21b8cf9f1f2 (diff) |
Turn on @typescript-eslint/no-unsafe-argument (#728)24.2.26.0
Diffstat (limited to 'ext/js/templates/sandbox')
-rw-r--r-- | ext/js/templates/sandbox/anki-template-renderer.js | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index ad2b0042..d2bb25d5 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -674,6 +674,7 @@ export class AnkiTemplateRenderer { */ _formatGlossary(args, _context, options) { const [dictionary, content] = /** @type {[dictionary: string, content: import('dictionary-data').TermGlossaryContent]} */ (args); + /** @type {import('anki-templates').NoteData} */ const data = options.data.root; if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); } if (!(typeof content === 'object' && content !== null)) { return ''; } @@ -713,31 +714,42 @@ export class AnkiTemplateRenderer { * @type {import('template-renderer').HelperFunction<boolean>} */ _hasMedia(args, _context, options) { - return this._mediaProvider.hasMedia(options.data.root, args, options.hash); + /** @type {import('anki-templates').NoteData} */ + const data = options.data.root; + return this._mediaProvider.hasMedia(data, args, options.hash); } /** * @type {import('template-renderer').HelperFunction<?string>} */ _getMedia(args, _context, options) { - return this._mediaProvider.getMedia(options.data.root, args, options.hash); + /** @type {import('anki-templates').NoteData} */ + const data = options.data.root; + return this._mediaProvider.getMedia(data, args, options.hash); } /** * @type {import('template-renderer').HelperFunction<string>} */ _pronunciation(_args, _context, options) { - let {format, reading, downstepPosition, nasalPositions, devoicePositions} = options.hash; - - if (typeof reading !== 'string' || reading.length === 0) { return ''; } - if (typeof downstepPosition !== 'number') { return ''; } - if (!Array.isArray(nasalPositions)) { nasalPositions = []; } - if (!Array.isArray(devoicePositions)) { devoicePositions = []; } + const {format, reading, downstepPosition} = options.hash; + + if ( + typeof reading !== 'string' || + reading.length === 0 || + typeof downstepPosition !== 'number' + ) { + return ''; + } const morae = getKanaMorae(reading); switch (format) { case 'text': + { + const nasalPositions = this._getValidNumberArray(options.hash.nasalPositions); + const devoicePositions = this._getValidNumberArray(options.hash.devoicePositions); return this._getPronunciationHtml(createPronunciationText(morae, downstepPosition, nasalPositions, devoicePositions)); + } case 'graph': return this._getPronunciationHtml(createPronunciationGraph(morae, downstepPosition)); case 'position': @@ -748,6 +760,20 @@ export class AnkiTemplateRenderer { } /** + * @param {unknown} value + * @returns {number[]} + */ + _getValidNumberArray(value) { + const result = []; + if (Array.isArray(value)) { + for (const item of value) { + if (typeof item === 'number') { result.push(item); } + } + } + return result; + } + + /** * @type {import('template-renderer').HelperFunction<string>} */ _hiragana(args, context, options) { |