Janiczek/elm-syntax-type-inference/2.0.0

NOTE: the latest version is 2.0.1

Elm.TypeInference

Type inference for elm-syntax ASTs.

The process:

{ directDependencies : List PackageName
, allDependencies : List Dependency
, sourcesToResolveAmbiguity : Dict PackageName (List File)
, projectPackageName : Maybe PackageName
, projectFiles : List File
}

Build a Project from dependencies and project sources.

  • directDependencies: names of the packages listed in the project's elm.json (e.g. "elm/core").
  • allDependencies: type information for every dependency in the closure, parsed from each dependency's elm.json and docs.json.
  • sourcesToResolveAmbiguity: Elm sources from dependencies' ELM_HOME needed to disambiguate hidden types. Start with Dict.empty; if you get Err (NeedPackageSources needed), read and parse those files and retry with them supplied.
  • projectPackageName: the project's own package name (Just for packages, Nothing for applications).
  • projectFiles: the project's Elm sources.

A Project with no dependencies and no files, not even elm/core.

Handy for tests and mocks. Add modules with addFile.

type Project

An indexed project: every file's imports are resolved, but nothing has been solved yet. Cheap to build.

Keep sources up to date incrementally with addFile and removeFile.

type alias Dependency =
{ name : PackageName
, dependencies : List PackageName
, modules : List Module
}

A dependency package with its type information, parsed from the dependency's elm.json (via Elm.Project.decoder) and docs.json (via elm/project-metadata-utils Elm.Docs.decoder):

  • name: the package identifier (e.g. "elm/html").
  • dependencies: names of the package's immediate elm.json dependencies (eg. "elm/virtual-dom").
  • modules: the decoded docs.json modules.
getType : ModuleName -> Range -> Project -> ( Result InferError Type, Project )

Get the Type for a given source Range.

The computed data is cached into a new version of the Project, save it into your model to speed up future getType calls.

ModuleName
-> ( Result InferError (List ( Range, Type )), Project )

Mostly a test helper.

Returns Types of all AST nodes in the project.

Fully expand the type aliases in a Type.

type alias X =
    { a : Int, b : String }


foo : X
foo = { a = 5, b = "hello" }


-- Assuming you `getType` for the `foo`:
Named
   { package = ""
   , moduleName = ["Main"]
   , name = "X"
   , arguments = []
   }


-- You can expand it:
expand project theTypeAbove
-->
Record
    { fields =
        Dict.fromList
            [ ( "a", Int )
            , ( "b", String )
            ]
    }
addFile : File -> Project -> Result ProjectError Project

Make Project aware of a new or changed file.

The module and everything that (transitively) imports it is invalidated behind the scenes and will be re-inferred lazily by getType.

removeFile : ModuleName -> Project -> Project

Remove a module from a Project.

Everything that (transitively) imported it is invalidated behind the scenes and will be re-inferred lazily by getType.