fix: add prefer-readonly rule (#5122)

This commit is contained in:
LongYinan 2023-11-29 04:44:25 +00:00
parent e9ea67bd38
commit 7a7cbc45d7
No known key found for this signature in database
GPG Key ID: C3666B7FC82ADAD7
21 changed files with 39 additions and 36 deletions

View File

@ -270,6 +270,7 @@ const config = {
},
],
'@typescript-eslint/no-misused-promises': ['error'],
'@typescript-eslint/prefer-readonly': 'error',
'i/no-extraneous-dependencies': ['error'],
'react-hooks/exhaustive-deps': [
'warn',

View File

@ -14,7 +14,7 @@ const TrivialExceptions = [NotFoundException];
@Catch()
export class ExceptionLogger implements ExceptionFilter {
private logger = new Logger('ExceptionLogger');
private readonly logger = new Logger('ExceptionLogger');
catch(exception: Error, host: ArgumentsHost) {
// with useGlobalFilters, the context is always HTTP

View File

@ -53,8 +53,8 @@ class AuthGuard implements CanActivate {
constructor(
@Inject(NextAuthOptionsProvide)
private readonly nextAuthOptions: NextAuthOptions,
private auth: AuthService,
private prisma: PrismaService,
private readonly auth: AuthService,
private readonly prisma: PrismaService,
private readonly reflector: Reflector
) {}

View File

@ -28,9 +28,9 @@ export const getUtcTimestamp = () => Math.floor(Date.now() / 1000);
@Injectable()
export class AuthService {
constructor(
private config: Config,
private prisma: PrismaService,
private mailer: MailService
private readonly config: Config,
private readonly prisma: PrismaService,
private readonly mailer: MailService
) {}
sign(user: UserClaim) {

View File

@ -60,9 +60,9 @@ const MAX_SEQ_NUM = 0x3fffffff; // u31
*/
@Injectable()
export class DocManager implements OnModuleInit, OnModuleDestroy {
private logger = new Logger(DocManager.name);
private readonly logger = new Logger(DocManager.name);
private job: NodeJS.Timeout | null = null;
private seqMap = new Map<string, number>();
private readonly seqMap = new Map<string, number>();
private busy = false;
constructor(

View File

@ -16,7 +16,7 @@ export class DocID {
raw: string;
workspace: string;
variant: DocVariant;
private sub: string | null;
private readonly sub: string | null;
static parse(raw: string): DocID | null {
try {

View File

@ -24,7 +24,7 @@ if (typeof window !== 'undefined') {
}
export class DebugLogger {
private _debug: debug.Debugger;
private readonly _debug: debug.Debugger;
constructor(namespace: string) {
this._debug = debug(namespace);

View File

@ -1,7 +1,7 @@
import { assertExists } from '@blocksuite/global/utils';
export class UaHelper {
private uaMap;
private readonly uaMap;
public isLinux = false;
public isMacOs = false;
public isSafari = false;

View File

@ -45,8 +45,8 @@ export abstract class HandlerManager<
Handlers extends Record<string, PrimitiveHandlers>,
> {
static instance: HandlerManager<string, Record<string, PrimitiveHandlers>>;
private _app: App<Namespace, Handlers>;
private _namespace: Namespace;
private readonly _app: App<Namespace, Handlers>;
private readonly _namespace: Namespace;
private _handlers: Handlers;
constructor() {

View File

@ -4,9 +4,11 @@ import { typesystem } from './typesystem';
type MatcherData<Data, Type extends TType = TType> = { type: Type; data: Data };
export class Matcher<Data, Type extends TType = TType> {
private list: MatcherData<Data, Type>[] = [];
private readonly list: MatcherData<Data, Type>[] = [];
constructor(private _match?: (type: Type, target: TType) => boolean) {}
constructor(
private readonly _match?: (type: Type, target: TType) => boolean
) {}
register(type: Type, data: Data) {
this.list.push({ type, data });

View File

@ -92,8 +92,8 @@ export type ValueOfData<T extends DataDefine> = T extends DataDefine<infer R>
export class DataDefine<Data extends DataTypeShape = Record<string, unknown>> {
constructor(
private config: DataDefineConfig<Data>,
private dataMap: Map<string, DataDefine>
private readonly config: DataDefineConfig<Data>,
private readonly dataMap: Map<string, DataDefine>
) {}
create(data?: Data): TDataType<Data> {

View File

@ -5,8 +5,8 @@ import type { Map as YMap } from 'yjs';
import { Doc as YDoc } from 'yjs';
export class UserSetting {
constructor(
private workspace: Workspace,
private userId: string
private readonly workspace: Workspace,
private readonly userId: string
) {}
get setting(): YDoc {

View File

@ -13,7 +13,7 @@ const COLLECTIONS_TRASH_KEY = 'collections_trash';
const SETTING_KEY = 'setting';
export class WorkspaceSetting {
constructor(private workspace: Workspace) {}
constructor(private readonly workspace: Workspace) {}
get doc() {
return this.workspace.doc;

View File

@ -33,7 +33,7 @@ function pickAndBind<T extends object, U extends keyof T>(
class HelperProcessManager {
ready: Promise<void>;
#process: UtilityProcess;
readonly #process: UtilityProcess;
// a rpc server for the main process -> helper process
rpc?: _AsyncVersionOf<HelperToMain>;

View File

@ -29,7 +29,7 @@ const hrefRegExp = /\/tag\/([^/]+)$/;
export class CustomGitHubProvider extends BaseGitHubProvider<GithubUpdateInfo> {
constructor(
options: CustomPublishOptions,
private updater: AppUpdater,
private readonly updater: AppUpdater,
runtimeOptions: ProviderRuntimeOptions
) {
super(options as unknown as GithubOptions, 'github.com', runtimeOptions);

View File

@ -22,7 +22,7 @@ interface MessagePortLike {
}
export class MessageEventChannel implements EventBasedChannel {
constructor(private worker: MessagePortLike) {}
constructor(private readonly worker: MessagePortLike) {}
on(listener: (data: unknown) => void) {
const f = (data: unknown) => {

View File

@ -54,7 +54,7 @@ export class TraceReporter {
private spansCache = new Array<TraceSpan>();
private reportIntervalId: number | undefined | NodeJS.Timeout;
private reportInterval = 60_000;
private readonly reportInterval = 60_000;
private static instance: TraceReporter;
@ -175,7 +175,7 @@ export class TraceReporter {
};
}
private initTraceReport = () => {
private readonly initTraceReport = () => {
if (!this.reportIntervalId && TraceReporter.shouldReportTrace) {
if (typeof window !== 'undefined') {
this.reportIntervalId = window.setInterval(
@ -191,7 +191,7 @@ export class TraceReporter {
}
};
private reportHandler = () => {
private readonly reportHandler = () => {
if (this.spansCache.length <= 0) {
clearInterval(this.reportIntervalId);
this.reportIntervalId = undefined;

View File

@ -5,8 +5,8 @@ const logger = new DebugLogger('affine:blob-engine');
export class BlobEngine {
constructor(
private local: BlobStorage,
private remotes: BlobStorage[]
private readonly local: BlobStorage,
private readonly remotes: BlobStorage[]
) {}
get storages() {

View File

@ -15,12 +15,12 @@ interface SyncUpdateSender {
* - retryable, allow retry when previous sync request failed but with retry flag been set to true
*/
export class BatchSyncSender {
private buffered: Uint8Array[] = [];
private readonly buffered: Uint8Array[] = [];
private job: Promise<void> | null = null;
private started = true;
constructor(
private guid: string,
private readonly guid: string,
private readonly rawSender: SyncUpdateSender
) {}

View File

@ -63,9 +63,9 @@ export class SyncEngine {
private abort = new AbortController();
constructor(
private rootDoc: Doc,
private local: Storage,
private remotes: Storage[]
private readonly rootDoc: Doc,
private readonly local: Storage,
private readonly remotes: Storage[]
) {
this._status = {
step: SyncEngineStep.Stopped,

View File

@ -69,8 +69,8 @@ export class SyncPeer {
logger = new DebugLogger('affine:sync-peer:' + this.name);
constructor(
private rootDoc: Doc,
private storage: Storage
private readonly rootDoc: Doc,
private readonly storage: Storage
) {
this.logger.debug('peer start');
@ -150,7 +150,7 @@ export class SyncPeer {
}
}
private state: {
private readonly state: {
connectedDocs: Map<string, Doc>;
pushUpdatesQueue: AsyncQueue<{
docId: string;