Skip to content

Commit

Permalink
chore: better elm.json types for packages
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jun 26, 2024
1 parent e122b3e commit 2278796
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
16 changes: 9 additions & 7 deletions lib/template-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {
Expand Down Expand Up @@ -115,7 +116,7 @@ https://github.com/jfmengels/node-elm-review/issues/new
* @param {Options} options
* @param {VersionString} elmVersion
* @param {string} elmJson
* @param {Record<string, string>}extra
* @param {Record<string, string>} extra
* @param {boolean} onlineFirst
* @returns {ApplicationDependencies}
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, string>} */
/** @type {PackageDependencyList} */
const packagesToAdd = {};
for (const [pkg, version] of testDependenciesEntries) {
packagesToAdd[pkg] = `${version} <= v < ${nextVersion(version, 'major')}`;
Expand Down
19 changes: 15 additions & 4 deletions lib/types/content.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -39,14 +39,25 @@ export type ApplicationElmJson = {
};

export type ApplicationDependencies = {
direct: DependencyList;
indirect: DependencyList;
direct: ApplicationDependencyList;
indirect: ApplicationDependencyList;
};

export type DependencyList = Record<string, VersionString>;
export type ApplicationDependencyList = Record<PackageName, VersionString>;
export type PackageDependencyList = Record<PackageName, VersionRange>;

export type PackageName = `${string}/${string}`;

export type PackageElmJson = {
type: 'package';
name: PackageName;
summary: string;
license: string;
version: VersionString;
'exposed-modules': (string | Record<string, string>)[];
'elm-version': VersionRange;
dependencies: PackageDependencyList;
'test-dependencies': PackageDependencyList;
};

export type SourceDirectories = Path[];
Expand Down
12 changes: 12 additions & 0 deletions lib/types/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @template T
* @template U
* @param {Record<U extends string ? U : never, T>} 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));
}

0 comments on commit 2278796

Please sign in to comment.