Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8579 & #8614: Migrate mod component form state #8734

Merged
merged 15 commits into from
Jul 1, 2024
22 changes: 10 additions & 12 deletions src/analysis/analysisVisitors/baseAnalysisVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,37 @@ export abstract class AnalysisVisitorABC
{
abstract readonly id: string;

protected extension!: ModComponentFormState;
protected formState!: ModComponentFormState;

protected readonly annotations: AnalysisAnnotation[] = [];
getAnnotations(): AnalysisAnnotation[] {
return this.annotations;
}

/**
* Visit the extension point definition.
* Visit the starter brick definition.
*/
visitExtensionPoint(
extensionPoint: ModComponentFormState["extensionPoint"],
): void {
visitStarterBrick(starterBrick: ModComponentFormState["starterBrick"]): void {
// NOP
}

run(extension: ModComponentFormState): void {
this.extension = extension;
run(formState: ModComponentFormState): void {
this.formState = formState;

this.visitExtensionPoint(extension.extensionPoint);
this.visitStarterBrick(formState.starterBrick);

this.visitRootPipeline(extension.extension.blockPipeline, {
extensionPointType: extension.type,
this.visitRootPipeline(formState.modComponent.brickPipeline, {
starterBrickType: formState.type,
});
}
}

export abstract class AnalysisVisitorWithResolvedBricksABC extends AnalysisVisitorABC {
protected allBlocks!: TypedBrickMap;

override async run(extension: ModComponentFormState): Promise<void> {
override async run(formState: ModComponentFormState): Promise<void> {
this.allBlocks = await blockRegistry.allTyped();

super.run(extension);
super.run(formState);
}
}
12 changes: 6 additions & 6 deletions src/analysis/analysisVisitors/brickTypeAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ beforeAll(() => {

describe("BrickTypeAnalysis", () => {
test("disallow effect in renderer", async () => {
const modComponent = sidebarPanelFormStateFactory();
const formState = sidebarPanelFormStateFactory();

modComponent.extension.blockPipeline = [
formState.modComponent.brickPipeline = [
createNewConfiguredBrick(ALERT_EFFECT_ID),
createNewConfiguredBrick(DocumentRenderer.BRICK_ID),
];

const analysis = new BrickTypeAnalysis();

await analysis.run(modComponent);
await analysis.run(formState);

expect(analysis.getAnnotations()).toHaveLength(1);
});
Expand All @@ -55,16 +55,16 @@ describe("BrickTypeAnalysis", () => {
CancelEffect.BRICK_ID,
ErrorEffect.BRICK_ID,
])("allow %s in renderer", async (brickId) => {
const modComponent = sidebarPanelFormStateFactory();
const formState = sidebarPanelFormStateFactory();

modComponent.extension.blockPipeline = [
formState.modComponent.brickPipeline = [
createNewConfiguredBrick(brickId),
createNewConfiguredBrick(DocumentRenderer.BRICK_ID),
];

const analysis = new BrickTypeAnalysis();

await analysis.run(modComponent);
await analysis.run(formState);

expect(analysis.getAnnotations()).toHaveLength(0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { toExpression } from "@/utils/expressionUtils";
describe("checkEventNamesAnalysis", () => {
it("error on missing custom event", async () => {
const formState = triggerFormStateFactory();
formState.extensionPoint.definition.trigger = "custom";
formState.starterBrick.definition.trigger = "custom";

const analysis = new CheckEventNamesAnalysis([formState]);
await analysis.run(formState);
Expand All @@ -42,8 +42,8 @@ describe("checkEventNamesAnalysis", () => {

it.each(["click"])("allow dom event: %s", async (eventName) => {
const formState = triggerFormStateFactory();
formState.extensionPoint.definition.trigger = "custom";
formState.extensionPoint.definition.customEvent = { eventName };
formState.starterBrick.definition.trigger = "custom";
formState.starterBrick.definition.customEvent = { eventName };

const analysis = new CheckEventNamesAnalysis([formState]);
await analysis.run(formState);
Expand All @@ -53,8 +53,8 @@ describe("checkEventNamesAnalysis", () => {

it("warn on unknown event", async () => {
const formState = triggerFormStateFactory();
formState.extensionPoint.definition.trigger = "custom";
formState.extensionPoint.definition.customEvent = { eventName: "unknown" };
formState.starterBrick.definition.trigger = "custom";
formState.starterBrick.definition.customEvent = { eventName: "unknown" };

const analysis = new CheckEventNamesAnalysis([formState]);
await analysis.run(formState);
Expand All @@ -68,8 +68,8 @@ describe("checkEventNamesAnalysis", () => {

it("allow known event", async () => {
const triggerFormState = triggerFormStateFactory();
triggerFormState.extensionPoint.definition.trigger = "custom";
triggerFormState.extensionPoint.definition.customEvent = {
triggerFormState.starterBrick.definition.trigger = "custom";
triggerFormState.starterBrick.definition.customEvent = {
eventName: "myevent",
};

Expand All @@ -88,8 +88,8 @@ describe("checkEventNamesAnalysis", () => {

it("info on unknown event with dynamic expressions", async () => {
const triggerFormState = triggerFormStateFactory();
triggerFormState.extensionPoint.definition.trigger = "custom";
triggerFormState.extensionPoint.definition.customEvent = {
triggerFormState.starterBrick.definition.trigger = "custom";
triggerFormState.starterBrick.definition.customEvent = {
eventName: "myevent",
};

Expand Down Expand Up @@ -117,8 +117,8 @@ describe("checkEventNamesAnalysis", () => {

it("get knownEventNames() combines known and trigger names", async () => {
const triggerFormState = triggerFormStateFactory();
triggerFormState.extensionPoint.definition.trigger = "custom";
triggerFormState.extensionPoint.definition.customEvent = {
triggerFormState.starterBrick.definition.trigger = "custom";
triggerFormState.starterBrick.definition.customEvent = {
eventName: "myevent",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ class CheckEventNamesAnalysis extends AnalysisVisitorABC {
];
}

override visitExtensionPoint(
extensionPoint: ModComponentFormState["extensionPoint"],
override visitStarterBrick(
starterBrick: ModComponentFormState["starterBrick"],
) {
super.visitExtensionPoint(extensionPoint);
super.visitStarterBrick(starterBrick);

if (
isTriggerStarterBrick(extensionPoint) &&
extensionPoint.definition.trigger === "custom"
isTriggerStarterBrick(starterBrick) &&
starterBrick.definition.trigger === "custom"
) {
const eventName = extensionPoint.definition.customEvent?.eventName;
const eventName = starterBrick.definition.customEvent?.eventName;

if (!eventName) {
this.annotations.push({
analysisId: this.id,
type: AnnotationType.Error,
message: "Custom event name is required",
position: { path: "extensionPoint.definition.customEvent.eventName" },
position: { path: "starterBrick.definition.customEvent.eventName" },
});
} else if (
!DOM_EVENTS.includes(eventName) &&
Expand All @@ -79,7 +79,7 @@ class CheckEventNamesAnalysis extends AnalysisVisitorABC {
type: AnnotationType.Info,
message: `Custom event ${eventName} does not match a known emitted event in this Mod`,
position: {
path: "extensionPoint.definition.customEvent.eventName",
path: "starterBrick.definition.customEvent.eventName",
},
});
} else {
Expand All @@ -88,15 +88,15 @@ class CheckEventNamesAnalysis extends AnalysisVisitorABC {
type: AnnotationType.Warning,
message: `Custom event ${eventName} is not emitted in this Mod`,
position: {
path: "extensionPoint.definition.customEvent.eventName",
path: "starterBrick.definition.customEvent.eventName",
},
});
}
}
}
}

override async run(extension: ModComponentFormState) {
override async run(formState: ModComponentFormState) {
const results = this.formStates.map((x) =>
CollectEventNamesVisitor.collectNames(x),
);
Expand All @@ -111,7 +111,7 @@ class CheckEventNamesAnalysis extends AnalysisVisitorABC {
hasDynamicEventName: results.some((result) => result.hasDynamicEventName),
};

super.run(extension);
super.run(formState);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { toExpression } from "@/utils/expressionUtils";
describe("collectEventNamesAnalysis", () => {
it("collects event name from literal", () => {
const formState = formStateFactory();
formState.extension.blockPipeline[0] = {
formState.modComponent.brickPipeline[0] = {
id: CustomEventEffect.BRICK_ID,
config: {
eventName: "foo",
Expand All @@ -45,7 +45,7 @@ describe("collectEventNamesAnalysis", () => {

it("collects event name from template literal", () => {
const formState = formStateFactory();
formState.extension.blockPipeline[0] = {
formState.modComponent.brickPipeline[0] = {
id: CustomEventEffect.BRICK_ID,
config: {
eventName: toExpression("nunjucks", "foo"),
Expand All @@ -63,7 +63,7 @@ describe("collectEventNamesAnalysis", () => {

it("sets dynamic event flag", () => {
const formState = formStateFactory();
formState.extension.blockPipeline[0] = {
formState.modComponent.brickPipeline[0] = {
id: CustomEventEffect.BRICK_ID,
config: {
eventName: toExpression("nunjucks", "{{ @foo }}"),
Expand All @@ -81,8 +81,8 @@ describe("collectEventNamesAnalysis", () => {

it("collects custom trigger names from trigger starter bricks", () => {
const formState = triggerFormStateFactory();
formState.extensionPoint.definition.trigger = "custom";
formState.extensionPoint.definition.customEvent = {
formState.starterBrick.definition.trigger = "custom";
formState.starterBrick.definition.customEvent = {
eventName: "foo",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class CollectEventNamesVisitor extends PipelineVisitor {
}

private visitStarterBrick(
extensionPoint: ModComponentFormState["extensionPoint"],
starterBrick: ModComponentFormState["starterBrick"],
) {
if (isTriggerStarterBrick(extensionPoint)) {
const eventName = extensionPoint.definition.customEvent?.eventName;
if (isTriggerStarterBrick(starterBrick)) {
const eventName = starterBrick.definition.customEvent?.eventName;

if (eventName) {
this._triggerNames.add(eventName);
Expand Down Expand Up @@ -98,8 +98,8 @@ class CollectEventNamesVisitor extends PipelineVisitor {
): EventNameAnalysisResult {
const visitor = new CollectEventNamesVisitor();

visitor.visitRootPipeline(formState.extension.blockPipeline);
visitor.visitStarterBrick(formState.extensionPoint);
visitor.visitRootPipeline(formState.modComponent.brickPipeline);
visitor.visitStarterBrick(formState.starterBrick);

return visitor.result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ const hostRegexp = /^(\*|(^(\*\.)?[^*/]+))$/;
// All URLs is required for certain interactions, e.g., taking screenshots without activeTab
const allUrls = "<all_urls>";

const urlPatternFields = ["extensionPoint.definition.isAvailable.urlPatterns"];
const urlPatternFields = ["starterBrick.definition.isAvailable.urlPatterns"];

const stringUrlFields = [
"extensionPoint.definition.documentUrlPatterns",
"extensionPoint.definition.isAvailable.matchPatterns",
"starterBrick.definition.documentUrlPatterns",
"starterBrick.definition.isAvailable.matchPatterns",
"permissions.origins",
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ beforeAll(() => {
describe("ModVariableSchemasVisitor", () => {
it("collects event schema from a template literal", async () => {
const formState = formStateFactory();
formState.extension.blockPipeline[0] = {
formState.modComponent.brickPipeline[0] = {
id: AssignModVariable.BRICK_ID,
config: {
variableName: toExpression("nunjucks", "foo"),
Expand All @@ -44,15 +44,15 @@ describe("ModVariableSchemasVisitor", () => {

it("unions known schemas", async () => {
const formState = formStateFactory();
formState.extension.blockPipeline[0] = {
formState.modComponent.brickPipeline[0] = {
id: AssignModVariable.BRICK_ID,
config: {
variableName: "foo",
},
};

const otherFormState = formStateFactory();
otherFormState.extension.blockPipeline[0] = {
otherFormState.modComponent.brickPipeline[0] = {
id: AssignModVariable.BRICK_ID,
config: {
variableName: "bar",
Expand All @@ -71,15 +71,15 @@ describe("ModVariableSchemasVisitor", () => {

it("does not duplicate schemas", async () => {
const formState = formStateFactory();
formState.extension.blockPipeline[0] = {
formState.modComponent.brickPipeline[0] = {
id: AssignModVariable.BRICK_ID,
config: {
variableName: "foo",
},
};

const otherFormState = formStateFactory();
otherFormState.extension.blockPipeline[0] = {
otherFormState.modComponent.brickPipeline[0] = {
id: AssignModVariable.BRICK_ID,
config: {
variableName: "foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ModVariableSchemasVisitor extends PipelineVisitor {
const visitor = new ModVariableSchemasVisitor(allBlocks);

for (const formState of formStates) {
visitor.visitRootPipeline(formState.extension.blockPipeline);
visitor.visitRootPipeline(formState.modComponent.brickPipeline);
}

// Schema promises return undefined if not page state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ describe("PageStateAnalysis", () => {
},
]);

state.recipe = { id: registryIdFactory() } as BaseFormState["recipe"];
state.modMetadata = {
id: registryIdFactory(),
} as BaseFormState["modMetadata"];

const analysis = new PageStateAnalysis();
await analysis.run(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class PageStateVisitor extends AnalysisVisitorWithResolvedBricksABC {
}
}

override async run(extension: ModComponentFormState): Promise<void> {
this.isInMod = Boolean(extension.recipe);
await super.run(extension);
override async run(formState: ModComponentFormState): Promise<void> {
this.isInMod = Boolean(formState.modMetadata);
await super.run(formState);
}
}

Expand Down
Loading
Loading