1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-24 23:26:50 +02:00

Tools: Apply rule @typescript-eslint/type-annotation-spacing

This commit is contained in:
Laurent Cozic
2020-11-12 19:13:28 +00:00
parent 62feb7ff60
commit d20694e52c
291 changed files with 2205 additions and 2203 deletions

View File

@ -30,7 +30,7 @@ class BaseModel {
// TODO: This ancient part of Joplin about model types is a bit of a
// mess and should be refactored properly.
public static typeEnum_:any[] = [
public static typeEnum_: any[] = [
['TYPE_NOTE', ModelType.Note],
['TYPE_FOLDER', ModelType.Folder],
['TYPE_SETTING', ModelType.Setting],
@ -66,24 +66,24 @@ class BaseModel {
public static TYPE_SMART_FILTER = ModelType.SmartFilter;
public static TYPE_COMMAND = ModelType.Command;
protected static dispatch:Function = function() {};
private static saveMutexes_:any = {};
protected static dispatch: Function = function() {};
private static saveMutexes_: any = {};
private static db_:any;
private static db_: any;
static modelType():ModelType {
static modelType(): ModelType {
throw new Error('Must be overriden');
}
static tableName():string {
static tableName(): string {
throw new Error('Must be overriden');
}
static setDb(db:any) {
static setDb(db: any) {
this.db_ = db;
}
static addModelMd(model:any):any {
static addModelMd(model: any): any {
if (!model) return model;
if (Array.isArray(model)) {
@ -107,29 +107,29 @@ class BaseModel {
return false;
}
static byId(items:any[], id:string) {
static byId(items: any[], id: string) {
for (let i = 0; i < items.length; i++) {
if (items[i].id == id) return items[i];
}
return null;
}
static defaultValues(fieldNames:string[]) {
const output:any = {};
static defaultValues(fieldNames: string[]) {
const output: any = {};
for (const n of fieldNames) {
output[n] = this.db().fieldDefaultValue(this.tableName(), n);
}
return output;
}
static modelIndexById(items:any[], id:string) {
static modelIndexById(items: any[], id: string) {
for (let i = 0; i < items.length; i++) {
if (items[i].id == id) return i;
}
return -1;
}
static modelsByIds(items:any[], ids:string[]) {
static modelsByIds(items: any[], ids: string[]) {
const output = [];
for (let i = 0; i < items.length; i++) {
if (ids.indexOf(items[i].id) >= 0) {
@ -141,14 +141,14 @@ class BaseModel {
// Prefer the use of this function to compare IDs as it handles the case where
// one ID is null and the other is "", in which case they are actually considered to be the same.
static idsEqual(id1:string, id2:string) {
static idsEqual(id1: string, id2: string) {
if (!id1 && !id2) return true;
if (!id1 && !!id2) return false;
if (!!id1 && !id2) return false;
return id1 === id2;
}
static modelTypeToName(type:number) {
static modelTypeToName(type: number) {
for (let i = 0; i < BaseModel.typeEnum_.length; i++) {
const e = BaseModel.typeEnum_[i];
if (e[1] === type) return e[0].substr(5).toLowerCase();
@ -156,7 +156,7 @@ class BaseModel {
throw new Error(`Unknown model type: ${type}`);
}
static modelNameToType(name:string) {
static modelNameToType(name: string) {
for (let i = 0; i < BaseModel.typeEnum_.length; i++) {
const e = BaseModel.typeEnum_[i];
const eName = e[0].substr(5).toLowerCase();
@ -165,12 +165,12 @@ class BaseModel {
throw new Error(`Unknown model name: ${name}`);
}
static hasField(name:string) {
static hasField(name: string) {
const fields = this.fieldNames();
return fields.indexOf(name) >= 0;
}
static fieldNames(withPrefix:boolean = false) {
static fieldNames(withPrefix: boolean = false) {
const output = this.db().tableFieldNames(this.tableName());
if (!withPrefix) return output;
@ -183,7 +183,7 @@ class BaseModel {
return temp;
}
static fieldType(name:string, defaultValue:any = null) {
static fieldType(name: string, defaultValue: any = null) {
const fields = this.fields();
for (let i = 0; i < fields.length; i++) {
if (fields[i].name == name) return fields[i].type;
@ -196,8 +196,8 @@ class BaseModel {
return this.db().tableFields(this.tableName());
}
static removeUnknownFields(model:any) {
const newModel:any = {};
static removeUnknownFields(model: any) {
const newModel: any = {};
for (const n in model) {
if (!model.hasOwnProperty(n)) continue;
if (!this.hasField(n) && n !== 'type_') continue;
@ -208,7 +208,7 @@ class BaseModel {
static new() {
const fields = this.fields();
const output:any = {};
const output: any = {};
for (let i = 0; i < fields.length; i++) {
const f = fields[i];
output[f.name] = f.default;
@ -216,7 +216,7 @@ class BaseModel {
return output;
}
static modOptions(options:any) {
static modOptions(options: any) {
if (!options) {
options = {};
} else {
@ -228,30 +228,30 @@ class BaseModel {
return options;
}
static count(options:any = null) {
static count(options: any = null) {
if (!options) options = {};
let sql = `SELECT count(*) as total FROM \`${this.tableName()}\``;
if (options.where) sql += ` WHERE ${options.where}`;
return this.db()
.selectOne(sql)
.then((r:any) => {
.then((r: any) => {
return r ? r['total'] : 0;
});
}
static load(id:string, options:any = null) {
static load(id: string, options: any = null) {
return this.loadByField('id', id, options);
}
static shortId(id:string) {
static shortId(id: string) {
return id.substr(0, 5);
}
static loadByPartialId(partialId:string) {
static loadByPartialId(partialId: string) {
return this.modelSelectAll(`SELECT * FROM \`${this.tableName()}\` WHERE \`id\` LIKE ?`, [`${partialId}%`]);
}
static applySqlOptions(options:any, sql:string, params:any[] = null) {
static applySqlOptions(options: any, sql: string, params: any[] = null) {
if (!options) options = {};
if (options.order && options.order.length) {
@ -271,18 +271,18 @@ class BaseModel {
return { sql: sql, params: params };
}
static async allIds(options:any = null) {
static async allIds(options: any = null) {
const q = this.applySqlOptions(options, `SELECT id FROM \`${this.tableName()}\``);
const rows = await this.db().selectAll(q.sql, q.params);
return rows.map((r:any) => r.id);
return rows.map((r: any) => r.id);
}
static async all(options:any = null) {
static async all(options: any = null) {
if (!options) options = {};
if (!options.fields) options.fields = '*';
let sql = `SELECT ${this.db().escapeFields(options.fields)} FROM \`${this.tableName()}\``;
let params:any[] = [];
let params: any[] = [];
if (options.where) {
sql += ` WHERE ${options.where}`;
if (options.whereParams) params = params.concat(options.whereParams);
@ -292,7 +292,7 @@ class BaseModel {
return this.modelSelectAll(q.sql, q.params);
}
static async byIds(ids:string[], options:any = null) {
static async byIds(ids: string[], options: any = null) {
if (!ids.length) return [];
if (!options) options = {};
if (!options.fields) options.fields = '*';
@ -303,7 +303,7 @@ class BaseModel {
return this.modelSelectAll(q.sql);
}
static async search(options:any = null) {
static async search(options: any = null) {
if (!options) options = {};
if (!options.fields) options.fields = '*';
@ -325,25 +325,25 @@ class BaseModel {
return this.modelSelectAll(query.sql, query.params);
}
static modelSelectOne(sql:string, params:any[] = null) {
static modelSelectOne(sql: string, params: any[] = null) {
if (params === null) params = [];
return this.db()
.selectOne(sql, params)
.then((model:any) => {
.then((model: any) => {
return this.filter(this.addModelMd(model));
});
}
static modelSelectAll(sql:string, params:any[] = null) {
static modelSelectAll(sql: string, params: any[] = null) {
if (params === null) params = [];
return this.db()
.selectAll(sql, params)
.then((models:any[]) => {
.then((models: any[]) => {
return this.filterArray(this.addModelMd(models));
});
}
static loadByField(fieldName:string, fieldValue:any, options:any = null) {
static loadByField(fieldName: string, fieldValue: any, options: any = null) {
if (!options) options = {};
if (!('caseInsensitive' in options)) options.caseInsensitive = false;
if (!options.fields) options.fields = '*';
@ -352,7 +352,7 @@ class BaseModel {
return this.modelSelectOne(sql, [fieldValue]);
}
static loadByFields(fields:string[], options:any = null) {
static loadByFields(fields: string[], options: any = null) {
if (!options) options = {};
if (!('caseInsensitive' in options)) options.caseInsensitive = false;
if (!options.fields) options.fields = '*';
@ -367,12 +367,12 @@ class BaseModel {
return this.modelSelectOne(sql, params);
}
static loadByTitle(fieldValue:any) {
static loadByTitle(fieldValue: any) {
return this.modelSelectOne(`SELECT * FROM \`${this.tableName()}\` WHERE \`title\` = ?`, [fieldValue]);
}
static diffObjects(oldModel:any, newModel:any) {
const output:any = {};
static diffObjects(oldModel: any, newModel: any) {
const output: any = {};
const fields = this.diffObjectsFields(oldModel, newModel);
for (let i = 0; i < fields.length; i++) {
output[fields[i]] = newModel[fields[i]];
@ -381,7 +381,7 @@ class BaseModel {
return output;
}
static diffObjectsFields(oldModel:any, newModel:any) {
static diffObjectsFields(oldModel: any, newModel: any) {
const output = [];
for (const n in newModel) {
if (!newModel.hasOwnProperty(n)) continue;
@ -393,15 +393,15 @@ class BaseModel {
return output;
}
static modelsAreSame(oldModel:any, newModel:any) {
static modelsAreSame(oldModel: any, newModel: any) {
const diff = this.diffObjects(oldModel, newModel);
delete diff.type_;
return !Object.getOwnPropertyNames(diff).length;
}
static saveMutex(modelOrId:any) {
static saveMutex(modelOrId: any) {
const noLockMutex = {
acquire: function():any {
acquire: function(): any {
return null;
},
};
@ -420,7 +420,7 @@ class BaseModel {
return mutex;
}
static releaseSaveMutex(modelOrId:any, release:Function) {
static releaseSaveMutex(modelOrId: any, release: Function) {
if (!release) return;
if (!modelOrId) return release();
@ -435,8 +435,8 @@ class BaseModel {
release();
}
static saveQuery(o:any, options:any) {
let temp:any = {};
static saveQuery(o: any, options: any) {
let temp: any = {};
const fieldNames = this.fieldNames();
for (let i = 0; i < fieldNames.length; i++) {
const n = fieldNames[i];
@ -448,7 +448,7 @@ class BaseModel {
// be part of the final list of fields if autoTimestamp is on.
// id also will stay.
if (!options.isNew && options.fields) {
const filtered:any = {};
const filtered: any = {};
for (const k in temp) {
if (!temp.hasOwnProperty(k)) continue;
if (k !== 'id' && options.fields.indexOf(k) < 0) continue;
@ -460,7 +460,7 @@ class BaseModel {
o = temp;
let modelId = temp.id;
let query:any = {};
let query: any = {};
const timeNow = time.unixMs();
@ -512,7 +512,7 @@ class BaseModel {
return query;
}
static userSideValidation(o:any) {
static userSideValidation(o: any) {
if (o.id && !o.id.match(/^[a-f0-9]{32}$/)) {
throw new Error('Validation error: ID must a 32-characters lowercase hexadecimal string');
}
@ -523,7 +523,7 @@ class BaseModel {
}
}
static async save(o:any, options:any = null) {
static async save(o: any, options: any = null) {
// When saving, there's a mutex per model ID. This is because the model returned from this function
// is basically its input `o` (instead of being read from the database, for performance reasons).
// This works well in general except if that model is saved simultaneously in two places. In that
@ -593,7 +593,7 @@ class BaseModel {
return output;
}
static isNew(object:any, options:any) {
static isNew(object: any, options: any) {
if (options && 'isNew' in options) {
// options.isNew can be "auto" too
if (options.isNew === true) return true;
@ -603,7 +603,7 @@ class BaseModel {
return !object.id;
}
static filterArray(models:any[]) {
static filterArray(models: any[]) {
const output = [];
for (let i = 0; i < models.length; i++) {
output.push(this.filter(models[i]));
@ -611,7 +611,7 @@ class BaseModel {
return output;
}
static filter(model:any) {
static filter(model: any) {
if (!model) return model;
const output = Object.assign({}, model);
@ -634,12 +634,12 @@ class BaseModel {
return output;
}
static delete(id:string) {
static delete(id: string) {
if (!id) throw new Error('Cannot delete object without an ID');
return this.db().exec(`DELETE FROM ${this.tableName()} WHERE id = ?`, [id]);
}
static batchDelete(ids:string[], options:any = null) {
static batchDelete(ids: string[], options: any = null) {
if (!ids.length) return;
options = this.modOptions(options);
const idFieldName = options.idFieldName ? options.idFieldName : 'id';