From 4187313c232039d0f63d43980f791ec35ea5760c Mon Sep 17 00:00:00 2001 From: Etienne Deladonchamps Date: Tue, 23 Apr 2024 00:08:17 +0200 Subject: [PATCH] Upgrade erreur to v7 --- package.json | 7 ++-- pnpm-lock.yaml | 92 +++++++++++++++++++++++-------------------------- src/erreur.ts | 67 ++++++++++++++++++----------------- src/mod.ts | 2 +- src/restore.ts | 4 +-- src/typedMap.ts | 4 +-- src/types.ts | 12 ++++--- 7 files changed, 96 insertions(+), 92 deletions(-) diff --git a/package.json b/package.json index 8d35fc8..804b608 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "test": "pnpm run lint && vitest run --coverage", "test:run": "vitest run", "test:watch": "vitest --watch", + "test:watch:coverage": "vitest --watch --coverage", "typecheck": "tsc", "typecheck:watch": "tsc --watch" }, @@ -85,12 +86,12 @@ "root": true }, "dependencies": { - "@dldc/erreur": "^6.0.6" + "@dldc/erreur": "7.0.0-1" }, "devDependencies": { "@types/node": "^20.12.7", - "@typescript-eslint/eslint-plugin": "^7.6.0", - "@typescript-eslint/parser": "^7.6.0", + "@typescript-eslint/eslint-plugin": "^7.7.1", + "@typescript-eslint/parser": "^7.7.1", "@vitest/coverage-v8": "^1.5.0", "auto-changelog": "^2.4.0", "eslint": "^8.56.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9a15d45..d82d575 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,19 +6,19 @@ settings: dependencies: '@dldc/erreur': - specifier: ^6.0.6 - version: 6.0.6 + specifier: 7.0.0-1 + version: 7.0.0-1 devDependencies: '@types/node': specifier: ^20.12.7 version: 20.12.7 '@typescript-eslint/eslint-plugin': - specifier: ^7.6.0 - version: 7.6.0(@typescript-eslint/parser@7.6.0)(eslint@8.56.0)(typescript@5.4.5) + specifier: ^7.7.1 + version: 7.7.1(@typescript-eslint/parser@7.7.1)(eslint@8.56.0)(typescript@5.4.5) '@typescript-eslint/parser': - specifier: ^7.6.0 - version: 7.6.0(eslint@8.56.0)(typescript@5.4.5) + specifier: ^7.7.1 + version: 7.7.1(eslint@8.56.0)(typescript@5.4.5) '@vitest/coverage-v8': specifier: ^1.5.0 version: 1.5.0(vitest@1.5.0) @@ -114,14 +114,8 @@ packages: resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} dev: true - /@dldc/erreur@6.0.6: - resolution: {integrity: sha512-wPhBjLX0TcsF9ztsoA/e+axFKYiwVxweyBkYASI6dKi8p0Jy7BQ2rdd9nx7Hk5etGnIgz+ObgIwXnCcns/xoDw==} - dependencies: - '@dldc/stack': 4.1.7 - dev: false - - /@dldc/stack@4.1.7: - resolution: {integrity: sha512-LDl0TEUD0NGKphZFw+/X8yhNTYVOiGhNaEe4/WRROkjz9B4B07OosrQLBJ9Dam2kxJ5WpmfAhqIQ6udgJsZyjw==} + /@dldc/erreur@7.0.0-1: + resolution: {integrity: sha512-96W+ECwRFUmFIsyx6ATqFRArW66a+4YnOTgqs5MibuFhweRUjqUz5fWyO7xiIXsBA6ChYJHnFLU0JPKDOPWo/A==} dev: false /@esbuild/aix-ppc64@0.19.12: @@ -989,8 +983,8 @@ packages: resolution: {integrity: sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==} dev: true - /@typescript-eslint/eslint-plugin@7.6.0(@typescript-eslint/parser@7.6.0)(eslint@8.56.0)(typescript@5.4.5): - resolution: {integrity: sha512-gKmTNwZnblUdnTIJu3e9kmeRRzV2j1a/LUO27KNNAnIC5zjy1aSvXSRp4rVNlmAoHlQ7HzX42NbKpcSr4jF80A==} + /@typescript-eslint/eslint-plugin@7.7.1(@typescript-eslint/parser@7.7.1)(eslint@8.56.0)(typescript@5.4.5): + resolution: {integrity: sha512-KwfdWXJBOviaBVhxO3p5TJiLpNuh2iyXyjmWN0f1nU87pwyvfS0EmjC6ukQVYVFJd/K1+0NWGPDXiyEyQorn0Q==} engines: {node: ^18.18.0 || >=20.0.0} peerDependencies: '@typescript-eslint/parser': ^7.0.0 @@ -1001,11 +995,11 @@ packages: optional: true dependencies: '@eslint-community/regexpp': 4.10.0 - '@typescript-eslint/parser': 7.6.0(eslint@8.56.0)(typescript@5.4.5) - '@typescript-eslint/scope-manager': 7.6.0 - '@typescript-eslint/type-utils': 7.6.0(eslint@8.56.0)(typescript@5.4.5) - '@typescript-eslint/utils': 7.6.0(eslint@8.56.0)(typescript@5.4.5) - '@typescript-eslint/visitor-keys': 7.6.0 + '@typescript-eslint/parser': 7.7.1(eslint@8.56.0)(typescript@5.4.5) + '@typescript-eslint/scope-manager': 7.7.1 + '@typescript-eslint/type-utils': 7.7.1(eslint@8.56.0)(typescript@5.4.5) + '@typescript-eslint/utils': 7.7.1(eslint@8.56.0)(typescript@5.4.5) + '@typescript-eslint/visitor-keys': 7.7.1 debug: 4.3.4 eslint: 8.56.0 graphemer: 1.4.0 @@ -1018,8 +1012,8 @@ packages: - supports-color dev: true - /@typescript-eslint/parser@7.6.0(eslint@8.56.0)(typescript@5.4.5): - resolution: {integrity: sha512-usPMPHcwX3ZoPWnBnhhorc14NJw9J4HpSXQX4urF2TPKG0au0XhJoZyX62fmvdHONUkmyUe74Hzm1//XA+BoYg==} + /@typescript-eslint/parser@7.7.1(eslint@8.56.0)(typescript@5.4.5): + resolution: {integrity: sha512-vmPzBOOtz48F6JAGVS/kZYk4EkXao6iGrD838sp1w3NQQC0W8ry/q641KU4PrG7AKNAf56NOcR8GOpH8l9FPCw==} engines: {node: ^18.18.0 || >=20.0.0} peerDependencies: eslint: ^8.56.0 @@ -1028,10 +1022,10 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/scope-manager': 7.6.0 - '@typescript-eslint/types': 7.6.0 - '@typescript-eslint/typescript-estree': 7.6.0(typescript@5.4.5) - '@typescript-eslint/visitor-keys': 7.6.0 + '@typescript-eslint/scope-manager': 7.7.1 + '@typescript-eslint/types': 7.7.1 + '@typescript-eslint/typescript-estree': 7.7.1(typescript@5.4.5) + '@typescript-eslint/visitor-keys': 7.7.1 debug: 4.3.4 eslint: 8.56.0 typescript: 5.4.5 @@ -1039,16 +1033,16 @@ packages: - supports-color dev: true - /@typescript-eslint/scope-manager@7.6.0: - resolution: {integrity: sha512-ngttyfExA5PsHSx0rdFgnADMYQi+Zkeiv4/ZxGYUWd0nLs63Ha0ksmp8VMxAIC0wtCFxMos7Lt3PszJssG/E6w==} + /@typescript-eslint/scope-manager@7.7.1: + resolution: {integrity: sha512-PytBif2SF+9SpEUKynYn5g1RHFddJUcyynGpztX3l/ik7KmZEv19WCMhUBkHXPU9es/VWGD3/zg3wg90+Dh2rA==} engines: {node: ^18.18.0 || >=20.0.0} dependencies: - '@typescript-eslint/types': 7.6.0 - '@typescript-eslint/visitor-keys': 7.6.0 + '@typescript-eslint/types': 7.7.1 + '@typescript-eslint/visitor-keys': 7.7.1 dev: true - /@typescript-eslint/type-utils@7.6.0(eslint@8.56.0)(typescript@5.4.5): - resolution: {integrity: sha512-NxAfqAPNLG6LTmy7uZgpK8KcuiS2NZD/HlThPXQRGwz6u7MDBWRVliEEl1Gj6U7++kVJTpehkhZzCJLMK66Scw==} + /@typescript-eslint/type-utils@7.7.1(eslint@8.56.0)(typescript@5.4.5): + resolution: {integrity: sha512-ZksJLW3WF7o75zaBPScdW1Gbkwhd/lyeXGf1kQCxJaOeITscoSl0MjynVvCzuV5boUz/3fOI06Lz8La55mu29Q==} engines: {node: ^18.18.0 || >=20.0.0} peerDependencies: eslint: ^8.56.0 @@ -1057,8 +1051,8 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/typescript-estree': 7.6.0(typescript@5.4.5) - '@typescript-eslint/utils': 7.6.0(eslint@8.56.0)(typescript@5.4.5) + '@typescript-eslint/typescript-estree': 7.7.1(typescript@5.4.5) + '@typescript-eslint/utils': 7.7.1(eslint@8.56.0)(typescript@5.4.5) debug: 4.3.4 eslint: 8.56.0 ts-api-utils: 1.3.0(typescript@5.4.5) @@ -1067,13 +1061,13 @@ packages: - supports-color dev: true - /@typescript-eslint/types@7.6.0: - resolution: {integrity: sha512-h02rYQn8J+MureCvHVVzhl69/GAfQGPQZmOMjG1KfCl7o3HtMSlPaPUAPu6lLctXI5ySRGIYk94clD/AUMCUgQ==} + /@typescript-eslint/types@7.7.1: + resolution: {integrity: sha512-AmPmnGW1ZLTpWa+/2omPrPfR7BcbUU4oha5VIbSbS1a1Tv966bklvLNXxp3mrbc+P2j4MNOTfDffNsk4o0c6/w==} engines: {node: ^18.18.0 || >=20.0.0} dev: true - /@typescript-eslint/typescript-estree@7.6.0(typescript@5.4.5): - resolution: {integrity: sha512-+7Y/GP9VuYibecrCQWSKgl3GvUM5cILRttpWtnAu8GNL9j11e4tbuGZmZjJ8ejnKYyBRb2ddGQ3rEFCq3QjMJw==} + /@typescript-eslint/typescript-estree@7.7.1(typescript@5.4.5): + resolution: {integrity: sha512-CXe0JHCXru8Fa36dteXqmH2YxngKJjkQLjxzoj6LYwzZ7qZvgsLSc+eqItCrqIop8Vl2UKoAi0StVWu97FQZIQ==} engines: {node: ^18.18.0 || >=20.0.0} peerDependencies: typescript: '*' @@ -1081,8 +1075,8 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/types': 7.6.0 - '@typescript-eslint/visitor-keys': 7.6.0 + '@typescript-eslint/types': 7.7.1 + '@typescript-eslint/visitor-keys': 7.7.1 debug: 4.3.4 globby: 11.1.0 is-glob: 4.0.3 @@ -1094,8 +1088,8 @@ packages: - supports-color dev: true - /@typescript-eslint/utils@7.6.0(eslint@8.56.0)(typescript@5.4.5): - resolution: {integrity: sha512-x54gaSsRRI+Nwz59TXpCsr6harB98qjXYzsRxGqvA5Ue3kQH+FxS7FYU81g/omn22ML2pZJkisy6Q+ElK8pBCA==} + /@typescript-eslint/utils@7.7.1(eslint@8.56.0)(typescript@5.4.5): + resolution: {integrity: sha512-QUvBxPEaBXf41ZBbaidKICgVL8Hin0p6prQDu6bbetWo39BKbWJxRsErOzMNT1rXvTll+J7ChrbmMCXM9rsvOQ==} engines: {node: ^18.18.0 || >=20.0.0} peerDependencies: eslint: ^8.56.0 @@ -1103,9 +1097,9 @@ packages: '@eslint-community/eslint-utils': 4.4.0(eslint@8.56.0) '@types/json-schema': 7.0.15 '@types/semver': 7.5.8 - '@typescript-eslint/scope-manager': 7.6.0 - '@typescript-eslint/types': 7.6.0 - '@typescript-eslint/typescript-estree': 7.6.0(typescript@5.4.5) + '@typescript-eslint/scope-manager': 7.7.1 + '@typescript-eslint/types': 7.7.1 + '@typescript-eslint/typescript-estree': 7.7.1(typescript@5.4.5) eslint: 8.56.0 semver: 7.6.0 transitivePeerDependencies: @@ -1113,11 +1107,11 @@ packages: - typescript dev: true - /@typescript-eslint/visitor-keys@7.6.0: - resolution: {integrity: sha512-4eLB7t+LlNUmXzfOu1VAIAdkjbu5xNSerURS9X/S5TUKWFRpXRQZbmtPqgKmYx8bj3J0irtQXSiWAOY82v+cgw==} + /@typescript-eslint/visitor-keys@7.7.1: + resolution: {integrity: sha512-gBL3Eq25uADw1LQ9kVpf3hRM+DWzs0uZknHYK3hq4jcTPqVCClHGDnB6UUUV2SFeBeA4KWHWbbLqmbGcZ4FYbw==} engines: {node: ^18.18.0 || >=20.0.0} dependencies: - '@typescript-eslint/types': 7.6.0 + '@typescript-eslint/types': 7.7.1 eslint-visitor-keys: 3.4.3 dev: true diff --git a/src/erreur.ts b/src/erreur.ts index 76c4d49..4130b3a 100644 --- a/src/erreur.ts +++ b/src/erreur.ts @@ -1,5 +1,4 @@ -import type { TKey } from '@dldc/erreur'; -import { Erreur, Key } from '@dldc/erreur'; +import { createErreurStore } from '@dldc/erreur'; export type TZenjsonErreurData = | { kind: 'CustomTypeNotFound'; typeName: string } @@ -8,32 +7,38 @@ export type TZenjsonErreurData = | { kind: 'InvalidSerializedSpecialValue'; value: string } | { kind: 'DuplicatedCustomTypeName'; typeName: string }; -export const ZenjsonErreurKey: TKey = Key.create('ZenjsonErreur'); - -export const ZenjsonErreur = { - CustomTypeNotFound: (typeName: string) => { - return Erreur.create(new Error('Unexpected: custom type not found')) - .with(ZenjsonErreurKey.Provider({ kind: 'CustomTypeNotFound', typeName })) - .withName('ZenjsonErreur'); - }, - KeyNotFound: (keyName: string) => { - return Erreur.create(new Error(`Key ${keyName} not found`)) - .with(ZenjsonErreurKey.Provider({ kind: 'KeyNotFound', keyName })) - .withName('ZenjsonErreur'); - }, - UnexpectedSpecialValue: (value: unknown) => { - return Erreur.create(new Error(`Unexpected special number value ${String(value)}`)) - .with(ZenjsonErreurKey.Provider({ kind: 'UnexpectedSpecialValue', value })) - .withName('ZenjsonErreur'); - }, - InvalidSerializedSpecialValue: (value: string) => { - return Erreur.create(new Error(`Invalid serialized special number: ${value}`)) - .with(ZenjsonErreurKey.Provider({ kind: 'InvalidSerializedSpecialValue', value })) - .withName('ZenjsonErreur'); - }, - DuplicatedCustomTypeName: (typeName: string) => { - return Erreur.create(new Error(`Invalid custom type list: duplicated type name: "${typeName}"`)) - .with(ZenjsonErreurKey.Provider({ kind: 'DuplicatedCustomTypeName', typeName })) - .withName('ZenjsonErreur'); - }, -}; +const ZenjsonErreurInternal = createErreurStore(); + +export const ZenjsonErreur = ZenjsonErreurInternal.asReadonly; + +export function throwCustomTypeNotFound(typeName: string): never { + return ZenjsonErreurInternal.setAndThrow(`Unexpected: custom type "${typeName}" not found`, { + kind: 'CustomTypeNotFound', + typeName, + }); +} + +export function throwKeyNotFound(keyName: string): never { + return ZenjsonErreurInternal.setAndThrow(`Key "${keyName}" not found`, { kind: 'KeyNotFound', keyName }); +} + +export function throwUnexpectedSpecialValue(value: unknown): never { + return ZenjsonErreurInternal.setAndThrow(`Unexpected special number value ${String(value)}`, { + kind: 'UnexpectedSpecialValue', + value, + }); +} + +export function throwInvalidSerializedSpecialValue(value: string): never { + return ZenjsonErreurInternal.setAndThrow(`Invalid serialized special number: ${value}`, { + kind: 'InvalidSerializedSpecialValue', + value, + }); +} + +export function throwDuplicatedCustomTypeName(typeName: string): never { + return ZenjsonErreurInternal.setAndThrow(`Invalid custom type list: duplicated type name: "${typeName}"`, { + kind: 'DuplicatedCustomTypeName', + typeName, + }); +} diff --git a/src/mod.ts b/src/mod.ts index 2413440..dfa4842 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -1,4 +1,4 @@ -export { ZenjsonErreur, ZenjsonErreurKey, type TZenjsonErreurData } from './erreur'; +export { ZenjsonErreur, type TZenjsonErreurData } from './erreur'; export { createRestore, restore } from './restore'; export { createSanitize, sanitize } from './sanitize'; export { createTypedKey, createTypedMap, type ITypedKey, type ITypedMap } from './typedMap'; diff --git a/src/restore.ts b/src/restore.ts index 2eb4e1c..4707144 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -1,4 +1,4 @@ -import { ZenjsonErreur } from './erreur'; +import { throwCustomTypeNotFound } from './erreur'; import type { ITypedMap } from './typedMap'; import { createTypedMap } from './typedMap'; import type { IRestoreContext, TCustomTypes } from './types'; @@ -28,7 +28,7 @@ function restoreInternal(data: unknown, customTypes: TCustomTypes, state: ITyped const [typeName, sanitized] = item; const type = customTypes.find((t) => t.name === typeName); if (!type) { - throw ZenjsonErreur.CustomTypeNotFound(typeName); + return throwCustomTypeNotFound(typeName); } return type.restore(sanitized, restoreCtx); } diff --git a/src/typedMap.ts b/src/typedMap.ts index e8b6f5d..4ccaf71 100644 --- a/src/typedMap.ts +++ b/src/typedMap.ts @@ -1,4 +1,4 @@ -import { ZenjsonErreur } from './erreur'; +import { throwKeyNotFound } from './erreur'; const TYPED_KEY = Symbol('TYPED_KEY'); @@ -53,7 +53,7 @@ export function createTypedMap(): ITypedMap { function getOrFail(key: ITypedKey): T { const value = get(key); if (value === undefined) { - throw ZenjsonErreur.KeyNotFound(key.name); + return throwKeyNotFound(key.name); } return value; } diff --git a/src/types.ts b/src/types.ts index 2ac00fc..b60ad81 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,8 @@ -import { ZenjsonErreur } from './erreur'; +import { + throwDuplicatedCustomTypeName, + throwInvalidSerializedSpecialValue, + throwUnexpectedSpecialValue, +} from './erreur'; import type { ITypedMap } from './typedMap'; export interface ICheckContext { @@ -63,7 +67,7 @@ export const specialNumberType: ICustomType { if (str === 'NaN') { @@ -75,7 +79,7 @@ export const specialNumberType: ICustomType { if (names.has(type.name)) { - throw ZenjsonErreur.DuplicatedCustomTypeName(type.name); + return throwDuplicatedCustomTypeName(type.name); } names.add(type.name); });