Skip to content

Commit

Permalink
rename variables (#409)
Browse files Browse the repository at this point in the history
* rename variables

* add comment to ambiguous variable

* rename variables

* add comments

* rename functions
  • Loading branch information
Casheeew authored Dec 20, 2023
1 parent 8b943cc commit 229f04b
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 72 deletions.
1 change: 1 addition & 0 deletions dev/manifest-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
8 changes: 4 additions & 4 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,26 +964,26 @@ export class Frontend {
_prepareSiteSpecific() {
switch (location.hostname.toLowerCase()) {
case 'docs.google.com':
this._prepareGoogleDocs();
this._prepareGoogleDocsWrapper();
break;
}
}

/**
* @returns {Promise<void>}
*/
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<void>}
*/
async _prepareGoogleDocs2() {
async _prepareGoogleDocs() {
if (typeof GoogleDocsUtil === 'undefined') { return; }
DocumentUtil.registerGetRangeFromPointHandler(GoogleDocsUtil.getRangeFromPoint.bind(GoogleDocsUtil));
}
Expand Down
8 changes: 4 additions & 4 deletions ext/js/app/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() => {
Expand All @@ -433,9 +433,9 @@ export class Popup extends EventDispatcher {
/**
* @returns {Promise<boolean>}
*/
async _injectInner1() {
async _injectInnerWrapper() {
try {
await this._injectInner2();
await this._injectInner();
return true;
} catch (e) {
this._resetFrame();
Expand All @@ -447,7 +447,7 @@ export class Popup extends EventDispatcher {
/**
* @returns {Promise<void>}
*/
async _injectInner2() {
async _injectInner() {
if (this._optionsContext === null) {
throw new Error('Options not initialized');
}
Expand Down
40 changes: 20 additions & 20 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -799,7 +799,7 @@ export class Backend {

/** @type {import('api').Handler<import('api').GetOrCreateSearchPopupDetails, import('api').GetOrCreateSearchPopupResult>} */
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);
}
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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; });
}
Expand All @@ -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');
/**
Expand Down Expand Up @@ -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; }
Expand Down
12 changes: 6 additions & 6 deletions ext/js/background/offscreen-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions ext/js/comm/mecab.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class Mecab {
*/
async getVersion() {
try {
await this._setupPort();
await this._setupPortWrapper();
} catch (e) {
// NOP
}
Expand All @@ -132,7 +132,7 @@ export class Mecab {
* @returns {Promise<import('mecab').ParseResult[]>} 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));
Expand Down Expand Up @@ -225,12 +225,12 @@ export class Mecab {
/**
* @returns {Promise<void>}
*/
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;
Expand All @@ -242,7 +242,7 @@ export class Mecab {
/**
* @returns {Promise<void>}
*/
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));
Expand Down
8 changes: 4 additions & 4 deletions ext/js/data/sandbox/string-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand All @@ -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; }
}
Expand Down
6 changes: 3 additions & 3 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions ext/js/display/option-toggle-hotkey-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -208,15 +208,15 @@ export class Translator {
* @param {TranslatorTagAggregator} tagAggregator
* @returns {Promise<import('translator').FindTermsResult>}
*/
async _findTermsInternal(text, enabledDictionaryMap, options, tagAggregator) {
async _findTermsInternalWrapper(text, enabledDictionaryMap, options, tagAggregator) {
if (options.removeNonJapaneseCharacters) {
text = this._getJapaneseOnlyText(text);
}
if (text.length === 0) {
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 = [];
Expand All @@ -242,7 +242,7 @@ export class Translator {
* @param {import('translation').FindTermsOptions} options
* @returns {Promise<import('translation-internal').DatabaseDeinflection[]>}
*/
async _findTermsInternal2(text, enabledDictionaryMap, options) {
async _findTermsInternal(text, enabledDictionaryMap, options) {
const deinflections = (
options.deinflect ?
this._getAllDeinflections(text, options) :
Expand Down
20 changes: 11 additions & 9 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}
Expand Down
Loading

0 comments on commit 229f04b

Please sign in to comment.