From 985681c7e86dc57e3245f1888bba9dca073b0d72 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 16 Sep 2024 15:20:07 +0200 Subject: [PATCH] front: enable @typescript-eslint/no-unsafe-return Part of #8772 See https://typescript-eslint.io/rules/no-unsafe-return/ --- front/.eslintrc | 1 + .../LinearMetadata/FormComponent.tsx | 23 ++++++++++++------- front/src/applications/editor/data/utils.ts | 1 + front/src/common/Map/Layers/GeoJSONs.tsx | 2 +- front/src/common/api/documentApi.ts | 3 ++- .../ChartHelpers/enableInteractivity.tsx | 3 ++- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/front/.eslintrc b/front/.eslintrc index 640a5ca21bd..4741908cc51 100644 --- a/front/.eslintrc +++ b/front/.eslintrc @@ -44,6 +44,7 @@ "@typescript-eslint/no-shadow": "error", "@typescript-eslint/no-use-before-define": "error", "@typescript-eslint/no-unnecessary-type-assertion": "error", + "@typescript-eslint/no-unsafe-return": "error", "@typescript-eslint/ban-types": [ "error", diff --git a/front/src/applications/editor/components/LinearMetadata/FormComponent.tsx b/front/src/applications/editor/components/LinearMetadata/FormComponent.tsx index 54ec42a9021..4c2874faa95 100644 --- a/front/src/applications/editor/components/LinearMetadata/FormComponent.tsx +++ b/front/src/applications/editor/components/LinearMetadata/FormComponent.tsx @@ -1,8 +1,9 @@ import { useState, useRef, useEffect, useCallback, useMemo } from 'react'; import Form, { getDefaultRegistry } from '@rjsf/core'; -import type { FieldProps } from '@rjsf/utils'; +import type { FieldProps, RJSFSchema } from '@rjsf/utils'; import validator from '@rjsf/validator-ajv8'; +import type { GeoJsonProperties, Geometry } from 'geojson'; import type { JSONSchema7 } from 'json-schema'; import { omit, head, max as fnMax, min as fnMin, isNil } from 'lodash'; import { useTranslation } from 'react-i18next'; @@ -34,7 +35,13 @@ import HelpModal from './HelpModal'; import { LinearMetadataTooltip } from './tooltip'; import 'common/IntervalsDataViz/style.scss'; -const IntervalEditorComponent = (props: FieldProps) => { +export type FormContext = { + geometry: Geometry; + length: number; + isCreation: boolean; +}; + +const IntervalEditorComponent = (props: FieldProps) => { const { name, formContext, formData, schema, onChange, registry } = props; const { openModal, closeModal } = useModal(); const { t } = useTranslation(); @@ -59,10 +66,10 @@ const IntervalEditorComponent = (props: FieldProps) => { // Get the distance of the geometry const distance = useMemo(() => { - if (!isNil(formContext.length)) { + if (!isNil(formContext?.length)) { return formContext.length; } - if (formContext.geometry?.type === 'LineString') { + if (formContext?.geometry?.type === 'LineString') { return getLineStringDistance(formContext.geometry); } return 0; @@ -414,16 +421,16 @@ const IntervalEditorComponent = (props: FieldProps) => { ); }; -export const FormComponent = (props: FieldProps) => { +export const FormComponent = (props: FieldProps) => { const { name, formContext, schema, registry } = props; const Fields = getDefaultRegistry().fields; // Get the distance of the geometry const distance = useMemo(() => { - if (!isNil(formContext.length)) { - return formContext.length!; + if (!isNil(formContext?.length)) { + return formContext.length; } - if (formContext.geometry?.type === 'LineString') { + if (formContext?.geometry?.type === 'LineString') { return getLineStringDistance(formContext.geometry); } return 0; diff --git a/front/src/applications/editor/data/utils.ts b/front/src/applications/editor/data/utils.ts index 97d3a7515e1..2aa4f852820 100644 --- a/front/src/applications/editor/data/utils.ts +++ b/front/src/applications/editor/data/utils.ts @@ -134,6 +134,7 @@ export function nestEntity(entity: EditorEntity, type: EditoastType): EditorEnti if (isLast) props[k] = oldProperties[key]; else props[k] = props[k] || {}; + // eslint-disable-next-line @typescript-eslint/no-unsafe-return return props[k]; }, newProperties); }); diff --git a/front/src/common/Map/Layers/GeoJSONs.tsx b/front/src/common/Map/Layers/GeoJSONs.tsx index a701fc0b55e..989bae032d1 100644 --- a/front/src/common/Map/Layers/GeoJSONs.tsx +++ b/front/src/common/Map/Layers/GeoJSONs.tsx @@ -418,7 +418,7 @@ interface EditorSourceProps { export const EditorSource = ({ id, data, layers, layerOrder }: EditorSourceProps) => { const dataFingerPrint = data.type === 'FeatureCollection' - ? data.features.map((f) => f.properties?.id).concat() + ? data.features.map((f) => f.properties?.id as unknown).concat() : data.properties?.id; return ( diff --git a/front/src/common/api/documentApi.ts b/front/src/common/api/documentApi.ts index 6bbb1a66470..2a21e25ae61 100644 --- a/front/src/common/api/documentApi.ts +++ b/front/src/common/api/documentApi.ts @@ -1,3 +1,4 @@ +import type { NewDocumentResponse } from 'common/api/osrdEditoastApi'; import mainConfig from 'config/config'; export const getDocument = async (documentKey: number): Promise => { @@ -13,6 +14,6 @@ export const postDocument = async (image: Blob) => { }, body: image, }); - const data = await res.json(); + const data: NewDocumentResponse = await res.json(); return data.document_key; }; diff --git a/front/src/modules/simulationResult/components/ChartHelpers/enableInteractivity.tsx b/front/src/modules/simulationResult/components/ChartHelpers/enableInteractivity.tsx index 3a5a06137bc..a3a108a1387 100644 --- a/front/src/modules/simulationResult/components/ChartHelpers/enableInteractivity.tsx +++ b/front/src/modules/simulationResult/components/ChartHelpers/enableInteractivity.tsx @@ -302,7 +302,8 @@ export const enableInteractivity = < setChart(newChart); }) .filter( - (event) => (event.button === 0 || event.button === 1) && (event.ctrlKey || event.shiftKey) + (event: MouseEvent) => + (event.button === 0 || event.button === 1) && (event.ctrlKey || event.shiftKey) ); // Updates in real time the position of the pointer and the vertical/horizontal guidelines