Skip to content

Commit

Permalink
Stop declaring typescript JSX config as global. (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie authored Jun 17, 2024
1 parent 54437ee commit 908f558
Show file tree
Hide file tree
Showing 26 changed files with 54 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/appservice/bot/ListCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BaseFunction, defineInterfaceCommand } from '../../commands/interface-m
import { findPresentationType, parameters } from '../../commands/interface-manager/ParameterParsing';
import { AppserviceBaseExecutor } from './AppserviceCommandHandler';
import { tickCrossRenderer } from '../../commands/interface-manager/MatrixHelpRenderer';
import { JSXFactory } from '../../commands/interface-manager/JSXFactory';
import { DeadDocumentJSX } from '../../commands/interface-manager/JSXFactory';
import { renderMatrixAndSend } from '../../commands/interface-manager/DeadDocumentMatrix';
import { ActionError, ActionResult, isError, Ok, UserID } from 'matrix-protection-suite';
import { MatrixSendClient } from 'matrix-protection-suite-for-matrix-bot-sdk';
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/CommonRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { ActionError, ActionException, ActionResult, DescriptionMeta, MatrixRoomReference, ResultForUsersInRoom, RoomSetResult, StringRoomID, StringUserID, isOk } from "matrix-protection-suite";
import { DocumentNode } from "../commands/interface-manager/DeadDocument";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { renderMentionPill, renderRoomPill } from "../commands/interface-manager/MatrixHelpRenderer";

export function renderElaborationTrail(error: ActionError): DocumentNode {
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/DraupnirRendererMessageCollector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RendererMessage, RendererMessageCollector } from "./RendererMessageColl
import { DescriptionMeta, StringRoomID, Task } from "matrix-protection-suite";
import { DocumentNode } from "../commands/interface-manager/DeadDocument";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";

export class DraupnirRendererMessageCollector implements RendererMessageCollector {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/ServerACLConsequencesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { ActionResult, Capability, DescriptionMeta, Ok, Permalinks, PolicyListRevision, RoomSetResult, ServerACLConsequencesContext, ServerConsequences, StringRoomID, describeCapabilityContextGlue, describeCapabilityRenderer, isError } from "matrix-protection-suite";
import { RendererMessageCollector } from "./RendererMessageCollector";
import { renderFailedSingularConsequence, renderRoomSetResult } from "./CommonRenderers";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { Draupnir } from "../Draupnir";

class StandardServerConsequencesRenderer implements ServerConsequences {
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/StandardEventConsequencesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { ActionResult, Capability, DescriptionMeta, EventConsequences, Permalinks, RoomEventRedacter, StringEventID, StringRoomID, describeCapabilityContextGlue, describeCapabilityRenderer, isError } from "matrix-protection-suite";
import { RendererMessageCollector } from "./RendererMessageCollector";
import { renderFailedSingularConsequence } from "./CommonRenderers";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { Draupnir } from "../Draupnir";

class StandardEventConsequencesRenderer implements EventConsequences {
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/StandardUserConsequencesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// https://github.com/matrix-org/mjolnir
// </text>

import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { ActionResult, Capability, DescriptionMeta, MatrixRoomReference, Ok, Permalinks, PolicyListRevision, ResultForUsersInRoom, ResultForUsersInSet, RoomSetResult, StandardUserConsequencesContext, StringRoomID, StringUserID, UserConsequences, describeCapabilityContextGlue, describeCapabilityRenderer, isError } from "matrix-protection-suite";
import { RendererMessageCollector } from "./RendererMessageCollector";
import { renderFailedSingularConsequence, renderOutcome, renderResultForUsersInRoom, renderRoomSetResult } from "./CommonRenderers";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Help.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ limitations under the License.
import { DocumentNode } from "./interface-manager/DeadDocument";
import { CommandTable, defineInterfaceCommand, findCommandTable, findTableCommand } from "./interface-manager/InterfaceCommand";
import { renderCommandSummary } from "./interface-manager/MatrixHelpRenderer";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";
import { findPresentationType, parameters, RestDescription } from "./interface-manager/ParameterParsing";
import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/KickCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ActionError, ActionResult, MatrixRoomReference, Ok, StringRoomID, Strin
import { KeywordsDescription, ParsedKeywords, findPresentationType, parameters } from "./interface-manager/ParameterParsing";
import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk";
import { DocumentNode } from "./interface-manager/DeadDocument";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";
import { defineInterfaceCommand, findTableCommand } from "./interface-manager/InterfaceCommand";
import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ProtectionsCommands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfac
import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer";
import { Draupnir } from "../Draupnir";
import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";
import { DocumentNode } from "./interface-manager/DeadDocument";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/ResolveAlias.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { ParsedKeywords, findPresentationType, parameters } from "./interface-ma
import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor";
import { renderRoomPill, tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";

async function resolveAliasCommand(
this: DraupnirContext,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Rooms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { DraupnirContext } from "./CommandHandler";
import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor";
import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer";
import { DocumentNode } from "./interface-manager/DeadDocument";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";
import { ActionException, ActionExceptionKind, ActionResult, MatrixRoomID, MatrixRoomReference, Ok, isError } from "matrix-protection-suite";
import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
import { DraupnirContext } from "./CommandHandler";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";
import { defineInterfaceCommand, findTableCommand } from "./interface-manager/InterfaceCommand";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";
import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer";
import { defineMatrixInterfaceAdaptor, MatrixContext, MatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor";
import { findPresentationType, parameters, union } from "./interface-manager/ParameterParsing";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/StatusCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { defineInterfaceCommand, findTableCommand } from "./interface-manager/In
import { parameters } from "./interface-manager/ParameterParsing";
import { DraupnirContext } from "./CommandHandler";
import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor";
import { JSXFactory } from "./interface-manager/JSXFactory";
import { DeadDocumentJSX } from "./interface-manager/JSXFactory";
import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer";
import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix";
import { ActionResult, Ok, PolicyRoomRevision, PolicyRoomWatchProfile, PolicyRuleType, isError } from "matrix-protection-suite";
Expand Down
65 changes: 29 additions & 36 deletions src/commands/interface-manager/JSXFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,6 @@ import { DocumentNode, LeafNode, makeDocumentNode, makeLeafNode, NodeTag, TextNo
import { DeadDocumentPresentationMirror } from "./DeadDocumentPresentation";

type JSXChild = DocumentNode|LeafNode|string|number|JSXChild[];

export function JSXFactory(tag: NodeTag, properties: unknown, ...rawChildren: (DocumentNode|LeafNode|string)[]) {
const node = makeDocumentNode(tag);
if (properties) {
for (const [key, value] of Object.entries(properties)) {
node.attributeMap.set(key, value);
}
}
const ensureChild = (rawChild: JSXChild) => {
if (typeof rawChild === 'string') {
makeLeafNode<TextNode>(NodeTag.TextNode, node, rawChild);
} else if (typeof rawChild === 'number') {
makeLeafNode<TextNode>(NodeTag.TextNode, node, (rawChild as number).toString());
} else if (Array.isArray(rawChild)) {
rawChild.forEach(ensureChild);
// Then it's a DocumentNode|LeafNode
} else if (typeof rawChild.leafNode === 'boolean') {
if (rawChild.tag === NodeTag.Fragment) {
(rawChild as DocumentNode).getChildren().forEach(node.addChild, node);
} else {
node.addChild(rawChild);
}
} else {
node.addChild(DeadDocumentPresentationMirror.present(rawChild));
}
}
rawChildren.forEach(ensureChild);
return node;
}

// TODO: For `children?` to work without allowing people to accidentally use
// `{undefined}` then we need to enable:
// https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes
Expand All @@ -47,12 +17,35 @@ export function JSXFactory(tag: NodeTag, properties: unknown, ...rawChildren: (D
type NodeProperties = { children?: JSXChild[]|JSXChild };
type LeafNodeProperties = { children?: never[] };

/**
* Has to be global for some reason or VSCodium explodes.
* https://www.typescriptlang.org/docs/handbook/declaration-merging.html
* https://www.typescriptlang.org/tsconfig#jsxFactory
*/
declare global {
export namespace DeadDocumentJSX {
export function JSXFactory(tag: NodeTag, properties: unknown, ...rawChildren: (DocumentNode|LeafNode|string)[]) {
const node = makeDocumentNode(tag);
if (properties) {
for (const [key, value] of Object.entries(properties)) {
node.attributeMap.set(key, value);
}
}
const ensureChild = (rawChild: JSXChild) => {
if (typeof rawChild === 'string') {
makeLeafNode<TextNode>(NodeTag.TextNode, node, rawChild);
} else if (typeof rawChild === 'number') {
makeLeafNode<TextNode>(NodeTag.TextNode, node, (rawChild as number).toString());
} else if (Array.isArray(rawChild)) {
rawChild.forEach(ensureChild);
// Then it's a DocumentNode|LeafNode
} else if (typeof rawChild.leafNode === 'boolean') {
if (rawChild.tag === NodeTag.Fragment) {
(rawChild as DocumentNode).getChildren().forEach(node.addChild, node);
} else {
node.addChild(rawChild);
}
} else {
node.addChild(DeadDocumentPresentationMirror.present(rawChild));
}
}
rawChildren.forEach(ensureChild);
return node;
}
export namespace JSX {
export interface IntrinsicElements {
a: NodeProperties & { href?: string, name?: string, target?: string },
Expand Down
2 changes: 1 addition & 1 deletion src/commands/interface-manager/MatrixHelpRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { BaseFunction, CommandTable, InterfaceCommand } from "./InterfaceCommand";
import { MatrixContext, MatrixInterfaceAdaptor, RendererSignature } from "./MatrixInterfaceAdaptor";
import { ArgumentParseError, ParameterDescription, RestDescription } from "./ParameterParsing";
import { JSXFactory } from "./JSXFactory";
import { DeadDocumentJSX } from "./JSXFactory";
import { DocumentNode } from "./DeadDocument";
import { renderMatrixAndSend } from "./DeadDocumentMatrix";
import { LogService } from "matrix-bot-sdk";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/interface-manager/MatrixPresentations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { ReadItem } from "./CommandReader";
import { findPresentationType, makePresentationType, simpleTypeValidator } from "./ParameterParsing";
import { definePresentationRenderer } from "./DeadDocumentPresentation";
import { JSXFactory } from "./JSXFactory";
import { DeadDocumentJSX } from "./JSXFactory";
import { DocumentNode } from "./DeadDocument";
import { MatrixEventViaAlias, MatrixEventViaRoomID, MatrixRoomAlias, MatrixRoomID, MatrixRoomReference, UserID } from "matrix-protection-suite";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/interface-manager/MatrixPromptForAccept.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { ClientPlatform, Logger, RoomEvent, StringRoomID, Task, Value, isError } from "matrix-protection-suite";
import { renderMatrixAndSend } from "./DeadDocumentMatrix";
import { BaseFunction, CommandTable, InterfaceCommand } from "./InterfaceCommand";
import { JSXFactory } from "./JSXFactory";
import { DeadDocumentJSX } from "./JSXFactory";
import { MatrixContext, findMatrixInterfaceAdaptor } from "./MatrixInterfaceAdaptor";
import { ArgumentStream, ParameterDescription } from "./ParameterParsing";
import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
Expand Down
2 changes: 1 addition & 1 deletion src/models/RoomUpdateError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ limitations under the License.

import { DocumentNode } from "../commands/interface-manager/DeadDocument";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { ActionException, ActionExceptionKind, ActionResult, Ok, RoomUpdateError, StringRoomID } from "matrix-protection-suite";
import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";

Expand Down
2 changes: 1 addition & 1 deletion src/protections/BanPropagation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ limitations under the License.
* a Mjolnir PR that was originally created by Gergő Fándly https://github.com/matrix-org/mjolnir/pull/223
*/

import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";
import { renderMentionPill, renderRoomPill } from "../commands/interface-manager/MatrixHelpRenderer";
import { ListMatches, renderListRules } from "../commands/Rules";
Expand Down
2 changes: 1 addition & 1 deletion src/protections/MissingProtectionPermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { HandleMissingProtectionPermissions, MatrixRoomReference, ProtectionPermissionsChange, StringRoomID, Task } from "matrix-protection-suite";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";
import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { DocumentNode } from "../commands/interface-manager/DeadDocument";
import { renderRoomPill } from "../commands/interface-manager/MatrixHelpRenderer";

Expand Down
2 changes: 1 addition & 1 deletion src/protections/PolicyChangeNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AbstractProtection, ActionResult, Logger, MatrixRoomReference, Ok, Poli
import { DraupnirProtection } from "./Protection";
import { Draupnir } from "../Draupnir";
import { DocumentNode } from "../commands/interface-manager/DeadDocument";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";
import { renderMentionPill, renderRoomPill } from "../commands/interface-manager/MatrixHelpRenderer";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";

Expand Down
2 changes: 1 addition & 1 deletion src/protections/ProtectedRoomsSetRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { ActionError, ProtectionDescription, StringRoomID } from "matrix-protection-suite";
import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../commands/interface-manager/JSXFactory";

export async function renderProtectionFailedToStart(
client: MatrixSendClient,
Expand Down
2 changes: 1 addition & 1 deletion src/protections/invitation/JoinRoomsOnInviteProtection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DraupnirProtection } from "../Protection";
import { isInvitationForUser, isSenderJoinedInRevision } from "./inviteCore";
import { renderMatrixAndSend } from "../../commands/interface-manager/DeadDocumentMatrix";
import { DocumentNode } from "../../commands/interface-manager/DeadDocument";
import { JSXFactory } from "../../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../../commands/interface-manager/JSXFactory";
import { renderMentionPill, renderRoomPill } from "../../commands/interface-manager/MatrixHelpRenderer";
import { renderFailedSingularConsequence } from "../../capabilities/CommonRenderers";
import { ProtectroomsOnInvite } from "./ProtectRoomsOnInvite";
Expand Down
2 changes: 1 addition & 1 deletion src/protections/invitation/ProtectRoomsOnInvite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { Logger, MatrixRoomID, MembershipEvent, Ok, Permalink, ProtectedRoomsSet, RoomEvent, Task, Value, isError } from "matrix-protection-suite";
import { DocumentNode } from "../../commands/interface-manager/DeadDocument";
import { renderActionResultToEvent, renderMentionPill, renderRoomPill } from "../../commands/interface-manager/MatrixHelpRenderer";
import { JSXFactory } from "../../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../../commands/interface-manager/JSXFactory";
import { renderMatrixAndSend } from "../../commands/interface-manager/DeadDocumentMatrix";
import { StaticDecode, Type } from "@sinclair/typebox";
import { Draupnir } from "../../Draupnir";
Expand Down
2 changes: 1 addition & 1 deletion src/protections/invitation/WatchRoomsOnInvite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { ALL_RULE_TYPES, Logger, MJOLNIR_SHORTCODE_EVENT_TYPE, MatrixRoomID, MembershipEvent, Ok, Permalink, ProtectedRoomsSet, RoomEvent, RoomStateRevision, Task, Value, isError } from "matrix-protection-suite";
import { Draupnir } from "../../Draupnir";
import { DocumentNode } from "../../commands/interface-manager/DeadDocument";
import { JSXFactory } from "../../commands/interface-manager/JSXFactory";
import { DeadDocumentJSX } from "../../commands/interface-manager/JSXFactory";
import { renderActionResultToEvent, renderMentionPill, renderRoomPill } from "../../commands/interface-manager/MatrixHelpRenderer";
import { renderMatrixAndSend } from "../../commands/interface-manager/DeadDocumentMatrix";
import { StaticDecode, Type } from "@sinclair/typebox";
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"mocha"
],
"jsx": "react",
"jsxFactory": "JSXFactory"
"jsxFactory": "DeadDocumentJSX.JSXFactory"
},
"include": [
"./src/**/*"
Expand Down

0 comments on commit 908f558

Please sign in to comment.