Skip to content

Commit 8863e42

Browse files
authored
More typescript linting (#3310)
* More typescript linting * Improve types Signed-off-by: Michael Telatynski <[email protected]> * Discard changes to src/models/MSC3089TreeSpace.ts * Discard changes to src/realtime-callbacks.ts * Fix tests Signed-off-by: Michael Telatynski <[email protected]> * Improve coverage Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent bc52469 commit 8863e42

File tree

17 files changed

+58
-47
lines changed

17 files changed

+58
-47
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module.exports = {
138138
tryExtensions: [".ts"],
139139
},
140140
],
141+
"no-extra-boolean-cast": "error",
141142
},
142143
},
143144
{

spec/integ/matrix-client-methods.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,28 @@ describe("MatrixClient", function () {
19151915
return prom;
19161916
});
19171917
});
1918+
1919+
describe("getDomain", () => {
1920+
it("should return null if no userId is set", () => {
1921+
const client = new MatrixClient({ baseUrl: "http://localhost" });
1922+
expect(client.getDomain()).toBeNull();
1923+
});
1924+
1925+
it("should return the domain of the userId", () => {
1926+
expect(client.getDomain()).toBe("localhost");
1927+
});
1928+
});
1929+
1930+
describe("getUserIdLocalpart", () => {
1931+
it("should return null if no userId is set", () => {
1932+
const client = new MatrixClient({ baseUrl: "http://localhost" });
1933+
expect(client.getUserIdLocalpart()).toBeNull();
1934+
});
1935+
1936+
it("should return the localpart of the userId", () => {
1937+
expect(client.getUserIdLocalpart()).toBe("alice");
1938+
});
1939+
});
19181940
});
19191941

19201942
function withThreadId(event: MatrixEvent, newThreadId: string): MatrixEvent {

spec/test-utils/webrtc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,9 @@ export class MockCallMatrixClient extends TypedEventEmitter<EmittedEvents, Emitt
481481
public getUserId(): string {
482482
return this.userId;
483483
}
484+
public getSafeUserId(): string {
485+
return this.userId;
486+
}
484487

485488
public getDeviceId(): string {
486489
return this.deviceId;

spec/unit/webrtc/groupCall.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ describe("Group Call", function () {
14301430
let client: MatrixClient;
14311431

14321432
beforeEach(() => {
1433-
client = new MatrixClient({ baseUrl: "base_url" });
1433+
client = new MatrixClient({ baseUrl: "base_url", userId: "my_user_id" });
14341434

14351435
jest.spyOn(client, "sendStateEvent").mockResolvedValue({} as any);
14361436
});

src/@types/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ declare global {
6565
interface Navigator {
6666
// We check for the webkit-prefixed getUserMedia to detect if we're
6767
// on webkit: we should check if we still need to do this
68-
webkitGetUserMedia: DummyInterfaceWeShouldntBeUsingThis;
68+
webkitGetUserMedia?: DummyInterfaceWeShouldntBeUsingThis;
6969
}
7070
}

src/autodiscovery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class AutoDiscovery {
130130
* configuration, which may include error states. Rejects on unexpected
131131
* failure, not when verification fails.
132132
*/
133-
public static async fromDiscoveryConfig(wellknown: IClientWellKnown): Promise<ClientConfig> {
133+
public static async fromDiscoveryConfig(wellknown?: IClientWellKnown): Promise<ClientConfig> {
134134
// Step 1 is to get the config, which is provided to us here.
135135

136136
// We default to an error state to make the first few checks easier to

src/client.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,10 +1832,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
18321832
* @returns MXID for the logged-in user, or null if not logged in
18331833
*/
18341834
public getUserId(): string | null {
1835-
if (this.credentials && this.credentials.userId) {
1836-
return this.credentials.userId;
1837-
}
1838-
return null;
1835+
return this.credentials?.userId ?? null;
18391836
}
18401837

18411838
/**
@@ -1857,7 +1854,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
18571854
* @returns Domain of this MXID
18581855
*/
18591856
public getDomain(): string | null {
1860-
if (this.credentials && this.credentials.userId) {
1857+
if (this.credentials?.userId) {
18611858
return this.credentials.userId.replace(/^.*?:/, "");
18621859
}
18631860
return null;
@@ -1868,10 +1865,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
18681865
* @returns The user ID localpart or null.
18691866
*/
18701867
public getUserIdLocalpart(): string | null {
1871-
if (this.credentials && this.credentials.userId) {
1872-
return this.credentials.userId.split(":")[0].substring(1);
1873-
}
1874-
return null;
1868+
return this.credentials?.userId?.split(":")[0].substring(1) ?? null;
18751869
}
18761870

18771871
/**
@@ -4313,7 +4307,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
43134307
*/
43144308
public getIgnoredUsers(): string[] {
43154309
const event = this.getAccountData("m.ignored_user_list");
4316-
if (!event || !event.getContent() || !event.getContent()["ignored_users"]) return [];
4310+
if (!event?.getContent()["ignored_users"]) return [];
43174311
return Object.keys(event.getContent()["ignored_users"]);
43184312
}
43194313

src/crypto/EncryptionSetup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class EncryptionSetupBuilder {
106106
if (!this.keySignatures) {
107107
this.keySignatures = {};
108108
}
109-
const userSignatures = this.keySignatures[userId] || {};
109+
const userSignatures = this.keySignatures[userId] ?? {};
110110
this.keySignatures[userId] = userSignatures;
111111
userSignatures[deviceId] = signature;
112112
}

src/crypto/dehydration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import decryptAESSecretStorageItem from "../utils/decryptAESSecretStorageItem.ts
2727
import encryptAESSecretStorageItem from "../utils/encryptAESSecretStorageItem.ts";
2828

2929
export interface IDehydratedDevice {
30-
device_id: string; // eslint-disable-line camelcase
31-
device_data: SecretStorageKeyDescription & {
30+
device_id?: string; // eslint-disable-line camelcase
31+
device_data?: SecretStorageKeyDescription & {
3232
// eslint-disable-line camelcase
3333
algorithm: string;
3434
account: string; // pickle
@@ -90,7 +90,7 @@ export class DehydrationManager {
9090
}
9191

9292
public async setKey(
93-
key: Uint8Array,
93+
key?: Uint8Array,
9494
keyInfo: { [props: string]: any } = {},
9595
deviceDisplayName?: string,
9696
): Promise<boolean | undefined> {

src/crypto/store/localStorage-crypto-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore implements Crypto
162162
func: (session: ISessionInfo) => void,
163163
): void {
164164
const sessions = this._getEndToEndSessions(deviceKey);
165-
func(sessions[sessionId] || {});
165+
func(sessions[sessionId] ?? {});
166166
}
167167

168168
public getEndToEndSessions(
169169
deviceKey: string,
170170
txn: unknown,
171171
func: (sessions: { [sessionId: string]: ISessionInfo }) => void,
172172
): void {
173-
func(this._getEndToEndSessions(deviceKey) || {});
173+
func(this._getEndToEndSessions(deviceKey) ?? {});
174174
}
175175

176176
public getAllEndToEndSessions(txn: unknown, func: (session: ISessionInfo) => void): void {

0 commit comments

Comments
 (0)