Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/lint and type events service #8342

Merged
merged 8 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .eslintrc.events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* OpenCRVS is also distributed under the terms of the Civil Registration
* & Healthcare Disclaimer located at http://opencrvs.org/license.
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
module.exports = {
rules: {
'@typescript-eslint/await-thenable': 2,
'@typescript-eslint/no-empty-function': 'warn',
'@typescript-eslint/consistent-type-definitions': 1,
'@typescript-eslint/default-param-last': 2,
'@typescript-eslint/no-dynamic-delete': 2,
'@typescript-eslint/no-explicit-any': 1,
'@typescript-eslint/no-floating-promises': 2,
'@typescript-eslint/no-misused-promises': 2,
'@typescript-eslint/no-unnecessary-boolean-literal-compare': 2,
'@typescript-eslint/no-non-null-assertion': 2,
'@typescript-eslint/no-unnecessary-condition': 1,
'@typescript-eslint/no-unsafe-argument': 1,
'@typescript-eslint/no-unsafe-return': 1,
'@typescript-eslint/prefer-includes': 1,
'@typescript-eslint/promise-function-async': 2,
'@typescript-eslint/require-await': 2,
'@typescript-eslint/return-await': 2,
'@typescript-eslint/switch-exhaustiveness-check': 2,
'func-style': [
'error',
'declaration',
{
allowArrowFunctions: true
}
],
'no-restricted-syntax': [
'error',
{
selector:
':matches(Program > VariableDeclaration) > VariableDeclarator > ArrowFunctionExpression',
message: 'Top-level arrow functions are not allowed.'
}
],
'no-shadow': 1,
'no-undef-init': 2,
'no-return-assign': 2,
'vars-on-top': 1,
'block-spacing': ['warn', 'always'],
curly: ['warn', 'all'],
'no-nested-ternary': 'warn',
'no-multiple-empty-lines': 1,
'prefer-const': 1,
'block-scoped-var': 1,
'import/no-cycle': 1,
'max-lines': ['warn', 600]
},
parser: '@typescript-eslint/parser'
}
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test:watch": "vitest",
"lint": "yarn lint:css && yarn lint:ts",
"lint:css": "stylelint 'src/**/*.{ts,tsx}'",
"lint:ts": "eslint --fix './src/**/*.{ts,tsx}' --max-warnings=353",
"lint:ts": "eslint --fix './src/**/*.{ts,tsx}' --max-warnings=314",
"test:compilation": "tsc --noEmit",
"extract:translations": "bash extract-translations.sh",
"generate-gateway-types": "NODE_OPTIONS=--dns-result-order=ipv4first graphql-codegen --config codegen.ts && prettier --write src/utils/gateway.ts",
Expand Down
60 changes: 8 additions & 52 deletions packages/client/src/v2-events/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,18 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
module.exports = {
extends: '../../../../.eslintrc.events.js',
rules: {
'@typescript-eslint/await-thenable': 2,
'@typescript-eslint/no-empty-function': 'warn',
'@typescript-eslint/consistent-type-definitions': 1,
'@typescript-eslint/default-param-last': 2,
'@typescript-eslint/no-dynamic-delete': 2,
'@typescript-eslint/no-explicit-any': 1,
'@typescript-eslint/no-floating-promises': 2,
'@typescript-eslint/no-misused-promises': 2,
'@typescript-eslint/no-unnecessary-boolean-literal-compare': 2,
'@typescript-eslint/no-non-null-assertion': 2,
'@typescript-eslint/no-unnecessary-condition': 1,
'@typescript-eslint/no-unsafe-argument': 1,
'@typescript-eslint/no-unsafe-return': 1,
'@typescript-eslint/prefer-includes': 1,
'@typescript-eslint/promise-function-async': 2,
'@typescript-eslint/require-await': 2,
'@typescript-eslint/return-await': 2,
'@typescript-eslint/switch-exhaustiveness-check': 2,
'func-style': [
'error',
'declaration',
{
allowArrowFunctions: true
}
],
'no-restricted-syntax': [
'error',
'react/jsx-no-literals': 1,
'react/destructuring-assignment': 1,
'react/jsx-sort-props': [
1,
{
selector:
':matches(Program > VariableDeclaration) > VariableDeclarator > ArrowFunctionExpression',
message: 'Top-level arrow functions are not allowed.'
reservedFirst: true,
callbacksLast: true,
shorthandFirst: true
}
],
'react/jsx-no-literals': 1,
'no-shadow': 1,
'no-undef-init': 2,
'no-return-assign': 2,
'vars-on-top': 1,
'block-spacing': ['warn', 'always'],
curly: ['warn', 'all'],
'no-nested-ternary': 'warn',
'react/destructuring-assignment': 1,
'no-multiple-empty-lines': 1,
'prefer-const': 1,
'block-scoped-var': 1,
'import/order': [
'error',
{
Expand All @@ -73,16 +39,6 @@ module.exports = {
pathGroupsExcludedImportTypes: ['builtin']
}
],
'import/no-cycle': 1,
'max-lines': ['warn', 600],
'react/jsx-sort-props': [
1,
{
reservedFirst: true,
callbacksLast: true,
shorthandFirst: true
}
],
'no-restricted-imports': [
'error',
{
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/v2-events/components/forms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function evalExpressionInFieldDefinition(
export function hasInitialValueDependencyInfo(
value: BaseField['initialValue']
): value is DependencyInfo {
return typeof value === 'object' && value !== null && 'dependsOn' in value
return typeof value === 'object' && 'dependsOn' in value
}

export function getDependentFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function Pages() {
)
const formPages = configuration.actions
.find((action) => action.type === ActionType.DECLARE)
?.forms.find((form) => form.active)?.pages
?.forms.find((f) => f.active)?.pages

if (!formPages) {
throw new Error('Form configuration not found for type: ' + event.type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ export function Review() {

const { eventConfiguration: config } = useEventConfiguration(event.type)

if (!config) {
throw new Error('Event configuration not found with type: ' + event.type)
}

const { forms: formConfigs } = config.actions.filter(
(action) => action.type === 'DECLARE'
)[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function Pages() {
)
const formPages = configuration.actions
.find((action) => action.type === ActionType.REGISTER)
?.forms.find((form) => form.active)?.pages
?.forms.find((f) => f.active)?.pages

if (!formPages) {
throw new Error('Form configuration not found for type: ' + event.type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ export function Review() {

const { eventConfiguration: config } = useEventConfiguration(event.type)

if (!config) {
throw new Error('Event configuration not found with type: ' + event.type)
}

const { forms: formConfigs } = config.actions.filter(
(action) => action.type === ActionType.REGISTER
)[0]
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/v2-events/features/events/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { EventConfig, FormConfig } from '@opencrvs/commons/client'
import { EventConfigInput, FormConfig } from '@opencrvs/commons/client'

export const DEFAULT_FORM = {
label: {
Expand Down Expand Up @@ -262,4 +262,4 @@ export const tennisClubMembershipEvent = {
}
}
]
} satisfies EventConfig
} satisfies EventConfigInput
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function waitUntilEventIsCreated<T extends { eventId: string }, R>(
const localVersion = utils.event.get.getData(eventId)

if (!localVersion || localVersion.id === localVersion.transactionId) {
// eslint-disable-next-line no-console
console.error(
'Event that has not been stored yet cannot be actioned upon'
)
Expand Down
78 changes: 54 additions & 24 deletions packages/commons/src/events/DeduplicationConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,71 @@ const DateDistanceMatcher = Matcher.extend({
})
})

export type And = {
export type AndInput = {
type: 'and'
clauses: any[]
clauses: ClauseInput[]
}

const And: z.ZodType<And> = z.object({
type: z.literal('and'),
clauses: z.lazy(() => Clause.array())
})
export type AndOutput = {
type: 'and'
clauses: ClauseOutput[]
}

export type Or = {
export type OrInput = {
type: 'or'
clauses: any[]
clauses: ClauseInput[]
}

const Or: z.ZodType<Or> = z.object({
export type OrOutput = {
type: 'or'
clauses: ClauseOutput[]
}

const And = z.object({
type: z.literal('and'),
clauses: z.lazy(() => Clause.array())
})

const Or = z.object({
type: z.literal('or'),
clauses: z.lazy(() => Clause.array())
})

export type Clause =
| And
| Or
| z.infer<typeof FuzzyMatcher>
| z.infer<typeof StrictMatcher>
| z.infer<typeof DateRangeMatcher>

export const Clause = z.union([
And,
Or,
FuzzyMatcher,
StrictMatcher,
DateRangeMatcher,
DateDistanceMatcher
])
export type ClauseInput =
| AndInput
| OrInput
| z.input<typeof FuzzyMatcher>
| z.input<typeof StrictMatcher>
| z.input<typeof DateRangeMatcher>
| z.input<typeof DateDistanceMatcher>

export type ClauseOutput =
| AndOutput
| OrOutput
| z.output<typeof FuzzyMatcher>
| z.output<typeof StrictMatcher>
| z.output<typeof DateRangeMatcher>
| z.output<typeof DateDistanceMatcher>

/**
* Defines a deduplication clause. Clauses are either matcher clauses or logical clauses. Logical clauses (and, or) are used to combine multiple clauses.
* Since the definiton is recursive, we use z.lazy to define the schema.
* Zod supports recursive schemas, but needs help with Input and Output types.
*
* Default assumption is that the ZodType is the input. Markers use default values, so we need to explicitly define output type, too.
*
*/
export const Clause: z.ZodType<ClauseOutput, z.ZodTypeDef, ClauseInput> =
z.lazy(() =>
z.discriminatedUnion('type', [
And,
Or,
FuzzyMatcher,
StrictMatcher,
DateRangeMatcher,
DateDistanceMatcher
])
)

export const DeduplicationConfig = z.object({
id: z.string(),
Expand Down
2 changes: 1 addition & 1 deletion packages/events/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
module.exports = {
extends: '../../.eslintrc.js',
extends: '../../.eslintrc.events.js',
env: {
es6: true
},
Expand Down
2 changes: 1 addition & 1 deletion packages/events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"testcontainers": "^10.15.0",
"ts-jest": "27.1.4",
"ts-node": "^6.1.1",
"typescript": "4.9.5",
"typescript": "5.6.3",
"vite-tsconfig-paths": "^3.5.0",
"vitest": "^2.1.5"
},
Expand Down
Loading
Loading