From 22787967b040f0258ccfab450044950932990a4f Mon Sep 17 00:00:00 2001 From: Eli <88557639+lishaduck@users.noreply.github.com> Date: Wed, 26 Jun 2024 07:07:37 -0500 Subject: [PATCH] chore: better elm.json types for packages --- lib/template-dependencies.js | 16 +++++++++------- lib/types/content.ts | 19 +++++++++++++++---- lib/types/utils.js | 12 ++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 lib/types/utils.js diff --git a/lib/template-dependencies.js b/lib/template-dependencies.js index 3c3171a38..81dcf2744 100644 --- a/lib/template-dependencies.js +++ b/lib/template-dependencies.js @@ -4,6 +4,7 @@ const MinVersion = require('./min-version'); const ErrorMessage = require('./error-message'); const DependencyProvider = require('./dependency-provider'); const FS = require('./fs-wrapper'); +const {getRecordEntries} = require('./types/utils'); /** @type {DependencyProvider | null} */ let dependencyProvider = null; @@ -13,7 +14,7 @@ const parseElmFolder = path.join(__dirname, '../parseElm'); /** * @import {Options} from "./types/options" * @import {VersionString} from "./types/version" - * @import {ApplicationElmJson, ApplicationDependencies, DependencyList} from "./types/content" + * @import {ApplicationElmJson, ApplicationDependencies, ApplicationDependencyList, PackageName, PackageDependencyList} from "./types/content" */ module.exports = { @@ -115,7 +116,7 @@ https://github.com/jfmengels/node-elm-review/issues/new * @param {Options} options * @param {VersionString} elmVersion * @param {string} elmJson - * @param {Record}extra + * @param {Record} extra * @param {boolean} onlineFirst * @returns {ApplicationDependencies} */ @@ -251,9 +252,9 @@ function filterOutDuplicateDependencies(testDependencies, regularDependencies) { /** * Filter out test-dependencies that are already in the regular dependencies (only on a section of the dependencies). * - * @param {DependencyList} testDependencies - * @param {DependencyList} regularDependencies - * @returns {DependencyList} + * @param {ApplicationDependencyList} testDependencies + * @param {ApplicationDependencyList} regularDependencies + * @returns {ApplicationDependencyList} */ function filterOutDuplicateDependenciesForSection( testDependencies, @@ -300,11 +301,12 @@ function update(options, elmJson) { true ); - const testDependenciesEntries = Object.entries( + /** @type {[PackageName, VersionString][]} */ + const testDependenciesEntries = getRecordEntries( elmJson['test-dependencies'].direct ); if (testDependenciesEntries.length > 0) { - /** @type {Record} */ + /** @type {PackageDependencyList} */ const packagesToAdd = {}; for (const [pkg, version] of testDependenciesEntries) { packagesToAdd[pkg] = `${version} <= v < ${nextVersion(version, 'major')}`; diff --git a/lib/types/content.ts b/lib/types/content.ts index 97494c97b..d96f2968a 100644 --- a/lib/types/content.ts +++ b/lib/types/content.ts @@ -1,4 +1,4 @@ -import type {VersionString} from './version.js'; +import type {VersionRange, VersionString} from './version.js'; import type {Path} from './path.js'; export type File = { @@ -39,14 +39,25 @@ export type ApplicationElmJson = { }; export type ApplicationDependencies = { - direct: DependencyList; - indirect: DependencyList; + direct: ApplicationDependencyList; + indirect: ApplicationDependencyList; }; -export type DependencyList = Record; +export type ApplicationDependencyList = Record; +export type PackageDependencyList = Record; + +export type PackageName = `${string}/${string}`; export type PackageElmJson = { type: 'package'; + name: PackageName; + summary: string; + license: string; + version: VersionString; + 'exposed-modules': (string | Record)[]; + 'elm-version': VersionRange; + dependencies: PackageDependencyList; + 'test-dependencies': PackageDependencyList; }; export type SourceDirectories = Path[]; diff --git a/lib/types/utils.js b/lib/types/utils.js new file mode 100644 index 000000000..979c959af --- /dev/null +++ b/lib/types/utils.js @@ -0,0 +1,12 @@ +/** + * @template T + * @template U + * @param {Record} o + * @returns {[U, T][]} + * + * @todo Find a better way to express constraints. + * @todo Is this even type safe? + */ +export function getRecordEntries(o) { + return /** @type {[U, T][]} */ (Object.entries(o)); +}