Skip to content

Commit 4b19cd5

Browse files
committed
🚧 Update declaration list visitor
1 parent 7ad49fb commit 4b19cd5

File tree

1 file changed

+84
-25
lines changed

1 file changed

+84
-25
lines changed

src/NoUnsortedRecords.elm

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,18 +1277,6 @@ annotToFields annot =
12771277
Nothing
12781278

12791279

1280-
{-| Unwrap a type alias from a declaration (if it is one).
1281-
-}
1282-
getAlias : Node Declaration -> Maybe TypeAlias
1283-
getAlias node =
1284-
case Node.value node of
1285-
AliasDeclaration alias_ ->
1286-
Just alias_
1287-
1288-
_ ->
1289-
Nothing
1290-
1291-
12921280
{-| Make subrecords for custom types, if appropriate.
12931281
-}
12941282
makeCustomTypeSubrecords : SubrecordCanonicity -> String -> List TypeWithPositionalVars -> List ( String, KnownRecord )
@@ -1364,20 +1352,91 @@ getExposedNames =
13641352

13651353
{-| Visit declarations, storing record field orders.
13661354
-}
1367-
declarationListVisitor : RuleConfig -> ModuleContext -> List (Node Declaration) -> ModuleContext
1355+
declarationListVisitor :
1356+
RuleConfig
1357+
->
1358+
{ moduleName : ModuleName
1359+
, lookupTable : ModuleNameLookupTable
1360+
, aliases : Dict ModuleName (Dict String TypeWithPositionalVars)
1361+
, canonicalRecords : Dict ModuleName (Dict String KnownRecord)
1362+
, constructors : Dict ModuleName (Dict String { customTypeName : Maybe String, type_ : TypeWithPositionalVars })
1363+
, functionTypes : Dict ModuleName (Dict String Type)
1364+
, exposingList : Maybe ExposedNames
1365+
, fileIsIgnored : Bool
1366+
}
1367+
-> List (Node Declaration)
1368+
->
1369+
{ aliases : Dict ModuleName (Dict String TypeWithPositionalVars)
1370+
, canonicalRecords : Dict ModuleName (Dict String KnownRecord)
1371+
, constructors : Dict ModuleName (Dict String { customTypeName : Maybe String, type_ : TypeWithPositionalVars })
1372+
, functionTypes : Dict ModuleName (Dict String Type)
1373+
, exposed :
1374+
{ aliases : Dict String TypeWithPositionalVars
1375+
, canonicalRecords : Dict String KnownRecord
1376+
, constructors : Dict String { customTypeName : Maybe String, type_ : TypeWithPositionalVars }
1377+
, functionTypes : Dict String Type
1378+
}
1379+
}
13681380
declarationListVisitor (RuleConfig { subrecordTreatment }) context declarations =
1369-
let
1370-
makeAliasInfo : TypeAlias -> ( String, TypeWithPositionalVars )
1371-
makeAliasInfo { name, generics, typeAnnotation } =
1372-
( Node.value name
1373-
, typeAnnotToTypeWithPositionalVars context
1374-
-- Constrained type vars are not respected for aliases
1375-
{ constrainedTypeVarsAreRespected = False
1376-
, subrecordIsAlsoCanonical = subrecordCanonicityForRecord subrecordTreatment
1377-
}
1378-
(List.map Node.value generics)
1379-
typeAnnotation
1380-
)
1381+
-- Find aliases, canonical records, and function types and store them
1382+
List.foldl (accumulateDeclarationInfo subrecordTreatment context)
1383+
{ aliases = []
1384+
, canonicalRecords = []
1385+
, constructors = []
1386+
, functionTypes = []
1387+
, exposedAliases = []
1388+
, exposedCanonicalRecords = []
1389+
, exposedConstructors = []
1390+
, exposedFunctionTypes = []
1391+
}
1392+
declarations
1393+
|> (\r ->
1394+
if context.fileIsIgnored then
1395+
{ aliases = Dict.empty
1396+
, canonicalRecords = Dict.empty
1397+
, constructors = Dict.empty
1398+
, functionTypes = Dict.empty
1399+
, exposed =
1400+
{ aliases =
1401+
Dict.fromList r.exposedAliases
1402+
, canonicalRecords =
1403+
Dict.fromList r.exposedCanonicalRecords
1404+
, constructors =
1405+
Dict.fromList r.exposedConstructors
1406+
, functionTypes =
1407+
Dict.fromList r.exposedFunctionTypes
1408+
}
1409+
}
1410+
1411+
else
1412+
{ aliases =
1413+
validate (not << List.isEmpty) r.aliases
1414+
|> Maybe.map Dict.fromList
1415+
|> MaybeX.unwrap context.aliases (\v -> Dict.insert context.moduleName v context.aliases)
1416+
, canonicalRecords =
1417+
validate (not << List.isEmpty) r.canonicalRecords
1418+
|> Maybe.map Dict.fromList
1419+
|> MaybeX.unwrap context.canonicalRecords (\v -> Dict.insert context.moduleName v context.canonicalRecords)
1420+
, constructors =
1421+
validate (not << List.isEmpty) r.constructors
1422+
|> Maybe.map Dict.fromList
1423+
|> MaybeX.unwrap context.constructors (\v -> Dict.insert context.moduleName v context.constructors)
1424+
, functionTypes =
1425+
validate (not << List.isEmpty) r.functionTypes
1426+
|> Maybe.map Dict.fromList
1427+
|> MaybeX.unwrap context.functionTypes (\v -> Dict.insert context.moduleName v context.functionTypes)
1428+
, exposed =
1429+
{ aliases =
1430+
Dict.fromList r.exposedAliases
1431+
, canonicalRecords =
1432+
Dict.fromList r.exposedCanonicalRecords
1433+
, constructors =
1434+
Dict.fromList r.exposedConstructors
1435+
, functionTypes =
1436+
Dict.fromList r.exposedFunctionTypes
1437+
}
1438+
}
1439+
)
13811440

13821441
makeConstructorAndSubrecords : TypeWithPositionalVars -> List (Node String) -> ValueConstructor -> ( String, TypeWithPositionalVars, List ( String, KnownRecord ) )
13831442
makeConstructorAndSubrecords return typeVars { name, arguments } =

0 commit comments

Comments
 (0)