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

Prefer const enums due to typescript being stricter with them #3415

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ module.exports = {
additionalTestBlockFunctions: ["beforeAll", "beforeEach", "oldBackendOnly"],
},
],

// Prefer const enums
"no-restricted-syntax": [
"error",
{
selector: "TSEnumDeclaration:not([const=true])",
message: "Don't declare non-const enums",
},
],
},
overrides: [
{
Expand Down
2 changes: 1 addition & 1 deletion spec/test-utils/SyncResponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ISyncResponder {
sendOrQueueSyncResponse(response: object): void;
}

enum SyncResponderState {
const enum SyncResponderState {
IDLE,
WAITING_FOR_REQUEST,
WAITING_FOR_RESPONSE,
Expand Down
13 changes: 7 additions & 6 deletions spec/unit/crypto/verification/sas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import "../../../olm-loader";
import { makeTestClients } from "./util";
import { MatrixEvent } from "../../../../src/models/event";
import { ISasEvent, SAS, SasEvent } from "../../../../src/crypto/verification/SAS";
import { ISasEvent, SAS } from "../../../../src/crypto/verification/SAS";
import { DeviceInfo, IDevice } from "../../../../src/crypto/deviceinfo";
import { CryptoEvent, verificationMethods } from "../../../../src/crypto";
import * as olmlib from "../../../../src/crypto/olmlib";
Expand All @@ -28,6 +28,7 @@ import { IVerificationChannel } from "../../../../src/crypto/verification/reques
import { MatrixClient } from "../../../../src";
import { VerificationRequest } from "../../../../src/crypto/verification/request/VerificationRequest";
import { TestClient } from "../../../TestClient";
import { VerifierEvent } from "../../../../src/crypto-api/verification";

const Olm = global.Olm;

Expand Down Expand Up @@ -134,7 +135,7 @@ describe("SAS verification", function () {

bobPromise = new Promise<VerificationBase<any, any>>((resolve, reject) => {
bob.client.on(CryptoEvent.VerificationRequest, (request) => {
(<SAS>request.verifier!).on(SasEvent.ShowSas, (e) => {
(<SAS>request.verifier!).on(VerifierEvent.ShowSas, (e) => {
if (!e.sas.emoji || !e.sas.decimal) {
e.cancel();
} else if (!aliceSasEvent) {
Expand All @@ -159,7 +160,7 @@ describe("SAS verification", function () {
bob.client.getUserId()!,
bob.deviceId!,
) as SAS;
aliceVerifier.on(SasEvent.ShowSas, (e) => {
aliceVerifier.on(VerifierEvent.ShowSas, (e) => {
if (!e.sas.emoji || !e.sas.decimal) {
e.cancel();
} else if (!bobSasEvent) {
Expand Down Expand Up @@ -418,7 +419,7 @@ describe("SAS verification", function () {

const bobPromise = new Promise<VerificationBase<any, any>>((resolve, reject) => {
bob.client.on(CryptoEvent.VerificationRequest, (request) => {
(<SAS>request.verifier!).on(SasEvent.ShowSas, (e) => {
(<SAS>request.verifier!).on(VerifierEvent.ShowSas, (e) => {
e.mismatch();
});
resolve(request.verifier!);
Expand Down Expand Up @@ -509,7 +510,7 @@ describe("SAS verification", function () {
bobPromise = new Promise<void>((resolve, reject) => {
bob.client.on(CryptoEvent.VerificationRequest, async (request) => {
const verifier = request.beginKeyVerification(SAS.NAME) as SAS;
verifier.on(SasEvent.ShowSas, (e) => {
verifier.on(VerifierEvent.ShowSas, (e) => {
if (!e.sas.emoji || !e.sas.decimal) {
e.cancel();
} else if (!aliceSasEvent) {
Expand All @@ -533,7 +534,7 @@ describe("SAS verification", function () {
const aliceRequest = await alice.client.requestVerificationDM(bob.client.getUserId()!, "!room_id");
await aliceRequest.waitFor((r) => r.started);
aliceVerifier = aliceRequest.verifier! as SAS;
aliceVerifier.on(SasEvent.ShowSas, (e) => {
aliceVerifier.on(VerifierEvent.ShowSas, (e) => {
if (!e.sas.emoji || !e.sas.decimal) {
e.cancel();
} else if (!bobSasEvent) {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/queueToDevice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const FAKE_MSG = {
payload: FAKE_PAYLOAD,
};

enum StoreType {
const enum StoreType {
Memory = "Memory",
IndexedDB = "IndexedDB",
}
Expand Down
12 changes: 6 additions & 6 deletions src/@types/PushRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ limitations under the License.
// allow camelcase as these are things that go onto the wire
/* eslint-disable camelcase */

export enum PushRuleActionName {
export const enum PushRuleActionName {
DontNotify = "dont_notify",
Notify = "notify",
Coalesce = "coalesce",
}

export enum TweakName {
export const enum TweakName {
Highlight = "highlight",
Sound = "sound",
}
Expand All @@ -38,7 +38,7 @@ export type TweakSound = Tweak<TweakName.Sound, string>;

export type Tweaks = TweakHighlight | TweakSound;

export enum ConditionOperator {
export const enum ConditionOperator {
ExactEquals = "==",
LessThan = "<",
GreaterThan = ">",
Expand All @@ -60,7 +60,7 @@ export function isDmMemberCountCondition(condition: AnyMemberCountCondition): bo
return condition === "==2" || condition === "2";
}

export enum ConditionKind {
export const enum ConditionKind {
EventMatch = "event_match",
EventPropertyIs = "event_property_is",
EventPropertyContains = "event_property_contains",
Expand Down Expand Up @@ -127,15 +127,15 @@ export type PushRuleCondition =
| ICallStartedCondition
| ICallStartedPrefixCondition;

export enum PushRuleKind {
export const enum PushRuleKind {
Override = "override",
ContentSpecific = "content",
RoomSpecific = "room",
SenderSpecific = "sender",
Underride = "underride",
}

export enum RuleId {
export const enum RuleId {
Master = ".m.rule.master",
IsUserMention = ".org.matrix.msc3952.is_user_mention",
IsRoomMention = ".org.matrix.msc3952.is_room_mention",
Expand Down
4 changes: 2 additions & 2 deletions src/@types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface ISSOFlow extends ILoginFlow {
[DELEGATED_OIDC_COMPATIBILITY.altName]?: boolean;
}

export enum IdentityProviderBrand {
export const enum IdentityProviderBrand {
Gitlab = "gitlab",
Github = "github",
Apple = "apple",
Expand Down Expand Up @@ -92,7 +92,7 @@ export interface ILoginParams {
}
/* eslint-enable camelcase */

export enum SSOAction {
export const enum SSOAction {
/** The user intends to login to an existing account */
LOGIN = "login",

Expand Down
8 changes: 4 additions & 4 deletions src/@types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { UnstableValue } from "../NamespacedValue";

export enum EventType {
export const enum EventType {
// Room state events
RoomCanonicalAlias = "m.room.canonical_alias",
RoomCreate = "m.room.create",
Expand Down Expand Up @@ -95,14 +95,14 @@ export enum EventType {
GroupCallMemberPrefix = "org.matrix.msc3401.call.member",
}

export enum RelationType {
export const enum RelationType {
Annotation = "m.annotation",
Replace = "m.replace",
Reference = "m.reference",
Thread = "m.thread",
}

export enum MsgType {
export const enum MsgType {
Text = "m.text",
Emote = "m.emote",
Notice = "m.notice",
Expand All @@ -116,7 +116,7 @@ export enum MsgType {

export const RoomCreateTypeField = "type";

export enum RoomType {
export const enum RoomType {
Space = "m.space",
UnstableCall = "org.matrix.msc3417.call",
ElementVideo = "io.element.video",
Expand Down
2 changes: 1 addition & 1 deletion src/@types/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { EitherAnd } from "matrix-events-sdk";
import { UnstableValue } from "../NamespacedValue";
import { M_TEXT } from "./extensible_events";

export enum LocationAssetType {
export const enum LocationAssetType {
Self = "m.self",
Pin = "m.pin",
}
Expand Down
12 changes: 6 additions & 6 deletions src/@types/partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export interface IImageInfo {
h?: number;
}

export enum Visibility {
export const enum Visibility {
Public = "public",
Private = "private",
}

export enum Preset {
export const enum Preset {
PrivateChat = "private_chat",
TrustedPrivateChat = "trusted_private_chat",
PublicChat = "public_chat",
Expand All @@ -44,7 +44,7 @@ export type ResizeMethod = "crop" | "scale";
export type IdServerUnbindResult = "no-support" | "success";

// Knock and private are reserved keywords which are not yet implemented.
export enum JoinRule {
export const enum JoinRule {
Public = "public",
Invite = "invite",
/**
Expand All @@ -55,7 +55,7 @@ export enum JoinRule {
Restricted = "restricted",
}

export enum RestrictedAllowType {
export const enum RestrictedAllowType {
RoomMembership = "m.room_membership",
}

Expand All @@ -67,12 +67,12 @@ export interface IJoinRuleEventContent {
}[];
}

export enum GuestAccess {
export const enum GuestAccess {
CanJoin = "can_join",
Forbidden = "forbidden",
}

export enum HistoryVisibility {
export const enum HistoryVisibility {
Invited = "invited",
Joined = "joined",
Shared = "shared",
Expand Down
2 changes: 1 addition & 1 deletion src/@types/read_receipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

export enum ReceiptType {
export const enum ReceiptType {
Read = "m.read",
FullyRead = "m.fully_read",
ReadPrivate = "m.read.private",
Expand Down
4 changes: 2 additions & 2 deletions src/@types/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface ISearchResult {
context: IResultContext;
}

enum GroupKey {
const enum GroupKey {
RoomId = "room_id",
Sender = "sender",
}
Expand Down Expand Up @@ -76,7 +76,7 @@ interface IResultCategories {

export type SearchKey = "content.body" | "content.name" | "content.topic";

export enum SearchOrderBy {
export const enum SearchOrderBy {
Recent = "recent",
Rank = "rank",
}
Expand Down
2 changes: 1 addition & 1 deletion src/@types/threepids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

export enum ThreepidMedium {
export const enum ThreepidMedium {
Email = "email",
Phone = "msisdn",
}
Expand Down
4 changes: 3 additions & 1 deletion src/autodiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import { MatrixError, Method, timeoutSignal } from "./http-api";
// Dev note: Auto discovery is part of the spec.
// See: https://matrix.org/docs/spec/client_server/r0.4.0.html#server-discovery

export enum AutoDiscoveryAction {
export const enum AutoDiscoveryAction {
SUCCESS = "SUCCESS",
IGNORE = "IGNORE",
PROMPT = "PROMPT",
FAIL_PROMPT = "FAIL_PROMPT",
FAIL_ERROR = "FAIL_ERROR",
}

// Allow this non-const enum
// eslint-disable-next-line no-restricted-syntax
enum AutoDiscoveryError {
Invalid = "Invalid homeserver discovery response",
GenericFailure = "Failed to get autodiscovery configuration from server",
Expand Down
8 changes: 4 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export interface IMatrixClientCreateOpts extends ICreateClientOpts {
usingExternalCrypto?: boolean;
}

export enum PendingEventOrdering {
export const enum PendingEventOrdering {
Chronological = "chronological",
Detached = "detached",
}
Expand Down Expand Up @@ -478,7 +478,7 @@ export interface IStartClientOpts {

export interface IStoredClientOpts extends IStartClientOpts {}

export enum RoomVersionStability {
export const enum RoomVersionStability {
Stable = "stable",
Unstable = "unstable",
}
Expand Down Expand Up @@ -521,7 +521,7 @@ export interface ICrossSigningKey {
user_id: string;
}

enum CrossSigningKeyType {
const enum CrossSigningKeyType {
MasterKey = "master_key",
SelfSigningKey = "self_signing_key",
UserSigningKey = "user_signing_key",
Expand Down Expand Up @@ -879,7 +879,7 @@ interface IWhoamiResponse {
// Probably not the most graceful solution but does a good enough job for now
const EVENT_ID_PREFIX = "$";

export enum ClientEvent {
export const enum ClientEvent {
Sync = "sync",
Event = "event",
ToDeviceEvent = "toDeviceEvent",
Expand Down
2 changes: 1 addition & 1 deletion src/crypto-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { DeviceMap } from "./models/device";
import { UIAuthCallback } from "./interactive-auth";

/** Types of cross-signing key */
export enum CrossSigningKey {
export const enum CrossSigningKey {
Master = "master",
SelfSigning = "self_signing",
UserSigning = "user_signing",
Expand Down
2 changes: 1 addition & 1 deletion src/crypto-api/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import { MatrixEvent } from "../models/event";

/** Events emitted by `Verifier`. */
export enum VerifierEvent {
export const enum VerifierEvent {
/**
* The verification has been cancelled, by us or the other side.
*
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/CrossSigning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ function deviceToObject(device: DeviceInfo, userId: string): DeviceObject {
};
}

export enum CrossSigningLevel {
export const enum CrossSigningLevel {
MASTER = 4,
USER_SIGNING = 2,
SELF_SIGNING = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { CryptoEvent, CryptoEventHandlerMap } from "./index";
*/

// constants for DeviceList.deviceTrackingStatus
export enum TrackingStatus {
export const enum TrackingStatus {
NotTracked,
PendingDownload,
DownloadInProgress,
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/OutgoingRoomKeyRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const SEND_KEY_REQUESTS_DELAY_MS = 500;
* (deleted) <---------------------------+
* ```
*/
export enum RoomKeyRequestState {
export const enum RoomKeyRequestState {
/** request not yet sent */
Unsent,
/** request sent, awaiting reply */
Expand Down
Loading