Bug 1607801 - Pre-define ChromeUtils.import paths; r=julienw

This patch takes the approach from mossop on pre-defining ChromeUtils.import
paths, so that they don't need to be defined where they are used. Perhaps in
the future we could automate this more, but for now this will make the current
approach more ergonomic for consumers of the types.

Differential Revision: https://phabricator.services.mozilla.com/D59206
This commit is contained in:
Greg Tatum 2020-04-28 17:49:59 +00:00
parent 6d3e71a39a
commit 02e0ad2908
5 changed files with 70 additions and 37 deletions

View File

@ -17,11 +17,51 @@
* naming.
*/
declare namespace MockedExports {
/**
* This interface teaches ChromeUtils.import how to find modules.
*/
interface KnownModules {
"resource://gre/modules/Services.jsm":
typeof import("resource://gre/modules/Services.jsm");
"Services":
typeof import("Services");
"chrome":
typeof import("chrome");
"resource://gre/modules/osfile.jsm":
typeof import("resource://gre/modules/osfile.jsm");
"resource://gre/modules/AppConstants.jsm":
typeof import("resource://gre/modules/AppConstants.jsm");
"resource://gre/modules/ProfilerGetSymbols.jsm":
typeof import("resource://gre/modules/ProfilerGetSymbols.jsm");
"resource:///modules/CustomizableUI.jsm":
typeof import("resource:///modules/CustomizableUI.jsm")
"resource:///modules/CustomizableWidgets.jsm":
typeof import("resource:///modules/CustomizableWidgets.jsm");
"resource://devtools/shared/Loader.jsm":
typeof import("resource://devtools/shared/Loader.jsm");
"resource://devtools/client/performance-new/popup/background.jsm.js":
typeof import("resource://devtools/client/performance-new/popup/background.jsm.js");
"resource://devtools/client/shared/browser-loader.js": any;
"resource://devtools/client/performance-new/popup/menu-button.jsm.js":
typeof import("devtools/client/performance-new/popup/menu-button.jsm.js");
"resource://devtools/client/performance-new/popup/panel.jsm.js":
typeof import("devtools/client/performance-new/popup/panel.jsm.js");
"resource:///modules/PanelMultiView.jsm":
typeof import("resource:///modules/PanelMultiView.jsm");
}
interface ChromeUtils {
/**
* Use a JSDoc import declaration to pull in the correct type.
* This function reads the KnownModules and resolves which import to use.
* If you are getting the TS2345 error:
*
* Argument of type '"resource:///.../file.jsm"' is not assignable to parameter
* of type
*
* Then add the file path to the KnownModules above.
*/
import: (path: string) => any;
import: <S extends keyof KnownModules>(module: S) => KnownModules[S];
createObjectIn: (content: ContentWindow) => object;
exportFunction: (fn: Function, scope: object, options?: object) => void;
cloneInto: (value: any, scope: object, options?: object) => void;
@ -160,6 +200,10 @@ declare namespace MockedExports {
const CustomizableWidgetsJSM: any;
const PanelMultiViewJSM: any;
const LoaderJSM: {
require: (path: string) => any;
};
const Services: Services;
// This class is needed by the Cc importing mechanism. e.g.
@ -272,6 +316,10 @@ declare module "resource:///modules/PanelMultiView.jsm" {
export = MockedExports.PanelMultiViewJSM;
}
declare module "resource://devtools/shared/Loader.jsm" {
export = MockedExports.LoaderJSM;
}
declare var ChromeUtils: MockedExports.ChromeUtils;
declare var Cu: MockedExports.ChromeUtils;

View File

@ -59,9 +59,9 @@ const {
setRecordingPreferences,
presets,
getRecordingPreferences,
} = /** @type {import("resource://devtools/client/performance-new/popup/background.jsm.js")} */ (ChromeUtils.import(
} = ChromeUtils.import(
"resource://devtools/client/performance-new/popup/background.jsm.js"
));
);
/**
* This file initializes the DevTools Panel UI. It is in charge of initializing

View File

@ -13,12 +13,12 @@
// The following are not lazily loaded as they are needed during initialization.
/** @type {import("resource://gre/modules/Services.jsm")} */
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
/** @type {import("resource://gre/modules/AppConstants.jsm")} */
const { AppConstants } = ChromeUtils.import(
// For some reason TypeScript was giving me an error when de-structuring AppConstants. I
// suspect a bug in TypeScript was at play.
const AppConstants = ChromeUtils.import(
"resource://gre/modules/AppConstants.jsm"
);
).AppConstants;
/**
* @typedef {import("../@types/perf").RecordingStateFromPreferences} RecordingStateFromPreferences
@ -73,20 +73,17 @@ function requireLazy(callback) {
}
const lazyOS = requireLazy(() =>
/** @type {import("resource://gre/modules/osfile.jsm")} */
(ChromeUtils.import("resource://gre/modules/osfile.jsm"))
ChromeUtils.import("resource://gre/modules/osfile.jsm")
);
const lazyProfilerGetSymbols = requireLazy(() =>
/** @type {import("resource://gre/modules/ProfilerGetSymbols.jsm")} */
(ChromeUtils.import("resource://gre/modules/ProfilerGetSymbols.jsm"))
ChromeUtils.import("resource://gre/modules/ProfilerGetSymbols.jsm")
);
const lazyBrowserModule = requireLazy(() => {
const { require } = ChromeUtils.import(
"resource://devtools/shared/Loader.jsm"
);
/** @type {import("devtools/client/performance-new/browser")} */
const browserModule = require("devtools/client/performance-new/browser");
return browserModule;
});
@ -96,7 +93,6 @@ const lazyPreferenceManagement = requireLazy(() => {
"resource://devtools/shared/Loader.jsm"
);
/** @type {import("devtools/client/performance-new/preference-management")} */
const preferenceManagementModule = require("devtools/client/performance-new/preference-management");
return preferenceManagementModule;
});
@ -106,7 +102,6 @@ const lazyRecordingUtils = requireLazy(() => {
"resource://devtools/shared/Loader.jsm"
);
/** @type {import("devtools/shared/performance-new/recording-utils")} */
const recordingUtils = require("devtools/shared/performance-new/recording-utils");
return recordingUtils;
});
@ -115,21 +110,18 @@ const lazyUtils = requireLazy(() => {
const { require } = ChromeUtils.import(
"resource://devtools/shared/Loader.jsm"
);
/** @type {import("devtools/client/performance-new/utils")} */
const recordingUtils = require("devtools/client/performance-new/utils");
return recordingUtils;
});
const lazyProfilerMenuButton = requireLazy(() =>
/** @type {import("devtools/client/performance-new/popup/menu-button.jsm.js")} */
(ChromeUtils.import(
ChromeUtils.import(
"resource://devtools/client/performance-new/popup/menu-button.jsm.js"
))
)
);
const lazyCustomizableUI = requireLazy(() =>
/** @type {import("resource:///modules/CustomizableUI.jsm")} */
(ChromeUtils.import("resource:///modules/CustomizableUI.jsm"))
ChromeUtils.import("resource:///modules/CustomizableUI.jsm")
);
/** @type {Presets} */

View File

@ -37,22 +37,18 @@ function requireLazy(callback) {
/** @type {any} */ (this).exports = {};
const lazyServices = requireLazy(() =>
/** @type {import("resource://gre/modules/Services.jsm")} */
(ChromeUtils.import("resource://gre/modules/Services.jsm"))
ChromeUtils.import("resource://gre/modules/Services.jsm")
);
const lazyCustomizableUI = requireLazy(() =>
/** @type {import("resource:///modules/CustomizableUI.jsm")} */
(ChromeUtils.import("resource:///modules/CustomizableUI.jsm"))
ChromeUtils.import("resource:///modules/CustomizableUI.jsm")
);
const lazyCustomizableWidgets = requireLazy(() =>
/** @type {import("resource:///modules/CustomizableWidgets.jsm")} */
(ChromeUtils.import("resource:///modules/CustomizableWidgets.jsm"))
ChromeUtils.import("resource:///modules/CustomizableWidgets.jsm")
);
const lazyPopupPanel = requireLazy(() =>
/** @type {import("devtools/client/performance-new/popup/panel.jsm.js")} */
(ChromeUtils.import(
ChromeUtils.import(
"resource://devtools/client/performance-new/popup/panel.jsm.js"
))
)
);
const WIDGET_ID = "profiler-button";

View File

@ -43,18 +43,15 @@ function requireLazy(callback) {
/** @type {any} */ (this).module = {};
const lazyServices = requireLazy(() =>
/** @type {import("resource://gre/modules/Services.jsm")} */
(ChromeUtils.import("resource://gre/modules/Services.jsm"))
ChromeUtils.import("resource://gre/modules/Services.jsm")
);
const lazyPanelMultiView = requireLazy(() =>
/** @type {import("resource:///modules/PanelMultiView.jsm")} */
(ChromeUtils.import("resource:///modules/PanelMultiView.jsm"))
ChromeUtils.import("resource:///modules/PanelMultiView.jsm")
);
const lazyBackground = requireLazy(() =>
/** @type {import("resource://devtools/client/performance-new/popup/background.jsm.js")} */
(ChromeUtils.import(
ChromeUtils.import(
"resource://devtools/client/performance-new/popup/background.jsm.js"
))
)
);
/**