From 229f04ba358b3485fa7952088e03dad9e651bb23 Mon Sep 17 00:00:00 2001 From: Cashew <52880648+Scrub1492@users.noreply.github.com> Date: Wed, 20 Dec 2023 21:36:27 +0900 Subject: rename variables (#409) * rename variables * add comment to ambiguous variable * rename variables * add comments * rename functions --- dev/manifest-util.js | 1 + ext/js/app/frontend.js | 8 ++--- ext/js/app/popup.js | 8 ++--- ext/js/background/backend.js | 40 +++++++++++----------- ext/js/background/offscreen-proxy.js | 12 +++---- ext/js/comm/mecab.js | 10 +++--- ext/js/data/sandbox/string-util.js | 8 ++--- ext/js/display/display-generator.js | 6 ++-- ext/js/display/option-toggle-hotkey-handler.js | 18 +++++----- ext/js/display/query-parser.js | 1 + ext/js/language/translator.js | 8 ++--- ext/js/pages/settings/anki-controller.js | 20 ++++++----- ext/js/templates/sandbox/anki-template-renderer.js | 8 ++--- 13 files changed, 76 insertions(+), 72 deletions(-) diff --git a/dev/manifest-util.js b/dev/manifest-util.js index e4487035..42872a40 100644 --- a/dev/manifest-util.js +++ b/dev/manifest-util.js @@ -127,6 +127,7 @@ export class ManifestUtil { _applyModifications(manifest, modifications) { if (Array.isArray(modifications)) { for (const modification of modifications) { + // rename to path2 to avoid clashing with imported `node:path` module. const {action, path: path2} = modification; switch (action) { case 'set': diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index 8c11775a..c8b63813 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -964,7 +964,7 @@ export class Frontend { _prepareSiteSpecific() { switch (location.hostname.toLowerCase()) { case 'docs.google.com': - this._prepareGoogleDocs(); + this._prepareGoogleDocsWrapper(); break; } } @@ -972,18 +972,18 @@ export class Frontend { /** * @returns {Promise} */ - async _prepareGoogleDocs() { + async _prepareGoogleDocsWrapper() { if (typeof GoogleDocsUtil !== 'undefined') { return; } await yomitan.api.loadExtensionScripts([ '/js/accessibility/google-docs-util.js' ]); - this._prepareGoogleDocs2(); + this._prepareGoogleDocs(); } /** * @returns {Promise} */ - async _prepareGoogleDocs2() { + async _prepareGoogleDocs() { if (typeof GoogleDocsUtil === 'undefined') { return; } DocumentUtil.registerGetRangeFromPointHandler(GoogleDocsUtil.getRangeFromPoint.bind(GoogleDocsUtil)); } diff --git a/ext/js/app/popup.js b/ext/js/app/popup.js index 45604a55..a0712604 100644 --- a/ext/js/app/popup.js +++ b/ext/js/app/popup.js @@ -417,7 +417,7 @@ export class Popup extends EventDispatcher { _inject() { let injectPromise = this._injectPromise; if (injectPromise === null) { - injectPromise = this._injectInner1(); + injectPromise = this._injectInnerWrapper(); this._injectPromise = injectPromise; injectPromise.then( () => { @@ -433,9 +433,9 @@ export class Popup extends EventDispatcher { /** * @returns {Promise} */ - async _injectInner1() { + async _injectInnerWrapper() { try { - await this._injectInner2(); + await this._injectInner(); return true; } catch (e) { this._resetFrame(); @@ -447,7 +447,7 @@ export class Popup extends EventDispatcher { /** * @returns {Promise} */ - async _injectInner2() { + async _injectInner() { if (this._optionsContext === null) { throw new Error('Options not initialized'); } diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 99aa0c5d..d62c852b 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -331,7 +331,7 @@ export class Backend { text = text.substring(0, maximumSearchLength); } try { - const {tab, created} = await this._getOrCreateSearchPopup(); + const {tab, created} = await this._getOrCreateSearchPopupWrapper(); const {id} = tab; if (typeof id !== 'number') { throw new Error('Tab does not have an id'); @@ -799,7 +799,7 @@ export class Backend { /** @type {import('api').Handler} */ async _onApiGetOrCreateSearchPopup({focus = false, text}) { - const {tab, created} = await this._getOrCreateSearchPopup(); + const {tab, created} = await this._getOrCreateSearchPopupWrapper(); if (focus === true || (focus === 'ifCreated' && created)) { await this._focusTab(tab); } @@ -960,18 +960,18 @@ export class Backend { const queryParams = {}; if (query.length > 0) { queryParams.query = query; } const queryString = new URLSearchParams(queryParams).toString(); - let url = baseUrl; + let queryUrl = baseUrl; if (queryString.length > 0) { - url += `?${queryString}`; + queryUrl += `?${queryString}`; } /** @type {import('backend').FindTabsPredicate} */ - const predicate = ({url: url2}) => { - if (url2 === null || !url2.startsWith(baseUrl)) { return false; } - const parsedUrl = new URL(url2); - const baseUrl2 = `${parsedUrl.origin}${parsedUrl.pathname}`; - const mode2 = parsedUrl.searchParams.get('mode'); - return baseUrl2 === baseUrl && (mode2 === mode || (!mode2 && mode === 'existingOrNewTab')); + const predicate = ({url}) => { + if (url === null || !url.startsWith(baseUrl)) { return false; } + const parsedUrl = new URL(url); + const parsedBaseUrl = `${parsedUrl.origin}${parsedUrl.pathname}`; + const parsedMode = parsedUrl.searchParams.get('mode'); + return parsedBaseUrl === baseUrl && (parsedMode === mode || (!parsedMode && mode === 'existingOrNewTab')); }; const openInTab = async () => { @@ -997,10 +997,10 @@ export class Backend { } catch (e) { // NOP } - await this._createTab(url); + await this._createTab(queryUrl); return; case 'newTab': - await this._createTab(url); + await this._createTab(queryUrl); return; } } @@ -1072,9 +1072,9 @@ export class Backend { /** * @returns {Promise<{tab: chrome.tabs.Tab, created: boolean}>} */ - _getOrCreateSearchPopup() { + _getOrCreateSearchPopupWrapper() { if (this._searchPopupTabCreatePromise === null) { - const promise = this._getOrCreateSearchPopup2(); + const promise = this._getOrCreateSearchPopup(); this._searchPopupTabCreatePromise = promise; promise.then(() => { this._searchPopupTabCreatePromise = null; }); } @@ -1084,7 +1084,7 @@ export class Backend { /** * @returns {Promise<{tab: chrome.tabs.Tab, created: boolean}>} */ - async _getOrCreateSearchPopup2() { + async _getOrCreateSearchPopup() { // Use existing tab const baseUrl = chrome.runtime.getURL('/search.html'); /** @@ -2418,13 +2418,13 @@ export class Backend { if (error instanceof ExtensionError && typeof error.data === 'object' && error.data !== null) { const {errors} = /** @type {import('core').SerializableObject} */ (error.data); if (Array.isArray(errors)) { - for (const error2 of errors) { - if (!(error2 instanceof Error)) { continue; } - if (error2.name === 'AbortError') { + for (const errorDetail of errors) { + if (!(errorDetail instanceof Error)) { continue; } + if (errorDetail.name === 'AbortError') { return this._createAudioDownloadError('Audio download was cancelled due to an idle timeout', 'audio-download-idle-timeout', errors); } - if (!(error2 instanceof ExtensionError)) { continue; } - const {data} = error2; + if (!(errorDetail instanceof ExtensionError)) { continue; } + const {data} = errorDetail; if (!(typeof data === 'object' && data !== null)) { continue; } const {details} = /** @type {import('core').SerializableObject} */ (data); if (!(typeof details === 'object' && details !== null)) { continue; } diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js index 63f619fa..dfd342b4 100644 --- a/ext/js/background/offscreen-proxy.js +++ b/ext/js/background/offscreen-proxy.js @@ -95,16 +95,16 @@ export class OffscreenProxy { * @throws {Error} */ _getMessageResponseResult(response) { - const error = chrome.runtime.lastError; - if (error) { - throw new Error(error.message); + const runtimeError = chrome.runtime.lastError; + if (typeof runtimeError !== 'undefined') { + throw new Error(runtimeError.message); } if (!isObject(response)) { throw new Error('Offscreen document did not respond'); } - const error2 = response.error; - if (error2) { - throw ExtensionError.deserialize(error2); + const responseError = response.error; + if (responseError) { + throw ExtensionError.deserialize(responseError); } return response.result; } diff --git a/ext/js/comm/mecab.js b/ext/js/comm/mecab.js index 0a87463b..6d0f3f40 100644 --- a/ext/js/comm/mecab.js +++ b/ext/js/comm/mecab.js @@ -105,7 +105,7 @@ export class Mecab { */ async getVersion() { try { - await this._setupPort(); + await this._setupPortWrapper(); } catch (e) { // NOP } @@ -132,7 +132,7 @@ export class Mecab { * @returns {Promise} A collection of parsing results of the text. */ async parseText(text) { - await this._setupPort(); + await this._setupPortWrapper(); const rawResults = await this._invoke('parse_text', {text}); // Note: The format of rawResults is not validated return this._convertParseTextResults(/** @type {import('mecab').ParseResultRaw} */ (rawResults)); @@ -225,12 +225,12 @@ export class Mecab { /** * @returns {Promise} */ - async _setupPort() { + async _setupPortWrapper() { if (!this._enabled) { throw new Error('MeCab not enabled'); } if (this._setupPortPromise === null) { - this._setupPortPromise = this._setupPort2(); + this._setupPortPromise = this._setupPort(); } try { await this._setupPortPromise; @@ -242,7 +242,7 @@ export class Mecab { /** * @returns {Promise} */ - async _setupPort2() { + async _setupPort() { const port = chrome.runtime.connectNative('yomitan_mecab'); this._eventListeners.addListener(port.onMessage, this._onMessage.bind(this)); this._eventListeners.addListener(port.onDisconnect, this._onDisconnect.bind(this)); diff --git a/ext/js/data/sandbox/string-util.js b/ext/js/data/sandbox/string-util.js index 096ecffe..3d810ffb 100644 --- a/ext/js/data/sandbox/string-util.js +++ b/ext/js/data/sandbox/string-util.js @@ -35,10 +35,10 @@ export class StringUtil { result += char; if (++position >= textLength) { break; } const charCode = char.charCodeAt(0); - if (charCode >= 0xd800 && charCode < 0xdc00) { + if (charCode >= 0xd800 && charCode < 0xdc00) { // charCode is a high surrogate code const char2 = text[position]; const charCode2 = char2.charCodeAt(0); - if (charCode2 >= 0xdc00 && charCode2 < 0xe000) { + if (charCode2 >= 0xdc00 && charCode2 < 0xe000) { // charCode2 is a low surrogate code result += char2; if (++position >= textLength) { break; } } @@ -61,10 +61,10 @@ export class StringUtil { result = char + result; if (--position < 0) { break; } const charCode = char.charCodeAt(0); - if (charCode >= 0xdc00 && charCode < 0xe000) { + if (charCode >= 0xdc00 && charCode < 0xe000) { // charCode is a low surrogate code const char2 = text[position]; const charCode2 = char2.charCodeAt(0); - if (charCode2 >= 0xd800 && charCode2 < 0xdc00) { + if (charCode2 >= 0xd800 && charCode2 < 0xdc00) { // charCode2 is a high surrogate code result = char2 + result; if (--position < 0) { break; } } diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index ccdd49cf..87b02f8a 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -882,13 +882,13 @@ export class DisplayGenerator { _appendFurigana(container, term, reading, addText) { container.lang = 'ja'; const segments = this._japaneseUtil.distributeFurigana(term, reading); - for (const {text, reading: reading2} of segments) { - if (reading2) { + for (const {text, reading: furigana} of segments) { + if (furigana) { const ruby = document.createElement('ruby'); const rt = document.createElement('rt'); addText(ruby, text); ruby.appendChild(rt); - rt.appendChild(document.createTextNode(reading2)); + rt.appendChild(document.createTextNode(furigana)); container.appendChild(ruby); } else { addText(container, text); diff --git a/ext/js/display/option-toggle-hotkey-handler.js b/ext/js/display/option-toggle-hotkey-handler.js index edd7de5b..73c92d59 100644 --- a/ext/js/display/option-toggle-hotkey-handler.js +++ b/ext/js/display/option-toggle-hotkey-handler.js @@ -71,17 +71,17 @@ export class OptionToggleHotkeyHandler { try { const optionsContext = this._display.getOptionsContext(); - const result = (await yomitan.api.getSettings([{ + const getSettingsResponse = (await yomitan.api.getSettings([{ scope: 'profile', path, optionsContext }]))[0]; - const {error} = result; - if (typeof error !== 'undefined') { - throw ExtensionError.deserialize(error); + const {error: getSettingsError} = getSettingsResponse; + if (typeof getSettingsError !== 'undefined') { + throw ExtensionError.deserialize(getSettingsError); } - value = result.result; + value = getSettingsResponse.result; if (typeof value !== 'boolean') { throw new Error(`Option value of type ${typeof value} cannot be toggled`); } @@ -96,10 +96,10 @@ export class OptionToggleHotkeyHandler { value, optionsContext }; - const result2 = (await yomitan.api.modifySettings([modification], this._source))[0]; - const {error: error2} = result2; - if (typeof error2 !== 'undefined') { - throw ExtensionError.deserialize(error2); + const modifySettingsResponse = (await yomitan.api.modifySettings([modification], this._source))[0]; + const {error: modifySettingsError} = modifySettingsResponse; + if (typeof modifySettingsError !== 'undefined') { + throw ExtensionError.deserialize(modifySettingsError); } this._showNotification(this._createSuccessMessage(path, value), true); diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js index e71c7251..3e59098f 100644 --- a/ext/js/display/query-parser.js +++ b/ext/js/display/query-parser.js @@ -339,6 +339,7 @@ export class QueryParser extends EventDispatcher { } /** + * Convert _reading_ to hiragana, katakana, or romaji, or _term_ if it is entirely kana and _reading_ is an empty string, based on _readingMode. * @param {string} term * @param {string} reading * @returns {string} diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index bf22ee6b..ddbd8578 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -70,7 +70,7 @@ export class Translator { async findTerms(mode, text, options) { const {enabledDictionaryMap, excludeDictionaryDefinitions, sortFrequencyDictionary, sortFrequencyDictionaryOrder} = options; const tagAggregator = new TranslatorTagAggregator(); - let {dictionaryEntries, originalTextLength} = await this._findTermsInternal(text, enabledDictionaryMap, options, tagAggregator); + let {dictionaryEntries, originalTextLength} = await this._findTermsInternalWrapper(text, enabledDictionaryMap, options, tagAggregator); switch (mode) { case 'group': @@ -208,7 +208,7 @@ export class Translator { * @param {TranslatorTagAggregator} tagAggregator * @returns {Promise} */ - async _findTermsInternal(text, enabledDictionaryMap, options, tagAggregator) { + async _findTermsInternalWrapper(text, enabledDictionaryMap, options, tagAggregator) { if (options.removeNonJapaneseCharacters) { text = this._getJapaneseOnlyText(text); } @@ -216,7 +216,7 @@ export class Translator { return {dictionaryEntries: [], originalTextLength: 0}; } - const deinflections = await this._findTermsInternal2(text, enabledDictionaryMap, options); + const deinflections = await this._findTermsInternal(text, enabledDictionaryMap, options); let originalTextLength = 0; const dictionaryEntries = []; @@ -242,7 +242,7 @@ export class Translator { * @param {import('translation').FindTermsOptions} options * @returns {Promise} */ - async _findTermsInternal2(text, enabledDictionaryMap, options) { + async _findTermsInternal(text, enabledDictionaryMap, options) { const deinflections = ( options.deinflect ? this._getAllDeinflections(text, options) : diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js index f470d9fa..6f357680 100644 --- a/ext/js/pages/settings/anki-controller.js +++ b/ext/js/pages/settings/anki-controller.js @@ -275,11 +275,13 @@ export class AnkiController { */ _onTestAnkiNoteViewerButtonClick(e) { const element = /** @type {HTMLElement} */ (e.currentTarget); + // Anki note GUI mode const {mode} = element.dataset; if (typeof mode !== 'string') { return; } - const mode2 = this._normalizeAnkiNoteGuiMode(mode); - if (mode2 === null) { return; } - this._testAnkiNoteViewerSafe(mode2); + + const normalizedMode = this._normalizeAnkiNoteGuiMode(mode); + if (normalizedMode === null) { return; } + this._testAnkiNoteViewerSafe(normalizedMode); } /** @@ -379,17 +381,17 @@ export class AnkiController { async _getAnkiData() { this._setAnkiStatusChanging(); const [ - [deckNames, error1], - [modelNames, error2] + [deckNames, getDeckNamesError], + [modelNames, getModelNamesError] ] = await Promise.all([ this._getDeckNames(), this._getModelNames() ]); - if (error1 !== null) { - this._showAnkiError(error1); - } else if (error2 !== null) { - this._showAnkiError(error2); + if (getDeckNamesError !== null) { + this._showAnkiError(getDeckNamesError); + } else if (getModelNamesError !== null) { + this._showAnkiError(getModelNamesError); } else { this._hideAnkiError(); } diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 857922bf..515e85da 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -171,10 +171,10 @@ export class AnkiTemplateRenderer { /** @type {import('template-renderer').HelperFunction} */ _furigana(args, context, options) { const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options); - const segs = this._japaneseUtil.distributeFurigana(expression, reading); + const segments = this._japaneseUtil.distributeFurigana(expression, reading); let result = ''; - for (const {text, reading: reading2} of segs) { + for (const {text, reading: reading2} of segments) { const safeText = this._escape(text); const safeReading = this._escape(reading2); if (safeReading.length > 0) { @@ -190,10 +190,10 @@ export class AnkiTemplateRenderer { /** @type {import('template-renderer').HelperFunction} */ _furiganaPlain(args, context, options) { const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options); - const segs = this._japaneseUtil.distributeFurigana(expression, reading); + const segments = this._japaneseUtil.distributeFurigana(expression, reading); let result = ''; - for (const {text, reading: reading2} of segs) { + for (const {text, reading: reading2} of segments) { if (reading2.length > 0) { if (result.length > 0) { result += ' '; } result += `${text}[${reading2}]`; -- cgit v1.2.3