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

front: turn on @typescript-eslint/no-unsafe-argument #10234

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions front/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-unsafe-return": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/non-nullable-type-assertion-style": "error",

"@typescript-eslint/no-restricted-types": [
Expand Down
2 changes: 1 addition & 1 deletion front/src/applications/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const Editor = () => {
...s,
state: {
...s.state,
...(typeof stateOrReducer === 'function' ? stateOrReducer(s.state) : stateOrReducer),
...(typeof stateOrReducer === 'function' ? stateOrReducer(s.state as S) : stateOrReducer),
},
}));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ const IntervalEditorComponent = (
}}
formData={selectedData}
onChange={(e) => {
const newItem = e.formData;
const newItem: LinearMetadataItem = e.formData;
const oldItem = data[selected];
let newData = [...data];
// we keep the old value for begin and end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import cx from 'classnames';
import { toNumber } from 'lodash';
import { useTranslation } from 'react-i18next';

export const FormLineStringLength = ({ id, value, required, onChange }: WidgetProps) => {
export const FormLineStringLength = ({ id, value: rawValue, required, onChange }: WidgetProps) => {
const { t } = useTranslation();
const value = toNumber(rawValue);

const [length, setLength] = useState<number>(toNumber(value));
const [length, setLength] = useState<number>(value);

useEffect(() => {
setLength(value);
}, [value]);

const hasChanged = useMemo(() => toNumber(value) !== length, [value, length]);
const hasChanged = useMemo(() => value !== length, [value, length]);

return (
<div>
Expand Down Expand Up @@ -43,7 +44,7 @@ export const FormLineStringLength = ({ id, value, required, onChange }: WidgetPr
type="button"
disabled={!hasChanged}
className={cx('btn btn-sm btn-secondary', { disabled: !hasChanged })}
onClick={() => setLength(toNumber(value))}
onClick={() => setLength(value)}
>
{t('common.cancel')}
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export const BasePointEditionLayers = ({
[renderedEntity]
);

const type = cleanSymbolType((entity.properties || {}).extensions?.sncf?.installation_type || '');
const entityProps = (entity.properties || {}) as {
extensions?: { sncf?: { installation_type?: string } };
};
const type = cleanSymbolType(entityProps.extensions?.sncf?.installation_type || '');
const layers = useMemo(
() =>
SourcesDefinitionsIndex[objType](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BiReset } from 'react-icons/bi';
import { LAYER_TO_EDITOAST_DICT, LAYERS_SET, type Layer } from 'applications/editor/consts';
import { NEW_ENTITY_ID } from 'applications/editor/data/utils';
import { DEFAULT_COMMON_TOOL_STATE } from 'applications/editor/tools/consts';
import type { SwitchEntity } from 'applications/editor/tools/switchEdition/types';
import type { TrackSectionEntity } from 'applications/editor/tools/trackEdition/types';
import { approximatePointDistanceForEditoast } from 'applications/editor/tools/utils';
import type { PartialOrReducer, ReadOnlyEditorContextType, Tool } from 'applications/editor/types';
Expand Down Expand Up @@ -213,19 +214,20 @@ function getRangeEditionTool<T extends EditorRange>({

if (interactionState.type === 'selectSwitch') {
if (feature && feature.sourceLayer === 'switches') {
if (Object.keys(selectedSwitches).includes(feature.properties.id)) {
const featureProps = feature.properties as SwitchEntity['properties'];
if (Object.keys(selectedSwitches).includes(featureProps.id)) {
setState({
selectedSwitches: Object.fromEntries(
Object.entries(selectedSwitches).filter(([key]) => key !== feature.properties.id)
Object.entries(selectedSwitches).filter(([key]) => key !== featureProps.id)
),
});
} else
setState({
selectedSwitches: {
...selectedSwitches,
[feature.properties.id]: {
[featureProps.id]: {
position: null,
type: feature.properties.switch_type,
type: featureProps.switch_type,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
} from 'applications/editor/tools/switchEdition/utils';
import { Loader } from 'common/Loaders';

import TrackSectionEndpointSelector from './TrackSectionEndpointSelector';
import TrackSectionEndpointSelector, {
type TrackSectionEndpointSelectorProps,
} from './TrackSectionEndpointSelector';

const TrackNodeTypeDiagram = lazy(() => import('./TrackNodeTypeDiagram'));

Expand All @@ -31,7 +33,7 @@ const CustomSchemaField = (props: FieldProps) => {
</div>
);
if (name.indexOf(FLAT_SWITCH_PORTS_PREFIX) === 0)
return <TrackSectionEndpointSelector {...props} />;
return <TrackSectionEndpointSelector {...(props as TrackSectionEndpointSelectorProps)} />;
return <SchemaField {...props} />;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ENDPOINTS,
type PortEndPointCandidate,
type SwitchEditionState,
type TrackEndpoint,
} from 'applications/editor/tools/switchEdition/types';
import { FLAT_SWITCH_PORTS_PREFIX } from 'applications/editor/tools/switchEdition/utils';
import type { ExtendedEditorContextType } from 'applications/editor/types';
Expand All @@ -26,7 +27,14 @@ const ENDPOINTS_SET = new Set(ENDPOINTS);
const ENDPOINT_OPTIONS = ENDPOINTS.map((s) => ({ value: s, label: s }));
const ENDPOINT_OPTIONS_DICT = keyBy(ENDPOINT_OPTIONS, 'value');

const TrackSectionEndpointSelector = ({ schema, formData, onChange, name }: FieldProps) => {
export type TrackSectionEndpointSelectorProps = FieldProps<TrackEndpoint>;

const TrackSectionEndpointSelector = ({
schema,
formData,
onChange,
name,
}: TrackSectionEndpointSelectorProps) => {
const dispatch = useAppDispatch();
const { state, setState } = useContext(
EditorContext
Expand All @@ -51,7 +59,10 @@ const TrackSectionEndpointSelector = ({ schema, formData, onChange, name }: Fiel
const infraID = useInfraID();

const portId = name.replace(FLAT_SWITCH_PORTS_PREFIX, '');
const endpoint = ENDPOINTS_SET.has(formData?.endpoint) ? formData.endpoint : DEFAULT_ENDPOINT;
const endpoint =
formData?.endpoint && ENDPOINTS_SET.has(formData.endpoint)
? formData.endpoint
: DEFAULT_ENDPOINT;
const [trackSection, setTrackSection] = useState<TrackSectionEntity | null>(null);

const isPicking =
Expand Down
6 changes: 4 additions & 2 deletions front/src/applications/editor/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,13 @@ export async function getEntitiesBbox(
}

if (entities.length === 1 && entities[0].objType === 'Route') {
const entity = entities[0] as RouteEntity;

// get start point
const { id: startPointId, type: startPointIdType } = entities[0].properties.entry_point;
const { id: startPointId, type: startPointIdType } = entity.properties.entry_point;
const startPoint = await getEntity(infraId, startPointId, startPointIdType, dispatch);
// get end point
const { id: endPointId, type: endPointIdType } = entities[0].properties.exit_point;
const { id: endPointId, type: endPointIdType } = entity.properties.exit_point;
const endPoint = await getEntity(infraId, endPointId, endPointIdType, dispatch);

const fc = featureCollection([startPoint, endPoint]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const StdcmInputVia = ({ stopType, stopDuration, updatePathStepStopTime }: Stdcm
const stopWarning = stopType === StdcmStopTypes.DRIVER_SWITCH && stopDuration && stopDuration < 3;

const debounceUpdatePathStepStopTime = useMemo(
() => debounce((value) => updatePathStepStopTime(value), 300),
() => debounce((value: string) => updatePathStepStopTime(value), 300),
[]
);

Expand Down
4 changes: 2 additions & 2 deletions front/src/common/IntervalsDataViz/dataviz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export const LinearMetadataDataviz = <T extends { [key: string]: any }>({
}
const dMin = minBy(data, field);
const dMax = maxBy(data, field);
setMin(dMin && dMin[field] < 0 ? dMin[field] : 0);
setMax(dMax && dMax[field] > 0 ? dMax[field] : 0);
setMin(dMin && dMin[field] < 0 ? (dMin[field] as number) : 0);
setMax(dMax && dMax[field] > 0 ? (dMax[field] as number) : 0);
} else {
setMin(0);
setMax(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useMap, type MapLayerMouseEvent } from 'react-map-gl/maplibre';

import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import Collapsable from 'common/Collapsable';
import type { IncompatibleConstraintFeature } from 'common/Map/types';
import { getMapMouseEventNearestFeature } from 'utils/mapHelper';

import IncompatibleConstraintsFilters from './IncompatibleConstrainstFilters';
Expand Down Expand Up @@ -88,8 +89,10 @@ const IncompatibleConstraints = ({ pathProperties }: IncompatibleConstraintsProp
const nearestResult = getMapMouseEventNearestFeature(e, {
layersId: ['pathfinding-incompatible-constraints'],
});
if (nearestResult?.feature && nearestResult?.feature.properties.ids) {
setHoveredConstraint(new Set(JSON.parse(nearestResult.feature.properties.ids)));
const nearestFeature = nearestResult?.feature as IncompatibleConstraintFeature | undefined;
if (nearestFeature && nearestFeature.properties.ids) {
const ids: string[] = JSON.parse(nearestFeature.properties.ids);
setHoveredConstraint(new Set(ids));
} else {
setHoveredConstraint(new Set([]));
}
Expand All @@ -102,17 +105,18 @@ const IncompatibleConstraints = ({ pathProperties }: IncompatibleConstraintsProp
const nearestResult = getMapMouseEventNearestFeature(e, {
layersId: ['pathfinding-incompatible-constraints'],
});
if (nearestResult?.feature && nearestResult?.feature.properties.ids) {
const nearestFeature = nearestResult?.feature as IncompatibleConstraintFeature | undefined;
if (nearestFeature && nearestFeature.properties.ids) {
setSelectedConstraint((prev) => {
const nextSelected = JSON.parse(nearestResult.feature.properties.ids);
const nextSelected: string[] = JSON.parse(nearestFeature.properties.ids);
if (isArray(nextSelected)) {
// if we click on the same selected value
// => we clear the selection
if (prev.size === nextSelected.length && nextSelected.every((s) => prev.has(s))) {
return new Set([]);
}
// select the clicked elements
return new Set(nextSelected as string[]);
return new Set(nextSelected);
}
return new Set([]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('getAxis', () => {
`(
'should return the correct axis name (axis=$axis, rotate=$rotate, expected=$expected',
({ axis, rotate, expected }) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
expect(getAxis(CHART_AXES.SPACE_TIME, axis, rotate)).toBe(expected);
}
);
Expand Down
4 changes: 2 additions & 2 deletions front/src/modules/timesStops/TimesStops.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { type TableType, type TimesStopsRow } from './types';
type TimesStopsProps<T extends TimesStopsRow> = {
rows: T[];
tableType: TableType;
cellClassName?: DataSheetGridProps['cellClassName'];
stickyRightColumn?: DataSheetGridProps['stickyRightColumn'];
cellClassName?: DataSheetGridProps<T>['cellClassName'];
stickyRightColumn?: DataSheetGridProps<T>['stickyRightColumn'];
headerRowHeight?: number;
onChange?: (newRows: T[], operation: Operation) => void;
dataIsLoading: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const processJsonFile = (
const isJsonFile = fileExtension === 'application/json';

// try to parse the file content
let rawContent;
let rawContent: Partial<TrainScheduleBase>[];
try {
rawContent = JSON.parse(fileContent);
} catch {
Expand Down
5 changes: 3 additions & 2 deletions front/src/utils/mapHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import WebMercatorViewport from 'viewport-mercator-project';

import type { Layer } from 'applications/editor/consts';
import { getAngle } from 'applications/editor/data/utils';
import type { TrackFeature } from 'common/Map/types';
import type { Zone } from 'types';
import { nearestPointOnLine } from 'utils/geometry';

Expand Down Expand Up @@ -405,10 +406,10 @@ export function getMapMouseEventNearestFeature(
export function getNearestTrack(
coordinates: number[] | Position,
map: Map
): { track: Feature<LineString>; nearest: number[]; distance: number } | null {
): { track: TrackFeature; nearest: number[]; distance: number } | null {
const tracks = map.queryRenderedFeatures(undefined, {
layers: ['chartis/tracks-geo/main'],
}) as Feature<LineString>[];
}) as TrackFeature[];
const result = head(
sortBy(
tracks.map((track) => {
Expand Down
Loading