Skip to content

Commit

Permalink
Further strict null checks (#8371)
Browse files Browse the repository at this point in the history
* getToken/refreshToken

* src/background/requests.ts

* src/contentScript/pageEditor/elementPicker.ts

* src/background/partnerIntegrations.ts

* whoops

* cleanup

* fix widget and getScopeAndId

* auto-add

* src/extensionConsole/pages/mods/GetStartedView.tsx

* src/mods/useModViewItems.tsx

* test memo compare dependencies

* fix scopeAndId tests

* autosuggest

* src/background/backgroundPlatform.ts

* src/background/restrictUnauthenticatedUrlAccess.ts

* src/bricks/effects/insertHtml.ts

* ./bricks/effects/runSubTour.ts

* ./bricks/effects/scrollIntoView.ts

* src/bricks/transformers/controlFlow/WithAsyncModVariable.ts

* mod state / variable context

* src/bricks/transformers/RunMetadataTransformer.ts

* src/bricks/transformers/splitText.ts

* src/components/fields/schemaFields/widgets/varPopup/useTreeRow.ts

* src/components/walkthroughModal/WalkthroughModalApp.tsx

* src/extensionConsole/pages/brickEditor/BrickHistory.tsx

* fix test

* src/extensionConsole/pages/mods/utils/exportBlueprint.ts

* src/extensionConsole/pages/mods/utils/exportBlueprint.ts

* src/pageScript/elementInfo.ts

* src/sidebar/LoginPanel.tsx

* src/testUtils/factories/authFactories.ts

* src/bricks/effects/attachAutocomplete.ts

* more bricks

* fix test

* src/components/fields/schemaFields/fieldTestUtils.ts

* add a bunch more

* the rest

* cleanup

* fix lint

* wip

* remove file

* remove logs

* fix factory

* update counts

* extensionPagePlatform

* partnerIntegrations

* update counts

* finding cycles

* documenting cycle

* move starterbricks into separate folders

* extract shared types from contextmenu

* migrate shared types for menuItemExtension

* migrate panel types; migrate more contextMenu types

* migrate more menuitem types

* migrate quickbar types

* migrate quickbar provider types

* migrate sidebar types

* migrate tour types

* update file paths

* auto-add

* fix types

* auto-add

* cleanup

* more contentScript messenger migrations

* selectElement

* insertPanel

* insertButton

* move insertButton to strict registration

* eliminate cycle

* auto-add

* migrate more registration methods

* auto-add

* update counts

* remove comments

* address pr comments

---------

Co-authored-by: Ben Loe <[email protected]>
  • Loading branch information
grahamlangford and Ben Loe authored Apr 30, 2024
1 parent 7ea3b7f commit 78fe96d
Show file tree
Hide file tree
Showing 24 changed files with 158 additions and 102 deletions.
13 changes: 0 additions & 13 deletions src/background/messenger/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,9 @@ export const contextMenus = {
preload: getMethod("PRELOAD_CONTEXT_MENUS", bg),
};

// Use this instead: `import reportError from "@/telemetry/reportError"`
// export const recordError = getNotifier("RECORD_ERROR", bg);

export const initTelemetry = getNotifier("INIT_TELEMETRY", bg);
export const sendDeploymentAlert = getNotifier("SEND_DEPLOYMENT_ALERT", bg);

export const getUserData = getMethod("GET_USER_DATA", bg);

export const installStarterBlueprints = getMethod(
"INSTALL_STARTER_BLUEPRINTS",
bg,
);

export const ping = getMethod("PING", bg);

export const collectPerformanceDiagnostics = getMethod(
"COLLECT_PERFORMANCE_DIAGNOSTICS",
bg,
);
27 changes: 3 additions & 24 deletions src/background/messenger/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,16 @@ import {
ensureContextMenu,
preloadContextMenus,
uninstallContextMenu,
} from "@/background/contextMenus"; // 205 strictNullCheck errors
} from "@/background/contextMenus"; // 201 strictNullCheck errors
import {
requestRunInAllFrames,
requestRunInOtherTabs,
requestRunInOpener,
requestRunInTarget,
requestRunInTop,
} from "@/background/executor"; // Depends on contentScript/messenger to pass strictNullCheck
import { removeExtensionForEveryTab } from "@/background/removeExtensionForEveryTab"; // 207 strictNullCheck errors
import { debouncedActivateStarterMods as installStarterBlueprints } from "@/background/starterMods"; // 211 strictNullCheck errors
import {
collectPerformanceDiagnostics,
initTelemetry,
pong,
recordEvent,
sendDeploymentAlert,
} from "@/background/telemetry"; // Depends on contentScript/messenger to pass strictNullCheck
import { removeExtensionForEveryTab } from "@/background/removeExtensionForEveryTab"; // 203 strictNullCheck errors
import { debouncedActivateStarterMods as installStarterBlueprints } from "@/background/starterMods"; // 209 strictNullCheck errors

import { setCopilotProcessData } from "@/background/partnerHandlers"; // Depends on contentScript/messenger to pass strictNullCheck

Expand All @@ -60,20 +53,13 @@ declare global {

INSTALL_STARTER_BLUEPRINTS: typeof installStarterBlueprints;

PING: typeof pong;
COLLECT_PERFORMANCE_DIAGNOSTICS: typeof collectPerformanceDiagnostics;

REMOVE_EXTENSION_EVERY_TAB: typeof removeExtensionForEveryTab;

REQUEST_RUN_IN_OPENER: typeof requestRunInOpener;
REQUEST_RUN_IN_TARGET: typeof requestRunInTarget;
REQUEST_RUN_IN_TOP: typeof requestRunInTop;
REQUEST_RUN_IN_OTHER_TABS: typeof requestRunInOtherTabs;
REQUEST_RUN_IN_ALL_FRAMES: typeof requestRunInAllFrames;

RECORD_EVENT: typeof recordEvent;
INIT_TELEMETRY: typeof initTelemetry;
SEND_DEPLOYMENT_ALERT: typeof sendDeploymentAlert;
}
}

Expand All @@ -87,19 +73,12 @@ export default function registerMessenger(): void {
UNINSTALL_CONTEXT_MENU: uninstallContextMenu,
ENSURE_CONTEXT_MENU: ensureContextMenu,

PING: pong,
COLLECT_PERFORMANCE_DIAGNOSTICS: collectPerformanceDiagnostics,

REMOVE_EXTENSION_EVERY_TAB: removeExtensionForEveryTab,

REQUEST_RUN_IN_OPENER: requestRunInOpener,
REQUEST_RUN_IN_TARGET: requestRunInTarget,
REQUEST_RUN_IN_TOP: requestRunInTop,
REQUEST_RUN_IN_OTHER_TABS: requestRunInOtherTabs,
REQUEST_RUN_IN_ALL_FRAMES: requestRunInAllFrames,

RECORD_EVENT: recordEvent,
INIT_TELEMETRY: initTelemetry,
SEND_DEPLOYMENT_ALERT: sendDeploymentAlert,
});
}
12 changes: 12 additions & 0 deletions src/background/messenger/strict/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ export const performConfiguredRequestInBackground = getMethod(

export const getPartnerPrincipals = getMethod("GET_PARTNER_PRINCIPALS", bg);
export const launchAuthIntegration = getMethod("LAUNCH_AUTH_INTEGRATION", bg);

export const ping = getMethod("PING", bg);
export const collectPerformanceDiagnostics = getMethod(
"COLLECT_PERFORMANCE_DIAGNOSTICS",
bg,
);

// Use this instead: `import reportError from "@/telemetry/reportError"`
// export const recordError = getNotifier("RECORD_ERROR", bg);

export const initTelemetry = getNotifier("INIT_TELEMETRY", bg);
export const sendDeploymentAlert = getNotifier("SEND_DEPLOYMENT_ALERT", bg);
19 changes: 19 additions & 0 deletions src/background/messenger/strict/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ import {
launchAuthIntegration,
} from "@/background/partnerIntegrations";
import { getAvailableVersion } from "@/background/installer";
import {
collectPerformanceDiagnostics,
initTelemetry,
pong,
recordEvent,
sendDeploymentAlert,
} from "@/background/telemetry";

expectContext("background");

Expand Down Expand Up @@ -110,6 +117,12 @@ declare global {
LAUNCH_AUTH_INTEGRATION: typeof launchAuthIntegration;

GET_AVAILABLE_VERSION: typeof getAvailableVersion;

PING: typeof pong;
COLLECT_PERFORMANCE_DIAGNOSTICS: typeof collectPerformanceDiagnostics;
RECORD_EVENT: typeof recordEvent;
INIT_TELEMETRY: typeof initTelemetry;
SEND_DEPLOYMENT_ALERT: typeof sendDeploymentAlert;
}
}

Expand Down Expand Up @@ -163,5 +176,11 @@ export default function registerMessenger(): void {
LAUNCH_AUTH_INTEGRATION: launchAuthIntegration,

GET_AVAILABLE_VERSION: getAvailableVersion,

PING: pong,
COLLECT_PERFORMANCE_DIAGNOSTICS: collectPerformanceDiagnostics,
RECORD_EVENT: recordEvent,
INIT_TELEMETRY: initTelemetry,
SEND_DEPLOYMENT_ALERT: sendDeploymentAlert,
});
}
2 changes: 1 addition & 1 deletion src/bricks/transformers/extensionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { TransformerABC } from "@/types/bricks/transformerTypes";
import { type Schema } from "@/types/schemaTypes";
import { getDiagnostics as collectFrameDiagnostics } from "@/contentScript/performanceMonitoring";
import { collectPerformanceDiagnostics as collectExtensionDiagnostics } from "@/background/messenger/api";
import { collectPerformanceDiagnostics as collectExtensionDiagnostics } from "@/background/messenger/strict/api";
import { propertiesToSchema } from "@/utils/schemaUtils";

class ExtensionDiagnostics extends TransformerABC {
Expand Down
2 changes: 1 addition & 1 deletion src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import registerBuiltinBricks from "@/bricks/registerBuiltinBricks";
import registerContribBlocks from "@/contrib/registerContribBlocks";
import brickRegistry from "@/bricks/registry";
import { initNavigation } from "@/contentScript/lifecycle";
import { initTelemetry } from "@/background/messenger/api";
import { initTelemetry } from "@/background/messenger/strict/api";
import { initToaster } from "@/utils/notify";
import { initPartnerIntegrations } from "@/contentScript/partnerIntegrations";
import {
Expand Down
3 changes: 0 additions & 3 deletions src/contentScript/messenger/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export const removeInstalledExtension = getNotifier(
export const resetTab = getNotifier("RESET_TAB");
export const toggleQuickBar = getMethod("TOGGLE_QUICK_BAR");

export const insertPanel = getMethod("INSERT_PANEL");
export const insertButton = getMethod("INSERT_BUTTON");

export const runBlock = getMethod("RUN_SINGLE_BLOCK");
export const runRendererBlock = getMethod("RUN_RENDERER_BLOCK");

Expand Down
14 changes: 3 additions & 11 deletions src/contentScript/messenger/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,19 @@ import {
queueReactivateTab,
reactivateTab,
removePersistedExtension,
} from "@/contentScript/lifecycle"; // 203 strictNullCheck errors
import { insertPanel } from "@/contentScript/pageEditor/insertPanel"; // 31 strictNullCheck errors
import { insertButton } from "@/contentScript/pageEditor/insertButton"; // 37 strictNullCheck errors
} from "@/contentScript/lifecycle"; // 202 strictNullCheck errors
import {
clearDynamicElements,
disableOverlay,
enableOverlay,
runExtensionPointReader,
updateDynamicElement,
} from "@/contentScript/pageEditor/dynamic"; // 206 strictNullCheck errors
} from "@/contentScript/pageEditor/dynamic"; // 205 strictNullCheck errors
import {
runBlockPreview,
resetTab,
runRendererBlock,
} from "@/contentScript/pageEditor"; // 208 strictNullCheck errors
} from "@/contentScript/pageEditor"; // 207 strictNullCheck errors
import { runBrick } from "@/contentScript/executor"; // Depends on background/messenger to pass strictNullCheck
import {
runHeadlessPipeline,
Expand All @@ -63,9 +61,6 @@ declare global {
REMOVE_INSTALLED_EXTENSION: typeof removePersistedExtension;
RESET_TAB: typeof resetTab;

INSERT_PANEL: typeof insertPanel;
INSERT_BUTTON: typeof insertButton;

RUN_SINGLE_BLOCK: typeof runBlockPreview;
RUN_RENDERER_BLOCK: typeof runRendererBlock;

Expand Down Expand Up @@ -96,9 +91,6 @@ export default function registerMessenger(): void {
REMOVE_INSTALLED_EXTENSION: removePersistedExtension,
RESET_TAB: resetTab,

INSERT_PANEL: insertPanel,
INSERT_BUTTON: insertButton,

RUN_SINGLE_BLOCK: runBlockPreview,
RUN_RENDERER_BLOCK: runRendererBlock,

Expand Down
3 changes: 3 additions & 0 deletions src/contentScript/messenger/strict/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ export const reloadMarketplaceEnhancements = getMethod(
);
export const getAttributeExamples = getMethod("GET_ATTRIBUTE_EXAMPLES");
export const selectElement = getMethod("SELECT_ELEMENT");

export const insertPanel = getMethod("INSERT_PANEL");
export const insertButton = getMethod("INSERT_BUTTON");
6 changes: 6 additions & 0 deletions src/contentScript/messenger/strict/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import { cancelSelect } from "@/contentScript/pageEditor/elementPicker";
import { reloadActivationEnhancements } from "@/contentScript/loadActivationEnhancementsCore";
import { getAttributeExamples } from "@/contentScript/pageEditor/elementInformation";
import selectElement from "@/contentScript/pageEditor/selectElement";
import { insertPanel } from "@/contentScript/pageEditor/insertPanel";
import { insertButton } from "@/contentScript/pageEditor/insertButton";

declare global {
interface MessengerMethods {
Expand Down Expand Up @@ -81,6 +83,8 @@ declare global {
RELOAD_MARKETPLACE_ENHANCEMENTS: typeof reloadActivationEnhancements;
GET_ATTRIBUTE_EXAMPLES: typeof getAttributeExamples;
SELECT_ELEMENT: typeof selectElement;
INSERT_PANEL: typeof insertPanel;
INSERT_BUTTON: typeof insertButton;
}
}
export default function registerMessenger(): void {
Expand Down Expand Up @@ -114,5 +118,7 @@ export default function registerMessenger(): void {
RELOAD_MARKETPLACE_ENHANCEMENTS: reloadActivationEnhancements,
GET_ATTRIBUTE_EXAMPLES: getAttributeExamples,
SELECT_ELEMENT: selectElement,
INSERT_PANEL: insertPanel,
INSERT_BUTTON: insertButton,
});
}
13 changes: 9 additions & 4 deletions src/contentScript/pageEditor/insertButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function insertButton(
"./beautify"
);

let selected;
let selected: HTMLElement[];
if (useNewFilter) {
const { elements } = await userSelectElement({
filter: `:is(a, button):not(${PRIVATE_ATTRIBUTES_SELECTOR})`,
Expand All @@ -52,14 +52,20 @@ export async function insertButton(
// if the parent is BUTTON, the user probably just selected the wrong thing
if (
selected.length === 1 &&
["A", "BUTTON"].includes(selected[0].parentElement?.tagName)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-type-assertion -- length check
["A", "BUTTON"].includes(selected[0]!.parentElement?.tagName ?? "")
) {
selected = [selected[0].parentElement];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-type-assertion -- verified above
selected = [selected[0]!.parentElement!];
}
}

const { container, selectors: containerSelectors } = findContainer(selected);

if (!containerSelectors[0]) {
throw new Error("No selector found for the button");
}

console.debug("insertButton", { container, selected });

const element: ButtonSelectionResult = {
Expand All @@ -75,7 +81,6 @@ export async function insertButton(
wrap_line_length: 80,
wrap_attributes: "force",
}),
shadowDOM: null,
position: "append",
},
containerInfo: await pageScript.getElementInfo({
Expand Down
4 changes: 4 additions & 0 deletions src/contentScript/pageEditor/insertPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export async function insertPanel(): Promise<PanelSelectionResult> {
const { elements: selected } = await userSelectElement();
const { container, selectors } = findContainer(selected);

if (!selectors[0]) {
throw new Error("No selector found for the panel");
}

return {
uuid: uuidv4(),
panel: {
Expand Down
2 changes: 1 addition & 1 deletion src/contentScript/performanceMonitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { ping } from "@/background/messenger/api";
import { ping } from "@/background/messenger/strict/api";
import { isContextInvalidatedError } from "@/errors/contextInvalidated";
import { isLoadedInIframe } from "@/utils/iframeUtils";
import { getSettingsState } from "@/store/settings/settingsStorage";
Expand Down
2 changes: 1 addition & 1 deletion src/extensionConsole/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { render } from "react-dom";
import React from "react";
import App from "@/extensionConsole/App";
import { initToaster } from "@/utils/notify";
import { initTelemetry } from "@/background/messenger/api";
import { initTelemetry } from "@/background/messenger/strict/api";
import { initMessengerLogging } from "@/development/messengerLogging";
import { initPerformanceMonitoring } from "@/telemetry/performance";
import { initRuntimeLogging } from "@/development/runtimeLogging";
Expand Down
8 changes: 1 addition & 7 deletions src/extensionConsole/pages/mods/ModsPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ import OnboardingView from "@/extensionConsole/pages/mods/onboardingView/Onboard
import EmptyView from "@/extensionConsole/pages/mods/emptyView/EmptyView";
import GetStartedView from "@/extensionConsole/pages/mods/GetStartedView";
import useOnboarding from "@/extensionConsole/pages/mods/onboardingView/useOnboarding";
import { type TableInstance } from "react-table";
import { type ModViewItem } from "@/types/modTypes";
import { type ModsPageContentProps } from "@/extensionConsole/pages/mods/modsPageTypes";

export type ModsPageContentProps = {
tableInstance: TableInstance<ModViewItem>;
width: number;
height: number;
};
const ModsPageContent: React.VoidFunctionComponent<ModsPageContentProps> = ({
tableInstance,
width,
Expand Down
2 changes: 1 addition & 1 deletion src/extensionConsole/pages/mods/gridView/GridView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import ListGroupHeader from "@/extensionConsole/pages/mods/listView/ListGroupHea
import { uuidv4 } from "@/types/helpers";
import { getUniqueId } from "@/utils/modUtils";
import GridCardErrorBoundary from "@/extensionConsole/pages/mods/gridView/GridCardErrorBoundary";
import { type ModsPageContentProps } from "@/extensionConsole/pages/mods/ModsPageContent";
import { type ModsPageContentProps } from "@/extensionConsole/pages/mods/modsPageTypes";

/**
* Expands `react-table` rows recursively in chunks of
Expand Down
2 changes: 1 addition & 1 deletion src/extensionConsole/pages/mods/listView/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { VariableSizeList as List } from "react-window";
import ListGroupHeader from "@/extensionConsole/pages/mods/listView/ListGroupHeader";
import { uuidv4 } from "@/types/helpers";
import ListItemErrorBoundary from "@/extensionConsole/pages/mods/listView/ListItemErrorBoundary";
import { type ModsPageContentProps } from "@/extensionConsole/pages/mods/ModsPageContent";
import { type ModsPageContentProps } from "@/extensionConsole/pages/mods/modsPageTypes";

const ROW_HEIGHT_PX = 90;
const HEADER_ROW_HEIGHT_PX = 43;
Expand Down
25 changes: 25 additions & 0 deletions src/extensionConsole/pages/mods/modsPageTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2024 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { type ModViewItem } from "@/types/modTypes";
import { type TableInstance } from "react-table";

export type ModsPageContentProps = {
tableInstance: TableInstance<ModViewItem>;
width: number;
height: number;
};
2 changes: 1 addition & 1 deletion src/pageEditor/starterBricks/menuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { getDomain } from "@/permissions/patterns";
import { faMousePointer } from "@fortawesome/free-solid-svg-icons";
import { type ElementConfig } from "@/pageEditor/starterBricks/elementConfig";
import MenuItemConfiguration from "@/pageEditor/tabs/menuItem/MenuItemConfiguration";
import { insertButton } from "@/contentScript/messenger/api";
import { insertButton } from "@/contentScript/messenger/strict/api";
import {
type ButtonDefinition,
type ButtonSelectionResult,
Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/starterBricks/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { getDomain } from "@/permissions/patterns";
import { faWindowMaximize } from "@fortawesome/free-solid-svg-icons";
import { type ElementConfig } from "@/pageEditor/starterBricks/elementConfig";
import PanelConfiguration from "@/pageEditor/tabs/panel/PanelConfiguration";
import { insertPanel } from "@/contentScript/messenger/api";
import { insertPanel } from "@/contentScript/messenger/strict/api";
import {
type DynamicDefinition,
type PanelSelectionResult,
Expand Down
Loading

0 comments on commit 78fe96d

Please sign in to comment.