From c545acf12dc9ebeba186d6d3f617c0bab16b9477 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 16:21:05 +0200 Subject: [PATCH 1/7] chore: disable eslint regex control needed since the introduction of color and functions to remove them in tests files --- .eslintrc.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 1918e9ef..32b8adfe 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,6 +9,7 @@ module.exports = { extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'], rules: { '@typescript-eslint/no-unused-vars': 'error', - '@typescript-eslint/consistent-type-definitions': ['error', 'type'] + '@typescript-eslint/consistent-type-definitions': ['error', 'type'], + "no-control-regex": 0 } }; From 820eabf547099db7b116dadb4117ec28945da9a1 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 16:21:27 +0200 Subject: [PATCH 2/7] fix: improve typing and correct some errors following eslint output --- pkg/core/src/plugin.ts | 8 ++++---- pkg/core/src/slangroom.ts | 4 ++-- pkg/core/test/plugin.ts | 4 ++-- pkg/http/src/plugin.ts | 28 +++++++++++++++++++--------- pkg/pocketbase/src/plugin.ts | 2 +- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/core/src/plugin.ts b/pkg/core/src/plugin.ts index ea9db30a..b2b17879 100644 --- a/pkg/core/src/plugin.ts +++ b/pkg/core/src/plugin.ts @@ -397,7 +397,7 @@ export type ResultOk = { ok: true; value: Jsonable }; /** * Result of a plugin execution, indicating failure. */ -export type ResultErr = { ok: false; error: any }; +export type ResultErr = { ok: false; error: Error }; /** * The Plugin Context. It has every info a plugin needs, plus some utilities. @@ -453,7 +453,7 @@ export type PluginContext = { * The utility function that makes a {@link Plugin} fail. Must be used with a * `return` statement. */ - fail(reason: any): PluginResult; + fail(reason: Error): PluginResult; /** * The utility function that makes a {@link Plugin} pass/succeed. Must be used with @@ -489,7 +489,7 @@ export class PluginContextImpl implements PluginContext { /** * {@inheritDoc PluginContext.fail} */ - fail(reason: any): PluginResult { + fail(reason: Error): PluginResult { return { ok: false, error: reason }; } @@ -592,7 +592,7 @@ export class PluginContextTest implements PluginContext { /** * {@inheritDoc PluginContext.fail} */ - fail(reason: any): PluginResult { + fail(reason: Error): PluginResult { return { ok: false, error: reason }; } diff --git a/pkg/core/src/slangroom.ts b/pkg/core/src/slangroom.ts index 15a6e70a..79a5ab9a 100644 --- a/pkg/core/src/slangroom.ts +++ b/pkg/core/src/slangroom.ts @@ -156,7 +156,7 @@ const thorwErrors = (errorArray: GenericError[], contract: string) => { for (let i = lineStart; i < lineEnd; i++) { const linePrefix = `${i} | `; if (i === lineNumber -1) { - let boldError = textHighlight(contractLines[i]!.slice(colStart, colEnd)); + const boldError = textHighlight(contractLines[i]!.slice(colStart, colEnd)); const redBackground = sentenceHighlight(contractLines[i]!.slice(0, colStart) + boldError + contractLines[i]!.slice(colEnd)); e = e.concat(`${lineNoColor(linePrefix)}${redBackground}\n`); e = e.concat(' '.repeat(colStart + linePrefix.length) + errorColor('^'.repeat(colEnd - colStart)) + '\n'); @@ -168,7 +168,7 @@ const thorwErrors = (errorArray: GenericError[], contract: string) => { e = e.concat(` - ${missingColor('missing words')}\n`); e = e.concat(` - ${extraColor('extra words')}\n`); - for (let err of errorArray) { + for (const err of errorArray) { e = e.concat('\n' + err.message + '\n'); } throw new Error(e); diff --git a/pkg/core/test/plugin.ts b/pkg/core/test/plugin.ts index 867cac9f..c63d01c8 100644 --- a/pkg/core/test/plugin.ts +++ b/pkg/core/test/plugin.ts @@ -165,9 +165,9 @@ test('PluginContext', async (t) => { const objCtx = new PluginContextTest('', {obj: 'obj'}); // pass and fail const pass = p.new('test pass', (ctx) => ctx.pass('done')); - const fail = p.new('test fail', (ctx) => ctx.fail('failed')); + const fail = p.new('test fail', (ctx) => ctx.fail(new Error ('failed'))); t.deepEqual(await pass(emptyCtx), { ok: true, value: 'done' }); - t.deepEqual(await fail(emptyCtx), { ok: false, error: 'failed' }); + t.deepEqual(await fail(emptyCtx), { ok: false, error: new Error('failed') }); // connect const getConnect = p.new('connect', 'test get connect', (ctx) => ctx.pass(ctx.getConnect())); const fetchConnect = p.new('connect', 'test fetch connect', (ctx) => ctx.pass(ctx.fetchConnect())); diff --git a/pkg/http/src/plugin.ts b/pkg/http/src/plugin.ts index 870af96c..b8f0fd10 100644 --- a/pkg/http/src/plugin.ts +++ b/pkg/http/src/plugin.ts @@ -78,15 +78,25 @@ const sameParallelRequest = (m: HttpMethod, isSame: boolean): PluginExecutor => } } - const results = (await Promise.allSettled(reqs)).map((x) => { - if (x.status === 'fulfilled') - return { status: x.value.status.toString(), result: x.value.data }; - - const err = x.reason; - if (axios.isAxiosError(err)) return { status: err.code ?? '', result: '' }; - - return ctx.fail(new HttpError(err.message)); - }); + const results: { status: string, result: any }[] = []; + try { + (await Promise.allSettled(reqs)).map((x) => { + if (x.status === 'fulfilled') { + results.push({ status: x.value.status.toString(), result: x.value.data }); + return; + } + + const err = x.reason; + if (axios.isAxiosError(err)) { + results.push({ status: err.code ?? '', result: '' }); + return; + } + + throw new HttpError(err.message); + }) + } catch (e) { + ctx.fail(new HttpError(e.message)); + } return ctx.pass(results); }; diff --git a/pkg/pocketbase/src/plugin.ts b/pkg/pocketbase/src/plugin.ts index 800898f1..f7164847 100644 --- a/pkg/pocketbase/src/plugin.ts +++ b/pkg/pocketbase/src/plugin.ts @@ -168,7 +168,7 @@ export const authWithPassword = p.new(['my_credentials'], 'login', async (ctx) = */ export const requestPasswordReset = p.new(['email'], 'ask password reset', async (ctx) => { const email = ctx.fetch('email') as string; - if (!(await isPbRunning())) return ctx.fail('Client is not running'); + if (!(await isPbRunning())) return ctx.fail(new PocketBaseError('Client is not running')); try { const res = await pb.collection('users').requestPasswordReset(email); From 464ccdab07d1637c41b55082cf4e53f967de3173 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 16:45:08 +0200 Subject: [PATCH 3/7] refactor(git): remove unused variable --- pkg/git/test/e2e.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/git/test/e2e.ts b/pkg/git/test/e2e.ts index 29e5bf73..e902ed09 100644 --- a/pkg/git/test/e2e.ts +++ b/pkg/git/test/e2e.ts @@ -148,6 +148,6 @@ test.serial('Should create a new git commit', async (t) => { t.truthy(typeof res.result['commit_hash'] === 'string'); }) -test.after.always('guaranteed cleanup', async (_t) => { +test.after.always('guaranteed cleanup', async () => { await fs.rm('./test/dumb', { recursive: true }) }); From 0ec6db0d453788f8b2bd1de9240316aac8e97e36 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 17:01:39 +0200 Subject: [PATCH 4/7] refactor(helpers): substitute type 'any' with JsonableObject and JsonableArray --- pkg/helpers/src/plugin.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/helpers/src/plugin.ts b/pkg/helpers/src/plugin.ts index 07368522..1b5f18e0 100644 --- a/pkg/helpers/src/plugin.ts +++ b/pkg/helpers/src/plugin.ts @@ -3,6 +3,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later import { Plugin } from '@slangroom/core'; +import type { JsonableObject, JsonableArray } from '@slangroom/shared'; import _ from 'lodash'; // read the version from the package.json import packageJson from '@slangroom/helpers/package.json' with { type: 'json' }; @@ -20,7 +21,7 @@ export const get = p.new(['object', 'path'], 'manipulate and get', async (ctx) = const object = ctx.fetch('object'); const path = ctx.fetch('path'); try { - return ctx.pass(_.get(object as any, path as string)); + return ctx.pass(_.get(object as JsonableObject, path as string)); } catch (e) { return ctx.fail(new HelperError(e)); } @@ -31,7 +32,7 @@ export const set = p.new(['object', 'path', 'value'], 'manipulate and set', asyn const path = ctx.fetch('path'); const value = ctx.fetch('value'); try { - return ctx.pass(_.set(object as any, path as string, value)); + return ctx.pass(_.set(object as JsonableObject, path as string, value)); } catch (e) { return ctx.fail(new HelperError(e)); } @@ -41,7 +42,7 @@ export const merge = p.new(['object', 'sources'], 'manipulate and merge', async const object = ctx.fetch('object'); const sources = ctx.fetch('sources'); try { - return ctx.pass(_.merge(object as any, ...sources as any[])); + return ctx.pass(_.merge(object as JsonableObject, ...sources as JsonableArray)); } catch (e) { return ctx.fail(new HelperError(e)); } @@ -51,7 +52,7 @@ export const omit = p.new(['object', 'paths'], 'manipulate and omit', async (ctx const object = ctx.fetch('object'); const paths = ctx.fetch('paths'); try { - return ctx.pass(_.omit(object as any, paths as any[])); + return ctx.pass(_.omit(object as JsonableObject, paths as string[])); } catch (e) { return ctx.fail(new HelperError(e)); } @@ -61,7 +62,7 @@ export const concat = p.new(['array', 'values'], 'manipulate and concat', async const array = ctx.fetch('array'); const values = ctx.fetch('values'); try { - return ctx.pass(_.concat(array as any[], ...values as any[])); + return ctx.pass(_.concat(array as JsonableArray, ...values as JsonableArray)); } catch (e) { return ctx.fail(new HelperError(e)); } @@ -70,7 +71,7 @@ export const concat = p.new(['array', 'values'], 'manipulate and concat', async export const compact = p.new(['array'], 'manipulate and compact', async (ctx) => { const array = ctx.fetch('array'); try { - return ctx.pass(_.compact(array as any[])); + return ctx.pass(_.compact(array as JsonableArray)); } catch (e) { return ctx.fail(new HelperError(e)); } @@ -78,7 +79,7 @@ export const compact = p.new(['array'], 'manipulate and compact', async (ctx) => export const pick = p.new(['object', 'properties'], 'manipulate and pick', async (ctx) => { const properties = ctx.fetch('properties') as string[] | string; - const obj = ctx.fetch('object') as {}; + const obj = ctx.fetch('object') as JsonableObject; try { const manipulated = _.pick(obj, properties); if (Object.keys(manipulated).length === 0) { From 010e52d7b582acb15177e97a2af05cf9bf8f9437 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 17:38:59 +0200 Subject: [PATCH 5/7] refactor(http): improve typings --- pkg/http/src/plugin.ts | 14 +++++++------- pkg/http/test/e2e.ts | 1 + pkg/http/test/plugin.ts | 2 ++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/http/src/plugin.ts b/pkg/http/src/plugin.ts index b8f0fd10..22cbdc2d 100644 --- a/pkg/http/src/plugin.ts +++ b/pkg/http/src/plugin.ts @@ -2,9 +2,9 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -import type { JsonableArray, JsonableObject } from '@slangroom/shared'; +import type { Jsonable, JsonableArray, JsonableObject } from '@slangroom/shared'; import { Plugin, type PluginExecutor } from '@slangroom/core'; -import axios, { type AxiosRequestConfig } from 'axios'; +import axios, { type AxiosRequestConfig, type AxiosHeaders } from 'axios'; // read the version from the package.json import packageJson from '@slangroom/http/package.json' with { type: 'json' }; @@ -34,7 +34,7 @@ const defaultRequest = (m: HttpMethod): PluginExecutor => { return async (ctx) => { const url = ctx.fetchConnect()[0]; // TODO: typecheck headers - const headers = ctx.get('headers') as any; + const headers = ctx.get('headers') as AxiosHeaders; const object = ctx.get('object'); const conf: AxiosRequestConfig = { url: url, method: m }; if (object) conf.data = object; @@ -51,10 +51,10 @@ const defaultRequest = (m: HttpMethod): PluginExecutor => { const sameParallelRequest = (m: HttpMethod, isSame: boolean): PluginExecutor => { return async (ctx) => { - const reqs: ReturnType>[] = []; + const reqs: ReturnType>[] = []; const urls = ctx.fetchConnect(); // TODO: typecheck headers - const headers = ctx.get('headers') as any; + const headers = ctx.get('headers') as AxiosHeaders; if (isSame) { // TODO: typecheck object JsonableObject @@ -78,11 +78,11 @@ const sameParallelRequest = (m: HttpMethod, isSame: boolean): PluginExecutor => } } - const results: { status: string, result: any }[] = []; + const results: { status: string, result: Jsonable }[] = []; try { (await Promise.allSettled(reqs)).map((x) => { if (x.status === 'fulfilled') { - results.push({ status: x.value.status.toString(), result: x.value.data }); + results.push({ status: x.value.status.toString(), result: x.value.data as Jsonable }); return; } diff --git a/pkg/http/test/e2e.ts b/pkg/http/test/e2e.ts index ada239ea..21359aa5 100644 --- a/pkg/http/test/e2e.ts +++ b/pkg/http/test/e2e.ts @@ -17,6 +17,7 @@ nock('http://localhost') .get('/greeting-en') .reply(200, { req: 'Hi!' }) .post('/sendresult') + // eslint-disable-next-line @typescript-eslint/no-explicit-any .reply((_, body: any) => { const req = body['req']; if (req?.includes('Hola') || req?.includes('Hi')) return [200, 'received result']; diff --git a/pkg/http/test/plugin.ts b/pkg/http/test/plugin.ts index 1230bd75..470e1456 100644 --- a/pkg/http/test/plugin.ts +++ b/pkg/http/test/plugin.ts @@ -31,11 +31,13 @@ nock('http://localhost') }, }) .post('/sendresult') + // eslint-disable-next-line @typescript-eslint/no-explicit-any .reply((_, body: any) => { if (body['myData']) return [200, 'received result']; return [500, 'Did not receive the result']; }) .put('/sendresultwithput') + // eslint-disable-next-line @typescript-eslint/no-explicit-any .reply((_, body: any) => { if (body['myData']) return [200, 'received result']; return [500, 'Did not receive the result']; From 727becdbb7ecf0d3bb98578f62444869bfd3142a Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 17:43:07 +0200 Subject: [PATCH 6/7] refactor(ignore): remove useless try...catch --- pkg/ignored/src/index.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/ignored/src/index.ts b/pkg/ignored/src/index.ts index 761375d4..ca03a632 100644 --- a/pkg/ignored/src/index.ts +++ b/pkg/ignored/src/index.ts @@ -17,6 +17,13 @@ export class InvalidStatementError extends Error { } } +type ParseResult = { + result: { + ignored: [string, number][], + invalid: [string, number, string][] + }, + logs: string +} /** * Check if the statement could be an ignored statement and thus adding Rule unknown ignore can solve the problem @@ -57,12 +64,7 @@ const couldBeIgnored = (lineNo: number, contract: string, invalidLinesNos: numbe export const getIgnoredStatements = async ( contract: string, ): Promise<{ignoredLines: [string, number][], invalidLines: {message: Error, lineNo: number}[]}> => { - let zout: {result: {ignored: [string, number][], invalid: [string, number, string][]}; logs: string}; - try { - zout = await zencodeParse(contract); - } catch (e) { - throw e; - } + const zout: ParseResult = await zencodeParse(contract); const invalidLinesNos: number[] = zout.result.invalid.map((x) => x[1]); const ruleUnknownIgnore = JSON.parse(zout.logs).some((x: string) => x.toLowerCase().includes('rule unknown ignore'));; return { From 63dd769845351acead1022bfb8abf513b74c2256 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 28 Jun 2024 18:53:40 +0200 Subject: [PATCH 7/7] chore: ignore unused vars that starts with _ --- .eslintrc.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 32b8adfe..d54fdc5e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,7 +8,13 @@ module.exports = { plugins: ['@typescript-eslint'], extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'], rules: { - '@typescript-eslint/no-unused-vars': 'error', + '@typescript-eslint/no-unused-vars': [ + 'error', + { + "argsIgnorePattern": "^_", + "varsIgnorePattern": "^_" + } + ], '@typescript-eslint/consistent-type-definitions': ['error', 'type'], "no-control-regex": 0 }