From 04c077d614b3eae95da1e9b506171a87948a4179 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 27 Aug 2019 22:33:33 +0900 Subject: Fix undefined checking on operation parameter --- src/shared/operations.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/shared') diff --git a/src/shared/operations.ts b/src/shared/operations.ts index 2df2e67..1ce5256 100644 --- a/src/shared/operations.ts +++ b/src/shared/operations.ts @@ -368,7 +368,9 @@ export type Operation = const assertOptionalBoolean = (obj: any, name: string) => { if (Object.prototype.hasOwnProperty.call(obj, name) && typeof obj[name] !== 'boolean') { - throw new TypeError(`Not a boolean parameter: '${name}'`); + throw new TypeError( + `Not a boolean parameter: '${name} (${typeof obj[name]})'`, + ); } }; @@ -376,7 +378,9 @@ const assertOptionalString = (obj: any, name: string, values?: string[]) => { if (Object.prototype.hasOwnProperty.call(obj, name)) { let value = obj[name]; if (typeof value !== 'string') { - throw new TypeError(`Not a string parameter: '${name}'`); + throw new TypeError( + `Not a string parameter: '${name}' (${typeof value})`, + ); } if (values && values.length && values.indexOf(value) === -1) { // eslint-disable-next-line max-len @@ -421,32 +425,32 @@ export const valueOf = (o: any): Operation => { assertOptionalBoolean(o, 'background'); return { type: FOLLOW_START, - newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), - background: Boolean(typeof o.background === undefined ? true : o.background), // eslint-disable-line max-len + newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab), + background: Boolean(typeof o.background === 'undefined' ? true : o.background), // eslint-disable-line max-len }; case PAGE_HOME: assertOptionalBoolean(o, 'newTab'); return { type: PAGE_HOME, - newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), + newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab), }; case TAB_CLOSE: assertOptionalString(o, 'select', ['left', 'right']); return { type: TAB_CLOSE, - select: (typeof o.select === undefined ? 'right' : o.select), + select: (typeof o.select === 'undefined' ? 'right' : o.select), }; case TAB_RELOAD: assertOptionalBoolean(o, 'cache'); return { type: TAB_RELOAD, - cache: Boolean(typeof o.cache === undefined ? false : o.cache), + cache: Boolean(typeof o.cache === 'undefined' ? false : o.cache), }; case URLS_PASTE: assertOptionalBoolean(o, 'newTab'); return { type: URLS_PASTE, - newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), + newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab), }; case INTERNAL_OPEN_URL: assertOptionalBoolean(o, 'newTab'); @@ -456,9 +460,9 @@ export const valueOf = (o: any): Operation => { return { type: INTERNAL_OPEN_URL, url: o.url, - newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), - newWindow: Boolean(typeof o.newWindow === undefined ? false : o.newWindow), // eslint-disable-line max-len - background: Boolean(typeof o.background === undefined ? true : o.background), // eslint-disable-line max-len + newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab), + newWindow: Boolean(typeof o.newWindow === 'undefined' ? false : o.newWindow), // eslint-disable-line max-len + background: Boolean(typeof o.background === 'undefined' ? true : o.background), // eslint-disable-line max-len }; case CANCEL: case ADDON_ENABLE: -- cgit v1.2.3 From 5197f22f9b9168a82e7cc6b0e0c1a6b970101e34 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Thu, 29 Aug 2019 03:37:58 +0000 Subject: Fix type checkings --- src/background/infrastructures/ContentMessageClient.ts | 8 ++++---- src/shared/Settings.ts | 9 +-------- src/shared/property-defs.ts | 6 ++++++ 3 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src/shared') diff --git a/src/background/infrastructures/ContentMessageClient.ts b/src/background/infrastructures/ContentMessageClient.ts index 2983409..4d2284c 100644 --- a/src/background/infrastructures/ContentMessageClient.ts +++ b/src/background/infrastructures/ContentMessageClient.ts @@ -22,14 +22,14 @@ export default class ContentMessageClient { return enabled as any as boolean; } - toggleAddonEnabled(tabId: number): Promise { - return browser.tabs.sendMessage(tabId, { + async toggleAddonEnabled(tabId: number): Promise { + await browser.tabs.sendMessage(tabId, { type: messages.ADDON_TOGGLE_ENABLED, }); } - scrollTo(tabId: number, x: number, y: number): Promise { - return browser.tabs.sendMessage(tabId, { + async scrollTo(tabId: number, x: number, y: number): Promise { + await browser.tabs.sendMessage(tabId, { type: messages.TAB_SCROLL_TO, x, y, diff --git a/src/shared/Settings.ts b/src/shared/Settings.ts index 2a392df..d338e2a 100644 --- a/src/shared/Settings.ts +++ b/src/shared/Settings.ts @@ -21,13 +21,6 @@ export default interface Settings { blacklist: string[]; } -const DefaultProperties: Properties = PropertyDefs.defs.reduce( - (o: {[name: string]: PropertyDefs.Type}, def) => { - o[def.name] = def.defaultValue; - return o; - }, {}) as Properties; - - export const keymapsValueOf = (o: any): Keymaps => { return Object.keys(o).reduce((keymaps: Keymaps, key: string): Keymaps => { let op = operations.valueOf(o[key]); @@ -82,7 +75,7 @@ export const propertiesValueOf = (o: any): Properties => { } } return { - ...DefaultProperties, + ...PropertyDefs.defaultValues, ...o, }; }; diff --git a/src/shared/property-defs.ts b/src/shared/property-defs.ts index 6315030..fec9f80 100644 --- a/src/shared/property-defs.ts +++ b/src/shared/property-defs.ts @@ -48,3 +48,9 @@ export const defs: Def[] = [ 'which are completed at the open page', 'sbh'), ]; + +export const defaultValues = { + hintchars: 'abcdefghijklmnopqrstuvwxyz', + smoothscroll: false, + complete: 'sbh', +}; -- cgit v1.2.3