From ffb53405fb48325d41c95662ecc8f472b748dc95 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 14 Jun 2019 07:52:24 -0700 Subject: [PATCH] chore: convert extension apis to TypeScript (#18688) Converts extensions-related files to TS --- filenames.auto.gni | 24 ++++----- lib/renderer/chrome-api.ts | 3 +- .../extensions/{event.js => event.ts} | 16 ++---- lib/renderer/extensions/{i18n.js => i18n.ts} | 36 ++++++------- .../extensions/{storage.js => storage.ts} | 53 ++++++++----------- .../{web-navigation.js => web-navigation.ts} | 19 +++---- 6 files changed, 66 insertions(+), 85 deletions(-) rename lib/renderer/extensions/{event.js => event.ts} (58%) rename lib/renderer/extensions/{i18n.js => i18n.ts} (51%) rename lib/renderer/extensions/{storage.js => storage.ts} (52%) rename lib/renderer/extensions/{web-navigation.js => web-navigation.ts} (53%) diff --git a/filenames.auto.gni b/filenames.auto.gni index 0e2ac61f8c..f4c1c11b1c 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -134,10 +134,10 @@ auto_filenames = { "lib/renderer/callbacks-registry.js", "lib/renderer/chrome-api.ts", "lib/renderer/content-scripts-injector.ts", - "lib/renderer/extensions/event.js", - "lib/renderer/extensions/i18n.js", - "lib/renderer/extensions/storage.js", - "lib/renderer/extensions/web-navigation.js", + "lib/renderer/extensions/event.ts", + "lib/renderer/extensions/i18n.ts", + "lib/renderer/extensions/storage.ts", + "lib/renderer/extensions/web-navigation.ts", "lib/renderer/inspector.ts", "lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal.ts", @@ -174,10 +174,10 @@ auto_filenames = { "lib/common/error-utils.js", "lib/content_script/init.js", "lib/renderer/chrome-api.ts", - "lib/renderer/extensions/event.js", - "lib/renderer/extensions/i18n.js", - "lib/renderer/extensions/storage.js", - "lib/renderer/extensions/web-navigation.js", + "lib/renderer/extensions/event.ts", + "lib/renderer/extensions/i18n.ts", + "lib/renderer/extensions/storage.ts", + "lib/renderer/extensions/web-navigation.ts", "lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal.ts", "lib/renderer/window-setup.ts", @@ -294,10 +294,10 @@ auto_filenames = { "lib/renderer/callbacks-registry.js", "lib/renderer/chrome-api.ts", "lib/renderer/content-scripts-injector.ts", - "lib/renderer/extensions/event.js", - "lib/renderer/extensions/i18n.js", - "lib/renderer/extensions/storage.js", - "lib/renderer/extensions/web-navigation.js", + "lib/renderer/extensions/event.ts", + "lib/renderer/extensions/i18n.ts", + "lib/renderer/extensions/storage.ts", + "lib/renderer/extensions/web-navigation.ts", "lib/renderer/init.ts", "lib/renderer/inspector.ts", "lib/renderer/ipc-renderer-internal-utils.ts", diff --git a/lib/renderer/chrome-api.ts b/lib/renderer/chrome-api.ts index e8bc85d7a5..f2c8db6139 100644 --- a/lib/renderer/chrome-api.ts +++ b/lib/renderer/chrome-api.ts @@ -2,8 +2,7 @@ import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-in import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils' import * as url from 'url' -// Todo: Import once extensions have been turned into TypeScript -const Event = require('@electron/internal/renderer/extensions/event') +import { Event } from '@electron/internal/renderer/extensions/event' class Tab { public id: number diff --git a/lib/renderer/extensions/event.js b/lib/renderer/extensions/event.ts similarity index 58% rename from lib/renderer/extensions/event.js rename to lib/renderer/extensions/event.ts index 93f5ad2f7e..d1fc5fb723 100644 --- a/lib/renderer/extensions/event.js +++ b/lib/renderer/extensions/event.ts @@ -1,26 +1,20 @@ -'use strict' +export class Event { + private listeners: Function[] = [] -class Event { - constructor () { - this.listeners = [] - } - - addListener (callback) { + addListener (callback: Function) { this.listeners.push(callback) } - removeListener (callback) { + removeListener (callback: Function) { const index = this.listeners.indexOf(callback) if (index !== -1) { this.listeners.splice(index, 1) } } - emit (...args) { + emit (...args: any[]) { for (const listener of this.listeners) { listener(...args) } } } - -module.exports = Event diff --git a/lib/renderer/extensions/i18n.js b/lib/renderer/extensions/i18n.ts similarity index 51% rename from lib/renderer/extensions/i18n.js rename to lib/renderer/extensions/i18n.ts index 9ae0ca900d..7d3c718434 100644 --- a/lib/renderer/extensions/i18n.js +++ b/lib/renderer/extensions/i18n.ts @@ -1,41 +1,41 @@ -'use strict' - // Implementation of chrome.i18n.getMessage // https://developer.chrome.com/extensions/i18n#method-getMessage // // Does not implement predefined messages: // https://developer.chrome.com/extensions/i18n#overview-predefined -const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') +import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils' -const getMessages = (extensionId) => { +interface Placeholder { + content: string; + example?: string; +} + +const getMessages = (extensionId: number) => { try { - const data = ipcRendererUtils.invokeSync('CHROME_GET_MESSAGES', extensionId) + const data = ipcRendererUtils.invokeSync('CHROME_GET_MESSAGES', extensionId) return JSON.parse(data) || {} } catch { return {} } } -const replaceNumberedSubstitutions = (message, substitutions) => { +const replaceNumberedSubstitutions = (message: string, substitutions: string[]) => { return message.replace(/\$(\d+)/, (_, number) => { const index = parseInt(number, 10) - 1 return substitutions[index] || '' }) } -const replacePlaceholders = (message, placeholders, substitutions) => { - if (typeof substitutions === 'string') { - substitutions = [substitutions] - } - if (!Array.isArray(substitutions)) { - substitutions = [] - } +const replacePlaceholders = (message: string, placeholders: Record, substitutions: string[] | string) => { + if (typeof substitutions === 'string') substitutions = [substitutions] + if (!Array.isArray(substitutions)) substitutions = [] if (placeholders) { - Object.keys(placeholders).forEach((name) => { + Object.keys(placeholders).forEach((name: string) => { let { content } = placeholders[name] - content = replaceNumberedSubstitutions(content, substitutions) + const substitutionsArray = Array.isArray(substitutions) ? substitutions : [] + content = replaceNumberedSubstitutions(content, substitutionsArray) message = message.replace(new RegExp(`\\$${name}\\$`, 'gi'), content) }) } @@ -43,7 +43,7 @@ const replacePlaceholders = (message, placeholders, substitutions) => { return replaceNumberedSubstitutions(message, substitutions) } -const getMessage = (extensionId, messageName, substitutions) => { +const getMessage = (extensionId: number, messageName: string, substitutions: string[]) => { const messages = getMessages(extensionId) if (messages.hasOwnProperty(messageName)) { const { message, placeholders } = messages[messageName] @@ -51,9 +51,9 @@ const getMessage = (extensionId, messageName, substitutions) => { } } -exports.setup = (extensionId) => { +exports.setup = (extensionId: number) => { return { - getMessage (messageName, substitutions) { + getMessage (messageName: string, substitutions: string[]) { return getMessage(extensionId, messageName, substitutions) } } diff --git a/lib/renderer/extensions/storage.js b/lib/renderer/extensions/storage.ts similarity index 52% rename from lib/renderer/extensions/storage.js rename to lib/renderer/extensions/storage.ts index ffd82a641d..5b39de4cc0 100644 --- a/lib/renderer/extensions/storage.js +++ b/lib/renderer/extensions/storage.ts @@ -1,11 +1,9 @@ -'use strict' +import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils' -const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') - -const getStorage = (storageType, extensionId, callback) => { +const getStorage = (storageType: string, extensionId: number, callback: Function) => { if (typeof callback !== 'function') throw new TypeError('No callback provided') - ipcRendererUtils.invoke('CHROME_STORAGE_READ', storageType, extensionId) + ipcRendererUtils.invoke('CHROME_STORAGE_READ', storageType, extensionId) .then(data => { if (data !== null) { callback(JSON.parse(data)) @@ -17,7 +15,7 @@ const getStorage = (storageType, extensionId, callback) => { }) } -const setStorage = (storageType, extensionId, storage, callback) => { +const setStorage = (storageType: string, extensionId: number, storage: Record, callback: Function) => { const json = JSON.stringify(storage) ipcRendererUtils.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json) .then(() => { @@ -25,13 +23,13 @@ const setStorage = (storageType, extensionId, storage, callback) => { }) } -const getStorageManager = (storageType, extensionId) => { +const getStorageManager = (storageType: string, extensionId: number) => { return { - get (keys, callback) { - getStorage(storageType, extensionId, storage => { + get (keys: string[], callback: Function) { + getStorage(storageType, extensionId, (storage: Record) => { if (keys == null) return callback(storage) - let defaults = {} + let defaults: Record = {} switch (typeof keys) { case 'string': keys = [keys] @@ -48,8 +46,8 @@ const getStorageManager = (storageType, extensionId) => { // eslint-disable-next-line standard/no-callback-literal if (keys.length === 0) return callback({}) - const items = {} - keys.forEach(function (key) { + const items: Record = {} + keys.forEach((key: string) => { let value = storage[key] if (value == null) value = defaults[key] items[key] = value @@ -58,22 +56,17 @@ const getStorageManager = (storageType, extensionId) => { }) }, - set (items, callback) { - getStorage(storageType, extensionId, storage => { - Object.keys(items).forEach(function (name) { - storage[name] = items[name] - }) - + set (items: Record, callback: Function) { + getStorage(storageType, extensionId, (storage: Record) => { + Object.keys(items).forEach(name => { storage[name] = items[name] }) setStorage(storageType, extensionId, storage, callback) }) }, - remove (keys, callback) { - getStorage(storageType, extensionId, storage => { - if (!Array.isArray(keys)) { - keys = [keys] - } - keys.forEach(function (key) { + remove (keys: string[], callback: Function) { + getStorage(storageType, extensionId, (storage: Record) => { + if (!Array.isArray(keys)) keys = [keys] + keys.forEach((key: string) => { delete storage[key] }) @@ -81,15 +74,13 @@ const getStorageManager = (storageType, extensionId) => { }) }, - clear (callback) { + clear (callback: Function) { setStorage(storageType, extensionId, {}, callback) } } } -module.exports = { - setup: extensionId => ({ - sync: getStorageManager('sync', extensionId), - local: getStorageManager('local', extensionId) - }) -} +export const setup = (extensionId: number) => ({ + sync: getStorageManager('sync', extensionId), + local: getStorageManager('local', extensionId) +}) diff --git a/lib/renderer/extensions/web-navigation.js b/lib/renderer/extensions/web-navigation.ts similarity index 53% rename from lib/renderer/extensions/web-navigation.js rename to lib/renderer/extensions/web-navigation.ts index e769b106b6..f98bf21a1f 100644 --- a/lib/renderer/extensions/web-navigation.js +++ b/lib/renderer/extensions/web-navigation.ts @@ -1,23 +1,20 @@ -'use strict' - -const Event = require('@electron/internal/renderer/extensions/event') +import { Event } from '@electron/internal/renderer/extensions/event' +import { IpcMainEvent } from 'electron' const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') class WebNavigation { - constructor () { - this.onBeforeNavigate = new Event() - this.onCompleted = new Event() + private onBeforeNavigate = new Event() + private onCompleted = new Event() - ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event, details) => { + constructor () { + ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event: IpcMainEvent, details: any) => { this.onBeforeNavigate.emit(details) }) - ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event, details) => { + ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event: IpcMainEvent, details: any) => { this.onCompleted.emit(details) }) } } -exports.setup = () => { - return new WebNavigation() -} +export const setup = () => new WebNavigation()