Skip to content

Commit

Permalink
Use Prettier to format code (#542)
Browse files Browse the repository at this point in the history
* add prettier

* run prettier

* update prettierignore

* add check prettier when linting
  • Loading branch information
H-Shay authored Oct 9, 2024
1 parent 6e26231 commit 943f878
Show file tree
Hide file tree
Showing 104 changed files with 4,487 additions and 2,688 deletions.
21 changes: 21 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/lib

/.idea
.vscode
.vscode/

/.github
/config
/docs
/src/appservice/config

/*.log
package-lock.json
yarn.lock
CONTRIBUTING.md
README.md
mx-tester.yml
package.json
releasing.md
tsconfig.json
tslint.json
6 changes: 6 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
printWidth: 120,
tabWidth: 4,
quoteProps: "consistent",
trailingComma: "all",
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"build": "tsc",
"postbuild": "rm -rf lib/test/ && cp -r lib/src/* lib/ && rm -rf lib/src/",
"lint": "tslint --project ./tsconfig.json -t stylish",
"lint": "tslint --project ./tsconfig.json -t stylish && npx prettier . --check",
"start:dev": "yarn build && node --async-stack-traces lib/index.js",
"test": "ts-mocha --project ./tsconfig.json test/commands/**/*.ts",
"test:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --require test/integration/fixtures.ts --timeout 300000 --project ./tsconfig.json \"test/integration/**/*Test.ts\"",
Expand Down
7 changes: 3 additions & 4 deletions src/ErrorCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ const TRIGGER_INTERVALS: { [key: string]: number } = {
* The ErrorCache is an attempt to make sure the error is reported only once.
*/
export default class ErrorCache {
private roomsToErrors: Map<string/*room id*/, Map<string /*error kind*/, number>> = new Map();
private roomsToErrors: Map<string /*room id*/, Map<string /*error kind*/, number>> = new Map();

constructor() {
}
constructor() {}

/**
* Reset the error cache for a room/kind in the situation where circumstances have changed e.g. if Mjolnir has been informed via sync of a `m.room.power_levels` event in the room, we would want to clear `ERROR_KIND_PERMISSION`
Expand Down Expand Up @@ -68,7 +67,7 @@ export default class ErrorCache {
const now = new Date().getTime();
const interval = TRIGGER_INTERVALS[kind];

if ((now - lastTriggerTime) >= interval) {
if (now - lastTriggerTime >= interval) {
triggers.set(kind, now);
return true;
} else {
Expand Down
48 changes: 36 additions & 12 deletions src/ManagementRoomOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ limitations under the License.
*/

import * as Sentry from "@sentry/node";
import { extractRequestError, LogLevel, LogService, MessageType, Permalinks, TextualMessageEventContent, UserID } from "@vector-im/matrix-bot-sdk";
import {
extractRequestError,
LogLevel,
LogService,
MessageType,
Permalinks,
TextualMessageEventContent,
UserID,
} from "@vector-im/matrix-bot-sdk";
import { IConfig } from "./config";
import { MatrixSendClient } from "./MatrixEmitter";
import { htmlEscape } from "./utils";
Expand All @@ -31,14 +39,11 @@ const levelToFn = {
* Allows the different componenets of mjolnir to send messages back to the management room without introducing a dependency on the entirity of a `Mjolnir` instance.
*/
export default class ManagementRoomOutput {

constructor(
private readonly managementRoomId: string,
private readonly client: MatrixSendClient,
private readonly config: IConfig,
) {

}
) {}

/**
* Take an arbitrary string and a set of room IDs, and return a
Expand All @@ -52,7 +57,11 @@ export default class ManagementRoomOutput {
* @param msgtype The desired message type of the returned TextualMessageEventContent
* @returns A TextualMessageEventContent with replaced room IDs
*/
private async replaceRoomIdsWithPills(text: string, roomIds: Set<string>, msgtype: MessageType = "m.text"): Promise<TextualMessageEventContent> {
private async replaceRoomIdsWithPills(
text: string,
roomIds: Set<string>,
msgtype: MessageType = "m.text",
): Promise<TextualMessageEventContent> {
const content: TextualMessageEventContent = {
body: text,
formatted_body: htmlEscape(text),
Expand All @@ -62,24 +71,33 @@ export default class ManagementRoomOutput {

// Though spec doesn't say so, room ids that have slashes in them are accepted by Synapse and Dendrite unfortunately for us.
const escapeRegex = (v: string): string => {
return v.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
return v.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
};

const viaServers = [(new UserID(await this.client.getUserId())).domain];
const viaServers = [new UserID(await this.client.getUserId()).domain];
for (const roomId of roomIds) {
let alias = roomId;
try {
alias = (await this.client.getPublishedAlias(roomId)) || roomId;
} catch (e) {
// This is a recursive call, so tell the function not to try and call us
await this.logMessage(LogLevel.WARN, "utils", `Failed to resolve room alias for ${roomId} - see console for details`, null, true);
await this.logMessage(
LogLevel.WARN,
"utils",
`Failed to resolve room alias for ${roomId} - see console for details`,
null,
true,
);
LogService.warn("utils", extractRequestError(e));
}
const regexRoomId = new RegExp(escapeRegex(roomId), "g");
content.body = content.body.replace(regexRoomId, alias);
if (content.formatted_body) {
const permalink = Permalinks.forRoom(alias, alias !== roomId ? [] : viaServers);
content.formatted_body = content.formatted_body.replace(regexRoomId, `<a href="${permalink}">${htmlEscape(alias)}</a>`);
content.formatted_body = content.formatted_body.replace(
regexRoomId,
`<a href="${permalink}">${htmlEscape(alias)}</a>`,
);
}
}

Expand All @@ -95,9 +113,15 @@ export default class ManagementRoomOutput {
* @param additionalRoomIds The roomIds in the message that we want to be replaced by room pills.
* @param isRecursive Whether logMessage is being called from logMessage.
*/
public async logMessage(level: LogLevel, module: string, message: string | any, additionalRoomIds: string[] | string | null = null, isRecursive = false): Promise<any> {
public async logMessage(
level: LogLevel,
module: string,
message: string | any,
additionalRoomIds: string[] | string | null = null,
isRecursive = false,
): Promise<any> {
if (level === LogLevel.ERROR) {
Sentry.captureMessage(`${module}: ${message}`, 'error');
Sentry.captureMessage(`${module}: ${message}`, "error");
}
if (!additionalRoomIds) additionalRoomIds = [];
if (!Array.isArray(additionalRoomIds)) additionalRoomIds = [additionalRoomIds];
Expand Down
24 changes: 12 additions & 12 deletions src/MatrixEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ import { MatrixClient } from "@vector-im/matrix-bot-sdk";
* when we're in appservice mode.
*/
export declare interface MatrixEmitter extends EventEmitter {
on(event: 'room.event', listener: (roomId: string, mxEvent: any) => void ): this
emit(event: 'room.event', roomId: string, mxEvent: any): boolean
on(event: "room.event", listener: (roomId: string, mxEvent: any) => void): this;
emit(event: "room.event", roomId: string, mxEvent: any): boolean;

on(event: 'room.message', listener: (roomId: string, mxEvent: any) => void ): this
emit(event: 'room.message', roomId: string, mxEvent: any): boolean
on(event: "room.message", listener: (roomId: string, mxEvent: any) => void): this;
emit(event: "room.message", roomId: string, mxEvent: any): boolean;

on(event: 'room.invite', listener: (roomId: string, mxEvent: any) => void ): this
emit(event: 'room.invite', roomId: string, mxEvent: any): boolean
on(event: "room.invite", listener: (roomId: string, mxEvent: any) => void): this;
emit(event: "room.invite", roomId: string, mxEvent: any): boolean;

on(event: 'room.join', listener: (roomId: string, mxEvent: any) => void ): this
emit(event: 'room.join', roomId: string, mxEvent: any): boolean
on(event: "room.join", listener: (roomId: string, mxEvent: any) => void): this;
emit(event: "room.join", roomId: string, mxEvent: any): boolean;

on(event: 'room.leave', listener: (roomId: string, mxEvent: any) => void ): this
emit(event: 'room.leave', roomId: string, mxEvent: any): boolean
on(event: "room.leave", listener: (roomId: string, mxEvent: any) => void): this;
emit(event: "room.leave", roomId: string, mxEvent: any): boolean;

on(event: 'room.archived', listener: (roomId: string, mxEvent: any) => void ): this
emit(event: 'room.archived', roomId: string, mxEvent: any): boolean
on(event: "room.archived", listener: (roomId: string, mxEvent: any) => void): this;
emit(event: "room.archived", roomId: string, mxEvent: any): boolean;

start(): Promise<void>;
stop(): void;
Expand Down
Loading

0 comments on commit 943f878

Please sign in to comment.