1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00

Tools: Add eslint rule prefer-await-to-then

This commit is contained in:
Laurent Cozic 2022-09-30 17:23:14 +01:00
parent cc6620a7e1
commit 44a96f347a
34 changed files with 65 additions and 0 deletions

View File

@ -90,6 +90,8 @@ module.exports = {
// Disable because of this: https://github.com/facebook/react/issues/16265
// "react-hooks/exhaustive-deps": "warn",
'promise/prefer-await-to-then': 'error',
// -------------------------------
// Formatting
// -------------------------------
@ -141,6 +143,7 @@ module.exports = {
'@seiyab/eslint-plugin-react-hooks',
// 'react-hooks',
'import',
'promise',
],
'overrides': [
{

View File

@ -69,6 +69,7 @@
"eslint": "^8.22.0",
"eslint-interactive": "^10.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-promise": "^6.0.1",
"eslint-plugin-react": "^7.30.1",
"fs-extra": "^8.1.0",
"glob": "^7.1.6",

View File

@ -124,6 +124,7 @@ async function handleAutocompletionPromise(line) {
return line;
}
function handleAutocompletion(str, callback) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
handleAutocompletionPromise(str).then(function(res) {
callback(undefined, res);
});

View File

@ -36,6 +36,7 @@ async function createClients() {
const client = createClient(clientId);
promises.push(fs.remove(client.profileDir));
promises.push(
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
execCommand(client, 'config sync.target 2').then(() => {
return execCommand(client, `config sync.2.path ${syncDir}`);
})
@ -2324,10 +2325,12 @@ async function main() {
clients[clientId].activeCommandCount++;
execRandomCommand(clients[clientId])
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch(error => {
logger.info(`Client ${clientId}:`);
logger.error(error);
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(r => {
if (r) {
logger.info(`Client ${clientId}:\n${r.trim()}`);

View File

@ -50,6 +50,7 @@ export default class PluginRunner extends BasePluginRunner {
const callId = `${pluginId}::${path}::${uuid.createNano()}`;
this.activeSandboxCalls_[callId] = true;
const promise = executeSandboxCall(pluginId, sandbox, `joplin.${path}`, mapEventHandlersToIds(args, this.eventHandlers_), this.eventHandler);
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
promise.finally(() => {
delete this.activeSandboxCalls_[callId];
});

View File

@ -502,6 +502,7 @@ class Application extends BaseApplication {
if (Setting.value('env') === 'dev') {
void AlarmService.updateAllNotifications();
} else {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
void reg.scheduleSync(1000).then(() => {
// Wait for the first sync before updating the notifications, since synchronisation
// might change the notifications.

View File

@ -45,6 +45,7 @@ class ClipperConfigScreenComponent extends React.Component {
if (confirm(_('Are you sure you want to renew the authorisation token?'))) {
void EncryptionService.instance()
.generateApiToken()
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((token) => {
Setting.setValue('api.token', token);
});

View File

@ -174,9 +174,11 @@ class ResourceScreenComponent extends React.Component<Props, State> {
return;
}
Resource.delete(resource.id)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch((error: Error) => {
bridge().showErrorMessageBox(error.message);
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.finally(() => {
void this.reloadResources(this.state.sorting);
});

View File

@ -50,6 +50,7 @@ class FolderScreenComponent extends BaseScreenComponent {
lastSavedFolder: Object.assign({}, folder),
});
} else {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
Folder.load(this.props.folderId).then(folder => {
this.setState({
folder: folder,

View File

@ -151,10 +151,12 @@ class NotesScreenComponent extends BaseScreenComponent {
}
deleteFolder_onPress(folderId) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
dialogs.confirm(this, _('Delete notebook? All notes and sub-notebooks within this notebook will also be deleted.')).then(ok => {
if (!ok) return;
Folder.delete(folderId)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(() => {
this.props.dispatch({
type: 'NAV_GO',
@ -162,6 +164,7 @@ class NotesScreenComponent extends BaseScreenComponent {
smartFilterId: 'c3176726992c11e9ac940492261af972',
});
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch(error => {
alert(error.message);
});

View File

@ -633,6 +633,7 @@ async function initialize(dispatch: Function) {
// start almost immediately to get the latest data.
// doWifiConnectionCheck set to true so initial sync
// doesn't happen on mobile data
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
void reg.scheduleSync(1000, null, true).then(() => {
// Wait for the first sync before updating the notifications, since synchronisation
// might change the notifications.

View File

@ -39,6 +39,7 @@ export default (dispatch: Function, folderId: string) => {
void Note.save({
parent_id: folderId,
is_todo: isTodo,
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
}, { provisional: true }).then((newNote: any) => {
dispatch({
type: 'NAV_GO',
@ -51,6 +52,7 @@ export default (dispatch: Function, folderId: string) => {
DeviceEventEmitter.addListener('quickActionShortcut', handleQuickAction);
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
QuickActions.popInitialAction().then(handleQuickAction).catch((reason: any) => reg.logger().error(reason));
};

View File

@ -41,6 +41,7 @@ class DatabaseDriverReactNative {
}
selectAll(sql, params = null) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
return this.exec(sql, params).then(r => {
const output = [];
for (let i = 0; i < r.rows.length; i++) {

View File

@ -880,6 +880,7 @@ export default class BaseApplication {
if (!Setting.value('api.token')) {
void EncryptionService.instance()
.generateApiToken()
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((token: string) => {
Setting.setValue('api.token', token);
});

View File

@ -248,6 +248,7 @@ class BaseModel {
if (options.where) sql += ` WHERE ${options.where}`;
return this.db()
.selectOne(sql)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((r: any) => {
return r ? r['total'] : 0;
});
@ -335,6 +336,7 @@ class BaseModel {
if (params === null) params = [];
return this.db()
.selectOne(sql, params)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((model: any) => {
return this.filter(this.addModelMd(model));
});
@ -344,6 +346,7 @@ class BaseModel {
if (params === null) params = [];
return this.db()
.selectAll(sql, params)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((models: any[]) => {
return this.filterArray(this.addModelMd(models));
});

View File

@ -292,6 +292,7 @@ export default class JoplinDatabase extends Database {
queries.push(this.wrapQuery('DELETE FROM table_fields'));
return this.selectAll('SELECT name FROM sqlite_master WHERE type="table"')
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(tableRows => {
const chain = [];
for (let i = 0; i < tableRows.length; i++) {
@ -303,6 +304,7 @@ export default class JoplinDatabase extends Database {
if (tableName === 'notes_spellfix') continue;
if (tableName === 'search_aux') continue;
chain.push(() => {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
return this.selectAll(`PRAGMA table_info("${tableName}")`).then(pragmas => {
for (let i = 0; i < pragmas.length; i++) {
const item = pragmas[i];
@ -325,6 +327,7 @@ export default class JoplinDatabase extends Database {
return promiseChain(chain);
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(() => {
queries.push({ sql: 'UPDATE version SET table_fields_version = ?', params: [newVersion] });
return this.transactionExecBatch(queries);

View File

@ -231,11 +231,14 @@ class Logger {
// when many log operations are being done (eg. during sync in
// dev mode).
let release: Function = null;
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
writeToFileMutex_.acquire().then((r: Function) => {
release = r;
return Logger.fsDriver().appendFile(target.path, `${line.join(': ')}\n`, 'utf8');
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
}).catch((error: any) => {
console.error('Cannot write to log file:', error);
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
}).finally(() => {
if (release) release();
});

View File

@ -70,9 +70,11 @@ export default class TaskQueue {
task
.callback()
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((result: any) => {
completeTask(task, result, null);
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch((error: Error) => {
if (!error) error = new Error('Unknown error');
completeTask(task, null, error);

View File

@ -102,6 +102,7 @@ shared.saveNoteButton_press = async function(comp, folderId = null, options = nu
comp.setState(newState);
if (isProvisionalNote) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
Note.updateGeolocation(note.id).then(geoNote => {
const stateNote = comp.state.note;
if (!stateNote || !geoNote) return;

View File

@ -588,6 +588,7 @@ export default async function importEnex(parentFolderId: string, filePath: strin
notes.push(note);
if (notes.length >= 10) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
processNotes().catch(error => {
importOptions.onError(createErrorWithNoteTitle(this, error));
});
@ -648,6 +649,7 @@ export default async function importEnex(parentFolderId: string, filePath: strin
saxStream.on('end', handleSaxStreamEvent(function() {
// Wait till there is no more notes to process.
const iid = shim.setInterval(() => {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
void processNotes().then(allDone => {
if (allDone) {
shim.clearTimeout(iid);

View File

@ -55,6 +55,7 @@ export default class Folder extends BaseItem {
return this.db()
.selectAll(`SELECT id FROM notes WHERE ${where.join(' AND ')}`, [parentId])
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then((rows: any[]) => {
const output = [];
for (let i = 0; i < rows.length; i++) {
@ -759,6 +760,7 @@ export default class Folder extends BaseItem {
syncDebugLog.info('Folder Save:', o);
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
return super.save(o, options).then((folder: FolderEntity) => {
this.dispatch({
type: 'FOLDER_UPDATE_ONE',

View File

@ -205,6 +205,7 @@ export default class Tag extends BaseItem {
}
}
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
return super.save(o, options).then((tag: TagEntity) => {
if (options.dispatchUpdateAction) {
this.dispatch({

View File

@ -82,12 +82,14 @@ class OneDriveApiNodeUtils {
this.api()
.execTokenRequest(query.code, `http://localhost:${port.toString()}`)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(() => {
writeResponse(200, _('The application has been authorised - you may now close this browser tab.'));
targetConsole.log('');
targetConsole.log(_('The application has been successfully authorised.'));
waitAndDestroy();
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch(error => {
writeResponse(400, error.message);
targetConsole.log('');

View File

@ -4,6 +4,7 @@ function promiseChain(chain, defaultValue = null) {
});
for (let i = 0; i < chain.length; i++) {
const f = chain[i];
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
output = output.then(f);
}
return output;

View File

@ -49,6 +49,7 @@ describe('Registry', function() {
it('should sync if do wifi check is false', done => {
void reg.scheduleSync(1, null, false)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(() =>{
expect(sync.start).toHaveBeenCalled();
done();

View File

@ -181,11 +181,13 @@ export default class ResourceFetcher extends BaseService {
fileApi
.get(remoteResourceContentPath, { path: localResourceContentPath, target: 'file' })
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(async () => {
await Resource.setLocalState(resource, { fetch_status: Resource.FETCH_STATUS_DONE });
this.logger().debug(`ResourceFetcher: Resource downloaded: ${resource.id}`);
await completeDownload(true, localResourceContentPath);
})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch(async (error: any) => {
this.logger().error(`ResourceFetcher: Could not download resource: ${resource.id}`, error);
await Resource.setLocalState(resource, { fetch_status: Resource.FETCH_STATUS_ERROR, fetch_error: error.message });

View File

@ -77,6 +77,7 @@ export default async function populateDatabase(db: any, options: Options = null)
let tagBatch = [];
for (let i = 0; i < options.tagCount; i++) {
const tagTitle = randomElement(wordList); // `tag${i}`
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
tagBatch.push(Tag.save({ title: tagTitle }, { dispatchUpdateAction: false }).then((savedTag: any) => {
createdTagIds.push(savedTag.id);
if (!options.silent) console.info(`Tags: ${i} / ${options.tagCount}`);
@ -99,6 +100,7 @@ export default async function populateDatabase(db: any, options: Options = null)
const parentIndex = randomIndex(createdFolderIds);
note.parent_id = createdFolderIds[parentIndex];
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
noteBatch.push(Note.save(note, { dispatchUpdateAction: false }).then((savedNote: any) => {
createdNoteIds.push(savedNote.id);
console.info(`Notes: ${i} / ${options.noteCount}`);

View File

@ -36,6 +36,7 @@ export default class JoplinPlugins {
// We don't use `await` when calling onStart because the plugin might be awaiting
// in that call too (for example, when opening a dialog on startup) so we don't
// want to get stuck here.
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
void script.onStart({}).catch((error: any) => {
// For some reason, error thrown from the executed script do not have the type "Error"
// but are instead plain object. So recreate the Error object here so that it can
@ -43,6 +44,7 @@ export default class JoplinPlugins {
const newError: Error = new Error(error.message);
newError.stack = error.stack;
logger.error(`Uncaught exception in plugin "${this.plugin.id}":`, newError);
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
}).then(() => {
logger.info(`Finished running onStart handler: ${this.plugin.id} (Took ${Date.now() - startTime}ms)`);
this.plugin.emit('started');

View File

@ -502,7 +502,9 @@ function shimInit(options = null) {
const cleanUpOnError = error => {
// We ignore any unlink error as we only want to report on the main error
fs.unlink(filePath)
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.catch(() => {})
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
.then(() => {
if (file) {
file.close(() => {

View File

@ -71,6 +71,7 @@ async function main() {
}
if (require.main === module) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
main().catch((error) => {
console.error('Fatal error');
console.error(error);

View File

@ -87,6 +87,7 @@ async function start(): Promise<void> {
}
if (require.main === module) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
start().catch((error) => {
console.error('Fatal error');
console.error(error);

View File

@ -48,6 +48,7 @@ const main = async () => {
};
if (require.main === module) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
main().catch((error) => {
console.error(error);
process.exit(1);

View File

@ -12,6 +12,7 @@ async function main() {
}
if (require.main === module) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
main().catch((error) => {
console.error('Fatal error');
console.error(error);

View File

@ -15287,6 +15287,15 @@ __metadata:
languageName: node
linkType: hard
"eslint-plugin-promise@npm:^6.0.1":
version: 6.0.1
resolution: "eslint-plugin-promise@npm:6.0.1"
peerDependencies:
eslint: ^7.0.0 || ^8.0.0
checksum: c1bb3c2e591787e97133dcaf764f908420a3a1959a3132e199db8f14d70dfa79fc9caf991ca60a4b60ae5f1f9823bc96c2e52304828a4278ef2f3964fe121de9
languageName: node
linkType: hard
"eslint-plugin-promise@npm:~3.4.0":
version: 3.4.2
resolution: "eslint-plugin-promise@npm:3.4.2"
@ -30008,6 +30017,7 @@ __metadata:
eslint: ^8.22.0
eslint-interactive: ^10.0.0
eslint-plugin-import: ^2.26.0
eslint-plugin-promise: ^6.0.1
eslint-plugin-react: ^7.30.1
fs-extra: ^8.1.0
glob: ^7.1.6