Skip to content

Commit

Permalink
Update linter dependencies (#114)
Browse files Browse the repository at this point in the history
* Replace babel-eslint with @babel/eslint-parser

as the former has been moved to the latter

* Migrate to eslint-plugin-matrix-org

Also add required peer dependencies

* Run lint:fix

* Add missing return types on functions

* Allow empty interface types

* Avoid unnecessary any

* Allow any on exported types

* Allow unknown object to string

* Lint test files

* Increase test coverage
  • Loading branch information
AndrewFerr authored Jan 3, 2025
1 parent 35d3e51 commit ccd40e2
Show file tree
Hide file tree
Showing 16 changed files with 1,632 additions and 1,026 deletions.
16 changes: 13 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module.exports = {
extends: ["matrix-org"],
plugins: [
"babel",
"matrix-org",
],
extends: [
"plugin:matrix-org/babel",
],
parserOptions: {
project: ["./tsconfig-dev.json"],
},
env: {
browser: true,
},
Expand All @@ -29,12 +34,17 @@ module.exports = {
},
overrides: [{
"files": ["src/**/*.ts", "test/**/*.ts"],
"extends": ["matrix-org/ts"],
"extends": ["plugin:matrix-org/typescript"],
"rules": {
// TypeScript has its own version of this
"babel/no-invalid-this": "off",

"quotes": "off",
},
}, {
"files": ["src/interfaces/**/*.ts"],
"rules": {
"@typescript-eslint/no-empty-object-type": "off",
},
}],
};
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,26 @@
"@action-validator/core": "^0.5.3",
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/eslint-parser": "^7.25.9",
"@babel/eslint-plugin": "^7.25.9",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/preset-env": "^7.11.5",
"@babel/preset-typescript": "^7.10.4",
"@casualbot/jest-sonar-reporter": "^2.2.7",
"@stylistic/eslint-plugin": "^2.10.1",
"@testing-library/dom": "^8.0.0",
"@types/jest": "^29.5.12",
"@types/node": "^18.16.0",
"babel-eslint": "^10.1.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"browserify": "^17.0.0",
"eslint": "^7.8.1",
"eslint-config-matrix-org": "^0.1.2",
"eslint": "^8.0.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-matrix-org": "^2.0.0",
"eslint-plugin-unicorn": "^56.0.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"rimraf": "^3.0.2",
Expand Down
62 changes: 32 additions & 30 deletions src/ClientWidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { EventEmitter } from "events";

import { ITransport } from "./transport/ITransport";
import { Widget } from "./models/Widget";
import { PostmessageTransport } from "./transport/PostmessageTransport";
Expand Down Expand Up @@ -213,12 +214,12 @@ export class ClientWidgetApi extends EventEmitter {
return this.allowedEvents.some(e => e.matchesAsRoomAccountData(EventDirection.Receive, eventType));
}

public stop() {
public stop(): void {
this.isStopped = true;
this.transport.stop();
}

private beginCapabilities() {
private beginCapabilities(): void {
// widget has loaded - tell all the listeners that
this.emit("preparing");

Expand All @@ -239,18 +240,18 @@ export class ClientWidgetApi extends EventEmitter {
});
}

private notifyCapabilities(requested: Capability[]) {
private notifyCapabilities(requested: Capability[]): void {
this.transport.send(WidgetApiToWidgetAction.NotifyCapabilities, <INotifyCapabilitiesActionRequestData>{
requested: requested,
approved: Array.from(this.allowedCapabilities),
}).catch(e => {
console.warn("non-fatal error notifying widget of approved capabilities:", e);
}).then(() => {
this.emit("capabilitiesNotified")
this.emit("capabilitiesNotified");
});
}

private onIframeLoad(ev: Event) {
private onIframeLoad(ev: Event): void {
if (this.widget.waitForIframeLoad) {
// If the widget is set to waitForIframeLoad the capabilities immediatly get setup after load.
// The client does not wait for the ContentLoaded action.
Expand All @@ -268,7 +269,7 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private handleContentLoadedAction(action: IContentLoadedActionRequest) {
private handleContentLoadedAction(action: IContentLoadedActionRequest): void {
if (this.contentLoadedWaitTimer !== undefined) {
clearTimeout(this.contentLoadedWaitTimer);
this.contentLoadedWaitTimer = undefined;
Expand All @@ -291,13 +292,13 @@ export class ClientWidgetApi extends EventEmitter {
this.contentLoadedActionSent = true;
}

private replyVersions(request: ISupportedVersionsActionRequest) {
private replyVersions(request: ISupportedVersionsActionRequest): void {
this.transport.reply<ISupportedVersionsActionResponseData>(request, {
supported_versions: CurrentApiVersions,
});
}

private handleCapabilitiesRenegotiate(request: IRenegotiateCapabilitiesActionRequest) {
private handleCapabilitiesRenegotiate(request: IRenegotiateCapabilitiesActionRequest): void {
// acknowledge first
this.transport.reply<IWidgetApiAcknowledgeResponseData>(request, {});

Expand All @@ -318,7 +319,7 @@ export class ClientWidgetApi extends EventEmitter {
});
}

private handleNavigate(request: INavigateActionRequest) {
private handleNavigate(request: INavigateActionRequest): void {
if (!this.hasCapability(MatrixCapabilities.MSC2931Navigate)) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Missing capability"},
Expand All @@ -331,7 +332,7 @@ export class ClientWidgetApi extends EventEmitter {
});
}

const onErr = (e: unknown) => {
const onErr = (e: unknown): void => {
console.error("[ClientWidgetApi] Failed to handle navigation: ", e);
this.handleDriverError(e, request, "Error handling navigation");
};
Expand All @@ -345,10 +346,10 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private handleOIDC(request: IGetOpenIDActionRequest) {
private handleOIDC(request: IGetOpenIDActionRequest): void {
let phase = 1; // 1 = initial request, 2 = after user manual confirmation

const replyState = (state: OpenIDRequestState, credential?: IOpenIDCredentials) => {
const replyState = (state: OpenIDRequestState, credential?: IOpenIDCredentials): void | Promise<IWidgetApiAcknowledgeResponseData> => {
credential = credential || {};
if (phase > 1) {
return this.transport.send<IOpenIDCredentialsActionRequestData>(
Expand All @@ -367,7 +368,7 @@ export class ClientWidgetApi extends EventEmitter {
}
};

const replyError = (msg: string) => {
const replyError = (msg: string): void | Promise<IWidgetApiAcknowledgeResponseData> => {
console.error("[ClientWidgetApi] Failed to handle OIDC: ", msg);
if (phase > 1) {
// We don't have a way to indicate that a random error happened in this flow, so
Expand Down Expand Up @@ -405,7 +406,7 @@ export class ClientWidgetApi extends EventEmitter {

this.driver.askOpenID(observer);
}
private handleReadRoomAccountData(request: IReadRoomAccountDataFromWidgetActionRequest) {
private handleReadRoomAccountData(request: IReadRoomAccountDataFromWidgetActionRequest): void | Promise<void> {
let events: Promise<IRoomAccountData[]> = Promise.resolve([]);
events = this.driver.readRoomAccountData(request.data.type);

Expand All @@ -416,11 +417,11 @@ export class ClientWidgetApi extends EventEmitter {
}

return events.then((evs) => {
this.transport.reply<IReadRoomAccountDataFromWidgetResponseData>(request, {events: evs})
this.transport.reply<IReadRoomAccountDataFromWidgetResponseData>(request, {events: evs});
});
}

private handleReadEvents(request: IReadEventFromWidgetActionRequest) {
private handleReadEvents(request: IReadEventFromWidgetActionRequest): void | Promise<void> {
if (!request.data.type) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Invalid request - missing event type"},
Expand All @@ -434,9 +435,10 @@ export class ClientWidgetApi extends EventEmitter {

let askRoomIds: string[] | null = null; // null denotes current room only
if (request.data.room_ids) {
askRoomIds = request.data.room_ids as string[];
if (!Array.isArray(askRoomIds)) {
askRoomIds = [askRoomIds as any as string];
if (Array.isArray(request.data.room_ids)) {
askRoomIds = request.data.room_ids;
} else {
askRoomIds = [request.data.room_ids];
}
for (const roomId of askRoomIds) {
if (!this.canUseRoomTimeline(roomId)) {
Expand Down Expand Up @@ -471,7 +473,7 @@ export class ClientWidgetApi extends EventEmitter {
return events.then(evs => this.transport.reply<IReadEventFromWidgetResponseData>(request, {events: evs}));
}

private handleSendEvent(request: ISendEventFromWidgetActionRequest) {
private handleSendEvent(request: ISendEventFromWidgetActionRequest): void {
if (!request.data.type) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Invalid request - missing event type"},
Expand Down Expand Up @@ -559,7 +561,7 @@ export class ClientWidgetApi extends EventEmitter {
});
}

private handleUpdateDelayedEvent(request: IUpdateDelayedEventFromWidgetActionRequest) {
private handleUpdateDelayedEvent(request: IUpdateDelayedEventFromWidgetActionRequest): void {
if (!request.data.delay_id) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Invalid request - missing delay_id"},
Expand Down Expand Up @@ -618,7 +620,7 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private async pollTurnServers(turnServers: AsyncGenerator<ITurnServer>, initialServer: ITurnServer) {
private async pollTurnServers(turnServers: AsyncGenerator<ITurnServer>, initialServer: ITurnServer): Promise<void> {
try {
await this.transport.send<IUpdateTurnServersRequestData>(
WidgetApiToWidgetAction.UpdateTurnServers,
Expand Down Expand Up @@ -683,7 +685,7 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private async handleReadRelations(request: IReadRelationsFromWidgetActionRequest) {
private async handleReadRelations(request: IReadRelationsFromWidgetActionRequest): Promise<void> {
if (!request.data.event_id) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Invalid request - missing event ID" },
Expand Down Expand Up @@ -732,7 +734,7 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private async handleUserDirectorySearch(request: IUserDirectorySearchFromWidgetActionRequest) {
private async handleUserDirectorySearch(request: IUserDirectorySearchFromWidgetActionRequest): Promise<void> {
if (!this.hasCapability(MatrixCapabilities.MSC3973UserDirectorySearch)) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Missing capability" },
Expand Down Expand Up @@ -773,15 +775,15 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private async handleGetMediaConfig(request: IGetMediaConfigActionFromWidgetActionRequest) {
private async handleGetMediaConfig(request: IGetMediaConfigActionFromWidgetActionRequest): Promise<void> {
if (!this.hasCapability(MatrixCapabilities.MSC4039UploadFile)) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Missing capability" },
});
}

try {
const result = await this.driver.getMediaConfig()
const result = await this.driver.getMediaConfig();

return this.transport.reply<IGetMediaConfigActionFromWidgetResponseData>(
request,
Expand All @@ -793,7 +795,7 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private async handleUploadFile(request: IUploadFileActionFromWidgetActionRequest) {
private async handleUploadFile(request: IUploadFileActionFromWidgetActionRequest): Promise<void> {
if (!this.hasCapability(MatrixCapabilities.MSC4039UploadFile)) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Missing capability" },
Expand Down Expand Up @@ -833,7 +835,7 @@ export class ClientWidgetApi extends EventEmitter {
}
}

private handleDriverError(e: unknown, request: IWidgetApiRequest, message: string) {
private handleDriverError(e: unknown, request: IWidgetApiRequest, message: string): void {
const data = this.driver.processError(e);
this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {
Expand All @@ -843,7 +845,7 @@ export class ClientWidgetApi extends EventEmitter {
});
}

private handleMessage(ev: CustomEvent<IWidgetApiRequest>) {
private handleMessage(ev: CustomEvent<IWidgetApiRequest>): void | Promise<void> {
if (this.isStopped) return;
const actionEv = new CustomEvent(`action:${ev.detail.action}`, {
detail: ev.detail,
Expand Down Expand Up @@ -875,7 +877,7 @@ export class ClientWidgetApi extends EventEmitter {
case WidgetApiFromWidgetAction.MSC3869ReadRelations:
return this.handleReadRelations(<IReadRelationsFromWidgetActionRequest>ev.detail);
case WidgetApiFromWidgetAction.MSC3973UserDirectorySearch:
return this.handleUserDirectorySearch(<IUserDirectorySearchFromWidgetActionRequest>ev.detail)
return this.handleUserDirectorySearch(<IUserDirectorySearchFromWidgetActionRequest>ev.detail);
case WidgetApiFromWidgetAction.BeeperReadRoomAccountData:
return this.handleReadRoomAccountData(<IReadRoomAccountDataFromWidgetActionRequest>ev.detail);
case WidgetApiFromWidgetAction.MSC4039GetMediaConfigAction:
Expand Down
Loading

0 comments on commit ccd40e2

Please sign in to comment.