11import { E , F } from "@eslint-react/eff" ;
22import pm from "picomatch" ;
33import { match , P } from "ts-pattern" ;
4- import type { PartialDeep } from "type-fest" ;
5- import { parse } from "valibot" ;
4+ import { assert } from "valibot" ;
65
76import { normalizedSettingsCache } from "./cache" ;
87import { getReactVersion } from "./get-react-version" ;
@@ -22,34 +21,25 @@ export const DEFAULT_ESLINT_REACT_SETTINGS = {
2221} as const satisfies ESLintReactSettings ;
2322
2423/**
25- * Unsafely casts settings from a data object from ` context.settings` .
26- * @internal
27- * @param data The data object .
28- * @returns settings The settings.
24+ * Get the normalized ESLint settings for "react-x" from the given context.
25+ * @param context The context.
26+ * @param context.settings The ESLint settings .
27+ * @returns The normalized ESLint settings.
2928 */
30- export function unsafeReadSettings ( data : unknown ) : PartialDeep < ESLintReactSettings > {
31- // @ts -expect-error - skip type checking for unsafe cast
32- // eslint-disable-next-line @susisu/safe-typescript/no-type-assertion
33- return ( data ?. [ "react-x" ] ?? { } ) as PartialDeep < ESLintReactSettings > ;
34- }
35-
36- /**
37- * Normalizes the settings by converting all shorthand properties to their full form.
38- * @param data The raw settings.
39- * @returns The normalized settings.
40- * @internal
41- */
42- export function normalizeSettings ( data : unknown ) : ESLintReactSettingsNormalized {
43- const memoized = normalizedSettingsCache . get ( data ) ;
44- if ( memoized ) return memoized ;
45-
46- const settings = {
29+ export function getSettingsFromContext ( context : { settings : unknown } ) : ESLintReactSettingsNormalized {
30+ assert ( ESLintSettingsSchema , context . settings ) ;
31+ const raw = context . settings ?. [ "react-x" ] ?? { } ;
32+ const memoized = normalizedSettingsCache . get ( raw ) ;
33+ if ( memoized ) {
34+ return memoized ;
35+ }
36+ const rawWithDefaults = {
4737 ...DEFAULT_ESLINT_REACT_SETTINGS ,
48- ...parse ( ESLintSettingsSchema , data ) [ "react-x" ] ?? { } ,
38+ ...raw ,
4939 } ;
50- const additionalComponents = settings . additionalComponents ?? [ ] ;
40+ const additionalComponents = rawWithDefaults . additionalComponents ?? [ ] ;
5141 const normalized = {
52- ...settings ,
42+ ...rawWithDefaults ,
5343 additionalComponents : additionalComponents . map ( ( component ) => ( {
5444 ...component ,
5545 attributes : component . attributes ?. map ( ( attr ) => ( {
@@ -64,18 +54,14 @@ export function normalizeSettings(data: unknown): ESLintReactSettingsNormalized
6454 if ( ! / ^ [ \w - ] + $ / u. test ( name ) ) return acc ;
6555 return acc . set ( name , as ) ;
6656 } , new Map < string , string > ( ) ) ,
67- version : match ( settings . version )
57+ version : match ( rawWithDefaults . version )
6858 . with ( P . union ( P . nullish , "" , "detect" ) , ( ) => E . getOrElse ( getReactVersion ( ) , F . constant ( "19.0.0" ) ) )
6959 . otherwise ( F . identity ) ,
7060 } ;
71- normalizedSettingsCache . set ( data , normalized ) ;
61+ normalizedSettingsCache . set ( raw , normalized ) ;
7262 return normalized ;
7363}
7464
75- export function getSettingsFromContext ( context : { settings : unknown } ) : ESLintReactSettingsNormalized {
76- return normalizeSettings ( context . settings ) ;
77- }
78-
7965/**
8066 * A helper function to define settings for "react-x" with type checking in JavaScript files.
8167 * @param settings The settings.
0 commit comments