1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-23 22:36:32 +02:00

Chore: Improve type safety (#12992)

This commit is contained in:
Henry Heino
2025-08-19 22:33:10 -07:00
committed by GitHub
parent 30000c34ec
commit f5882ecfcc
4 changed files with 15 additions and 12 deletions

View File

@@ -214,7 +214,7 @@ class BaseModel {
return fields.indexOf(name) >= 0;
}
public static fieldNames(withPrefix = false) {
public static fieldNames(withPrefix: string|boolean = false) {
const output = this.db().tableFieldNames(this.tableName());
if (!withPrefix) return output;

View File

@@ -596,7 +596,7 @@ export default class Synchronizer {
if (this.cancelling()) break;
let local = locals[i];
const ItemClass: typeof BaseItem = BaseItem.itemClass(local);
const ItemClass = BaseItem.itemClass(local);
const path = BaseItem.systemPath(local);
// Safety check to avoid infinite loops.

View File

@@ -125,7 +125,7 @@ export default class BaseItem extends BaseModel {
}
// Need to dynamically load the classes like this to avoid circular dependencies
public static getClass(name: string) {
public static getClass<T extends typeof BaseItem>(name: string): T {
for (let i = 0; i < BaseItem.syncItemDefinitions_.length; i++) {
if (BaseItem.syncItemDefinitions_[i].className === name) {
const classRef = BaseItem.syncItemDefinitions_[i].classRef;
@@ -710,8 +710,7 @@ export default class BaseItem extends BaseModel {
// // CHANGED:
// 'SELECT * FROM [ITEMS] items JOIN sync_items s ON s.item_id = items.id WHERE sync_target = ? AND'
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
let extraWhere: any = [];
let extraWhere: string[]|string = [];
if (className === 'Note') extraWhere.push('is_conflict = 0');
if (className === 'Resource') extraWhere.push('encryption_blob_encrypted = 0');
if (ItemClass.encryptionSupported()) extraWhere.push('encryption_applied = 0');
@@ -774,8 +773,7 @@ export default class BaseItem extends BaseModel {
changedItems = await ItemClass.modelSelectAll(sql);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
const neverSyncedItemIds = neverSyncedItem.map((it: any) => it.id);
const neverSyncedItemIds = neverSyncedItem.map((it: BaseItemEntity) => it.id);
const items = neverSyncedItem.concat(changedItems);
if (i >= classNames.length - 1) {

View File

@@ -1,6 +1,7 @@
import BaseModel, { DeleteOptions, ModelType } from '../BaseModel';
import BaseItem from './BaseItem';
import type FolderClass from './Folder';
import type ResourceClass from './Resource';
import ItemChange from './ItemChange';
import Setting from './Setting';
import shim from '../shim';
@@ -9,7 +10,6 @@ import markdownUtils from '../markdownUtils';
import { FolderEntity, NoteEntity } from '../services/database/types';
import Tag from './Tag';
const { sprintf } = require('sprintf-js');
import Resource from './Resource';
import syncDebugLog from '../services/synchronizer/syncDebugLog';
import { toFileProtocolPath, toForwardSlashes } from '../path-utils';
const { pregQuote, substrWithEllipsis } = require('../string-utils.js');
@@ -181,7 +181,7 @@ export default class Note extends BaseItem {
// this.logger().debug('replaceResourceInternalToExternalLinks', 'options:', options, 'body:', body);
const resourceIds = await this.linkedResourceIds(body);
const Resource = this.getClass('Resource');
const Resource = this.getClass<typeof ResourceClass>('Resource');
for (let i = 0; i < resourceIds.length; i++) {
const id = resourceIds[i];
@@ -216,6 +216,7 @@ export default class Note extends BaseItem {
pathsToTry.push(`file://${shim.pathRelativeToCwd(resourceDir)}`);
pathsToTry.push(`file:///${shim.pathRelativeToCwd(resourceDir)}`);
} else {
const Resource = this.getClass<typeof ResourceClass>('Resource');
pathsToTry.push(Resource.baseRelativeDirectoryPath());
}
@@ -242,6 +243,7 @@ export default class Note extends BaseItem {
pathsToTry = temp;
// this.logger().debug('replaceResourceExternalToInternalLinks', 'options:', options, 'pathsToTry:', pathsToTry);
const Resource = this.getClass<typeof ResourceClass>('Resource');
for (const basePath of pathsToTry) {
const reStrings = [
@@ -408,7 +410,7 @@ export default class Note extends BaseItem {
}
}
const Folder: typeof FolderClass = BaseItem.getClass('Folder');
const Folder = BaseItem.getClass<typeof FolderClass>('Folder');
const parentFolder: FolderEntity = await Folder.load(parentId, { fields: ['id', 'deleted_time'] });
const parentInTrash = parentFolder ? !!parentFolder.deleted_time : false;
@@ -615,7 +617,8 @@ export default class Note extends BaseItem {
}
public static async copyToFolder(noteId: string, folderId: string) {
if (folderId === this.getClass('Folder').conflictFolderId()) throw new Error(_('Cannot copy note to "%s" notebook', this.getClass('Folder').conflictFolderTitle()));
const Folder = this.getClass<typeof FolderClass>('Folder');
if (folderId === Folder.conflictFolderId()) throw new Error(_('Cannot copy note to "%s" notebook', Folder.conflictFolderTitle()));
return Note.duplicate(noteId, {
changes: {
@@ -627,7 +630,8 @@ export default class Note extends BaseItem {
}
public static async moveToFolder(noteId: string, folderId: string, saveOptions: SaveOptions|null = null) {
if (folderId === this.getClass('Folder').conflictFolderId()) throw new Error(_('Cannot move note to "%s" notebook', this.getClass('Folder').conflictFolderTitle()));
const Folder = this.getClass<typeof FolderClass>('Folder');
if (folderId === Folder.conflictFolderId()) throw new Error(_('Cannot move note to "%s" notebook', Folder.conflictFolderTitle()));
// When moving a note to a different folder, the user timestamp is not
// updated. However updated_time is updated so that the note can be
@@ -702,6 +706,7 @@ export default class Note extends BaseItem {
private static async duplicateNoteResources(noteBody: string): Promise<string> {
const resourceIds = await this.linkedResourceIds(noteBody);
let newBody: string = noteBody;
const Resource = this.getClass<typeof ResourceClass>('Resource');
for (const resourceId of resourceIds) {
const newResource = await Resource.duplicateResource(resourceId);