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

Make SonarCloud happier #2850

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/@types/PushRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export interface IPusher {
lang: string;
profile_tag?: string;
pushkey: string;
enabled?: boolean | null | undefined;
"org.matrix.msc3881.enabled"?: boolean | null | undefined;
enabled?: boolean | null;
"org.matrix.msc3881.enabled"?: boolean | null;
device_id?: string | null;
"org.matrix.msc3881.device_id"?: string | null;
}
Expand Down
24 changes: 6 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2930,7 +2930,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
throw new Error("End-to-end encryption disabled");
}

const path = this.makeKeyBackupPath(roomId!, sessionId!, version!);
const path = this.makeKeyBackupPath(roomId!, sessionId!, version);
await this.http.authedRequest(
Method.Put, path.path, path.queryData, data,
{ prefix: ClientPrefix.V3 },
Expand Down Expand Up @@ -3284,7 +3284,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
throw new Error("End-to-end encryption disabled");
}

const path = this.makeKeyBackupPath(roomId!, sessionId!, version!);
const path = this.makeKeyBackupPath(roomId!, sessionId!, version);
await this.http.authedRequest(
Method.Delete, path.path, path.queryData, undefined,
{ prefix: ClientPrefix.V3 },
Expand Down Expand Up @@ -4181,7 +4181,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

return this.sendEvent(
roomId,
threadId as (string | null),
threadId as string | null,
eventType,
sendContent,
txnId,
Expand Down Expand Up @@ -5225,7 +5225,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
];

// Here we handle non-thread timelines only, but still process any thread events to populate thread summaries.
let timeline = timelineSet.getTimelineForEvent(events[0].getId()!);
let timeline = timelineSet.getTimelineForEvent(events[0].getId());
if (timeline) {
timeline.getState(EventTimeline.BACKWARDS)!.setUnknownStateEvents(res.state.map(mapper));
} else {
Expand Down Expand Up @@ -6115,15 +6115,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// There can be only room-kind push rule per room
// and its id is the room id.
if (this.pushRules) {
if (!this.pushRules[scope] || !this.pushRules[scope].room) {
return;
}
for (let i = 0; i < this.pushRules[scope].room.length; i++) {
const rule = this.pushRules[scope].room[i];
if (rule.rule_id === roomId) {
return rule;
}
}
return this.pushRules[scope]?.room?.find(rule => rule.rule_id === roomId);
} else {
throw new Error(
"SyncApi.sync() must be done before accessing to push rules.",
Expand Down Expand Up @@ -6473,8 +6465,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// create a new filter
const createdFilter = await this.createFilter(filter.getDefinition());

// debuglog("Created new filter ID %s: %s", createdFilter.filterId,
// JSON.stringify(createdFilter.getDefinition()));
this.store.setFilterIdByName(filterName, createdFilter.filterId);
return createdFilter.filterId!;
}
Expand Down Expand Up @@ -8434,9 +8424,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
keyAlgorithm = "signed_curve25519";
}

for (let i = 0; i < devices.length; ++i) {
const userId = devices[i][0];
const deviceId = devices[i][1];
for (const [userId, deviceId] of devices) {
const query = queries[userId] || {};
queries[userId] = query;
query[deviceId] = keyAlgorithm;
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class DeviceList extends TypedEventEmitter<EmittedEvents, CryptoEventHand
}, delay);
}

return savePromise!;
return savePromise;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/OlmDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ export class OlmDevice {
log,
);

return info!;
return info;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/crypto/algorithms/olm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,10 @@ class OlmEncryption extends EncryptionAlgorithm {

const promises: Promise<void>[] = [];

for (let i = 0; i < users.length; ++i) {
const userId = users[i];
for (const userId of users) {
const devices = this.crypto.getStoredDevicesForUser(userId) || [];

for (let j = 0; j < devices.length; ++j) {
const deviceInfo = devices[j];
for (const deviceInfo of devices) {
const key = deviceInfo.getIdentityKey();
if (key == this.olmDevice.deviceCurve25519Key) {
// don't bother sending to ourself
Expand Down Expand Up @@ -304,8 +302,7 @@ class OlmDecryption extends DecryptionAlgorithm {

// try each session in turn.
const decryptionErrors: Record<string, string> = {};
for (let i = 0; i < sessionIds.length; i++) {
const sessionId = sessionIds[i];
for (const sessionId of sessionIds) {
try {
const payload = await this.olmDevice.decryptMessage(
theirDeviceIdentityKey, sessionId, message.type, message.body,
Expand Down
3 changes: 1 addition & 2 deletions src/crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,7 @@ export class Curve25519 implements BackupAlgorithm {
const backupPubKey = decryption.init_with_private_key(privKey);

if (backupPubKey !== this.authData.public_key) {
// eslint-disable-next-line no-throw-literal
throw { errcode: MatrixClient.RESTORE_BACKUP_ERROR_BAD_KEY };
throw new MatrixError({ errcode: MatrixClient.RESTORE_BACKUP_ERROR_BAD_KEY });
}

const keys: IMegolmSessionData[] = [];
Expand Down
12 changes: 4 additions & 8 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2379,9 +2379,8 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
*/
public async getOlmSessionsForUser(userId: string): Promise<Record<string, IUserOlmSession>> {
const devices = this.getStoredDevicesForUser(userId) || [];
const result = {};
for (let j = 0; j < devices.length; ++j) {
const device = devices[j];
const result: { [deviceId: string]: IUserOlmSession } = {};
for (const device of devices) {
const deviceKey = device.getIdentityKey();
const sessions = await this.olmDevice.getSessionInfoForDevice(deviceKey);

Expand Down Expand Up @@ -2682,14 +2681,11 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
): Promise<Record<string, Record<string, olmlib.IOlmSessionResult>>> {
const devicesByUser: Record<string, DeviceInfo[]> = {};

for (let i = 0; i < users.length; ++i) {
const userId = users[i];
for (const userId of users) {
devicesByUser[userId] = [];

const devices = this.getStoredDevicesForUser(userId) || [];
for (let j = 0; j < devices.length; ++j) {
const deviceInfo = devices[j];

for (const deviceInfo of devices) {
const key = deviceInfo.getIdentityKey();
if (key == this.olmDevice.deviceCurve25519Key) {
// don't bother setting up session to ourself
Expand Down
3 changes: 1 addition & 2 deletions src/crypto/olmlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ export async function ensureOlmSessionsForDevices(
const promises: Promise<void>[] = [];
for (const [userId, devices] of Object.entries(devicesByUser)) {
const userRes = otkResult[userId] || {};
for (let j = 0; j < devices.length; j++) {
const deviceInfo = devices[j];
for (const deviceInfo of devices) {
const deviceId = deviceInfo.deviceId;
const key = deviceInfo.getIdentityKey();

Expand Down
3 changes: 1 addition & 2 deletions src/filter-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ export class FilterComponent {
},
};

for (let n = 0; n < Object.keys(literalKeys).length; n++) {
const name = Object.keys(literalKeys)[n];
for (const name in literalKeys) {
const matchFunc = literalKeys[name];
const notName = "not_" + name;
const disallowedValues: string[] = this.filterJson[notName];
Expand Down
7 changes: 1 addition & 6 deletions src/interactive-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,6 @@ export class InteractiveAuth {
*/
private firstUncompletedStage(flow: IFlow): AuthType | undefined {
const completed = this.data.completed || [];
for (let i = 0; i < flow.stages.length; ++i) {
const stageType = flow.stages[i];
if (completed.indexOf(stageType) === -1) {
return stageType;
}
}
return flow.stages.find(stageType => !completed.includes(stageType));
}
}
3 changes: 1 addition & 2 deletions src/models/event-timeline-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ export class EventTimelineSet extends TypedEventEmitter<EmittedEvents, EventTime

let didUpdate = false;
let lastEventWasNew = false;
for (let i = 0; i < events.length; i++) {
const event = events[i];
for (const event of events) {
const eventId = event.getId()!;

const existingTimeline = this._eventIdToTimeline.get(eventId);
Expand Down
30 changes: 13 additions & 17 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,8 +1008,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
* Removing just the old live timeline whilst preserving previous ones is not supported.
*/
public resetLiveTimeline(backPaginationToken?: string | null, forwardPaginationToken?: string | null): void {
for (let i = 0; i < this.timelineSets.length; i++) {
this.timelineSets[i].resetLiveTimeline(
for (const timelineSet of this.timelineSets) {
timelineSet.resetLiveTimeline(
backPaginationToken ?? undefined,
forwardPaginationToken ?? undefined,
);
Expand Down Expand Up @@ -2128,8 +2128,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
const { duplicateStrategy, timelineWasEmpty, fromCache } = addLiveEventOptions;

// add to our timeline sets
for (let i = 0; i < this.timelineSets.length; i++) {
this.timelineSets[i].addLiveEvent(event, {
for (const timelineSet of this.timelineSets) {
timelineSet.addLiveEvent(event, {
duplicateStrategy,
fromCache,
timelineWasEmpty,
Expand Down Expand Up @@ -2214,8 +2214,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
}
}
} else {
for (let i = 0; i < this.timelineSets.length; i++) {
const timelineSet = this.timelineSets[i];
for (const timelineSet of this.timelineSets) {
if (timelineSet.getFilter()) {
if (timelineSet.getFilter()!.filterRoomTimeline([event]).length) {
timelineSet.addEventToTimeline(event,
Expand Down Expand Up @@ -2322,9 +2321,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
thread?.timelineSet.handleRemoteEcho(localEvent, oldEventId, newEventId);

if (shouldLiveInRoom) {
for (let i = 0; i < this.timelineSets.length; i++) {
const timelineSet = this.timelineSets[i];

for (const timelineSet of this.timelineSets) {
// if it's already in the timeline, update the timeline map. If it's not, add it.
timelineSet.handleRemoteEcho(localEvent, oldEventId, newEventId);
}
Expand Down Expand Up @@ -2407,8 +2404,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
// if the event was already in the timeline (which will be the case if
// opts.pendingEventOrdering==chronological), we need to update the
// timeline map.
for (let i = 0; i < this.timelineSets.length; i++) {
this.timelineSets[i].replaceEventId(oldEventId, newEventId!);
for (const timelineSet of this.timelineSets) {
timelineSet.replaceEventId(oldEventId, newEventId!);
}
}
} else if (newStatus == EventStatus.CANCELLED) {
Expand Down Expand Up @@ -2643,8 +2640,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
* @param {String[]} eventIds A list of eventIds to remove.
*/
public removeEvents(eventIds: string[]): void {
for (let i = 0; i < eventIds.length; ++i) {
this.removeEvent(eventIds[i]);
for (const eventId of eventIds) {
this.removeEvent(eventId);
}
}

Expand All @@ -2657,8 +2654,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
*/
public removeEvent(eventId: string): boolean {
let removedAny = false;
for (let i = 0; i < this.timelineSets.length; i++) {
const removed = this.timelineSets[i].removeEvent(eventId);
for (const timelineSet of this.timelineSets) {
const removed = timelineSet.removeEvent(eventId);
if (removed) {
if (removed.isRedaction()) {
this.revertRedactionLocalEcho(removed);
Expand Down Expand Up @@ -2740,8 +2737,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
* @param {Array<MatrixEvent>} events an array of account_data events to add
*/
public addAccountData(events: MatrixEvent[]): void {
for (let i = 0; i < events.length; i++) {
const event = events[i];
for (const event of events) {
if (event.getType() === "m.tag") {
this.addTags(event);
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ export class User extends TypedEventEmitter<UserEvent, UserEventHandlerMap> {

this.updateModifiedTime();

for (let i = 0; i < eventsToFire.length; i++) {
this.emit(eventsToFire[i], event, this);
for (const eventToFire of eventsToFire) {
this.emit(eventToFire, event, this);
}
}

Expand Down
20 changes: 4 additions & 16 deletions src/pushprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ export class PushProcessor {
*/
public static actionListToActionsObject(actionList: PushRuleAction[]): IActionsObject {
const actionObj: IActionsObject = { notify: false, tweaks: {} };
for (let i = 0; i < actionList.length; ++i) {
const action = actionList[i];
for (const action of actionList) {
if (action === PushRuleActionName.Notify) {
actionObj.notify = true;
} else if (typeof action === 'object') {
Expand Down Expand Up @@ -190,15 +189,13 @@ export class PushProcessor {
private static cachedGlobToRegex: Record<string, RegExp> = {}; // $glob: RegExp

private matchingRuleFromKindSet(ev: MatrixEvent, kindset: PushRuleSet): IAnnotatedPushRule | null {
for (let ruleKindIndex = 0; ruleKindIndex < RULEKINDS_IN_ORDER.length; ++ruleKindIndex) {
const kind = RULEKINDS_IN_ORDER[ruleKindIndex];
for (const kind of RULEKINDS_IN_ORDER) {
const ruleset = kindset[kind];
if (!ruleset) {
continue;
}

for (let ruleIndex = 0; ruleIndex < ruleset.length; ++ruleIndex) {
const rule = ruleset[ruleIndex];
for (const rule of ruleset) {
if (!rule.enabled) {
continue;
}
Expand Down Expand Up @@ -478,16 +475,7 @@ export class PushProcessor {
}

public ruleMatchesEvent(rule: Partial<IPushRule> & Pick<IPushRule, "conditions">, ev: MatrixEvent): boolean {
if (!rule.conditions?.length) return true;

let ret = true;
for (let i = 0; i < rule.conditions.length; ++i) {
const cond = rule.conditions[i];
// @ts-ignore
ret &= this.eventFulfillsCondition(cond, ev);
}
//console.log("Rule "+rule.rule_id+(ret ? " matches" : " doesn't match"));
return ret;
return !rule.conditions?.some(cond => !this.eventFulfillsCondition(cond, ev));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/realtime-callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function clearTimeout(key: number): void {
}

// remove the element from the list
let i;
let i: number;
for (i = 0; i < callbackList.length; i++) {
const cb = callbackList[i];
if (cb.key == key) {
Expand Down Expand Up @@ -137,7 +137,6 @@ function scheduleRealCallback(): void {
}

function runCallbacks(): void {
let cb: Callback;
const timestamp = Date.now();
debuglog("runCallbacks: now:", timestamp);

Expand All @@ -149,7 +148,7 @@ function runCallbacks(): void {
if (!first || first.runAt > timestamp) {
break;
}
cb = callbackList.shift()!;
const cb = callbackList.shift()!;
debuglog("runCallbacks: popping", cb.key);
callbacksToRun.push(cb);
}
Expand All @@ -159,8 +158,7 @@ function runCallbacks(): void {
// register their own setTimeouts.
scheduleRealCallback();

for (let i = 0; i < callbacksToRun.length; i++) {
cb = callbacksToRun[i];
for (const cb of callbacksToRun) {
try {
cb.func.apply(global, cb.params);
} catch (e) {
Expand Down
Loading