Skip to content

Commit

Permalink
chore: better version types
Browse files Browse the repository at this point in the history
A tuple could reduce overhead, but string'll do for now.
  • Loading branch information
lishaduck committed Jun 26, 2024
1 parent a346703 commit e122b3e
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 68 deletions.
6 changes: 3 additions & 3 deletions lib/autofix.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const exit = require('../vendor/exit');
/**
* @import {App,AutofixRequest} from "./types/app"
* @import {File} from "./types/content"
* @import {ElmVersion} from "./types/elm-version"
* @import {VersionString} from "./types/version"
* @import {Options} from "./types/options"
* @import {Path} from "./types/path"
* @import {FilesProposedByCurrentFix} from "./types/state"
Expand All @@ -31,7 +31,7 @@ module.exports = {
*
* @param {Options} options
* @param {App} app
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
*/
function subscribe(options, app, elmVersion) {
AppState.subscribe(
Expand All @@ -45,7 +45,7 @@ function subscribe(options, app, elmVersion) {
*
* @param {Options} options
* @param {App} app
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
* @returns {(data: AutofixRequest) => Promise<void>}
*/
function askConfirmationToFixWithOptions(options, app, elmVersion) {
Expand Down
6 changes: 3 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const exit = require('../vendor/exit');
/**
* @import {AppHash, BuildResult} from "./types/build"
* @import {ApplicationElmJson} from "./types/content"
* @import {ElmVersion} from "./types/elm-version"
* @import {VersionString} from "./types/version"
* @import {Options, Template} from "./types/options"
* @import {Path} from "./types/path"
*/
Expand Down Expand Up @@ -600,8 +600,8 @@ async function buildElmParser(options, reviewElmJson) {
*
* @param {Options} options
* @param {Path} buildFolder
* @param {ElmVersion} elmVersion
* @param {ElmVersion} elmSyntaxVersion
* @param {VersionString} elmVersion
* @param {VersionString} elmSyntaxVersion
* @returns {Promise<void>}
*/
async function createParserElmJsonFile(
Expand Down
21 changes: 11 additions & 10 deletions lib/dependency-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ProjectJsonFiles = require('./project-json-files');
/**
* @import {Options} from "./types/options"
* @import {Path} from "./types/path"
* @import {VersionString} from "./types/version"
*/

/** @type {boolean} */
Expand All @@ -33,7 +34,7 @@ class DependencyProvider {
* Solve dependencies completely offline, without any http request.
*
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} elmJson
* @param {Record<string, string>} extra
* @returns {string}
Expand All @@ -58,7 +59,7 @@ class DependencyProvider {
* Solve dependencies with http requests when required.
*
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} elmJson
* @param {Record<string, string>} extra
* @returns {string}
Expand Down Expand Up @@ -104,7 +105,7 @@ class OfflineAvailableVersionLister {
cache = new Map();

/**
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} pkg
* @returns {string[]}
*/
Expand All @@ -130,7 +131,7 @@ class OnlineVersionsCache {

/**
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @returns {void}
*/
update(options, elmVersion) {
Expand Down Expand Up @@ -243,15 +244,15 @@ class OnlineAvailableVersionLister {
/**
* @param {Options} options
* @param {OnlineVersionsCache} onlineCache
* @param {string} elmVersion
* @param {VersionString} elmVersion
*/
constructor(options, onlineCache, elmVersion) {
onlineCache.update(options, elmVersion);
this.onlineCache = onlineCache;
}

/**
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} pkg
* @returns {string[]}
*/
Expand All @@ -275,7 +276,7 @@ class OnlineAvailableVersionLister {
}

/**
* @param {string} elmVersion
* @param {import('./types/version').VersionString} elmVersion
* @param {string} pkg
* @returns {string[]}
*/
Expand All @@ -297,7 +298,7 @@ function readVersionsInElmHomeAndSort(elmVersion, pkg) {
* Solve dependencies completely offline, without any http request.
*
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} pkg
* @param {string} version
* @returns {string}
Expand Down Expand Up @@ -332,7 +333,7 @@ function fetchElmJsonOnline(options, elmVersion, pkg, version) {

/**
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} pkg
* @param {string} version
* @returns {string}
Expand Down Expand Up @@ -466,7 +467,7 @@ function parseVersions(key, json) {
* Cache in which we'll store information related Elm dependencies, computed by elm-solve-deps-wasm.
*
* @param {string} packageJsonVersion
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @returns {Path}
*/
function dependenciesCachePath(packageJsonVersion, elmVersion) {
Expand Down
9 changes: 5 additions & 4 deletions lib/elm-binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ErrorMessage = require('./error-message');
/**
* @import {Options} from "./types/options"
* @import {Path} from "./types/path"
* @import {VersionString} from "./types/version"
*/

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ A few options:
* Get the version of the Elm compiler
*
* @param {Path} elmBinary
* @returns {string}
* @returns {VersionString}
*/
function getElmVersion(elmBinary) {
const result = spawn.sync(elmBinary, ['--version'], {
Expand Down Expand Up @@ -93,15 +94,15 @@ function downloadDependenciesOfElmJson(elmBinary, elmJsonPath) {

/**
* @param {string} version
* @returns {string}
* @returns {VersionString}
*/
function trimVersion(version) {
const index = version.indexOf('-');
if (index === -1) {
return version.trim();
return /** @type {VersionString} */ (version.trim());
}

return version.slice(0, index).trim();
return /** @type {VersionString} */ (version.slice(0, index).trim());
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/min-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ErrorMessage = require('./error-message');
/**
* @import {Options} from "./types/options"
* @import {Path} from "./types/path"
* @import {VersionString} from "./types/min-version"
* @import {VersionString} from "./types/version"
*/

const minimalVersion = {major: 2, minor: 14};
Expand Down
14 changes: 7 additions & 7 deletions lib/project-json-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const FS = require('./fs-wrapper');

/**
* @import {Path} from "./types/path"
* @import {ElmVersion} from "./types/elm-version"
* @import {VersionString} from "./types/version"
* @import {Options} from "./types/options"
* @import {PackageElmJson} from "./types/content"
*/
Expand All @@ -21,7 +21,7 @@ const elmRoot =
* Get the elm.json file for a dependency.
*
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} name
* @param {string} packageVersion
* @returns {Promise<PackageElmJson>}
Expand Down Expand Up @@ -67,7 +67,7 @@ async function getElmJson(options, elmVersion, name, packageVersion) {
const elmJsonInElmHomePromises = new Map();

/**
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
* @param {string} name
* @param {string} packageVersion
* @returns {Promise<PackageElmJson>}
Expand All @@ -92,7 +92,7 @@ function getElmJsonFromElmHome(elmVersion, name, packageVersion) {
}

/**
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
* @param {string} name
* @param {string} packageVersion
* @returns {Path}
Expand All @@ -106,7 +106,7 @@ function getElmJsonFromElmHomePath(elmVersion, name, packageVersion) {
}

/**
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
* @param {string} name
* @returns {Path}
*/
Expand All @@ -118,7 +118,7 @@ function getPackagePathInElmHome(elmVersion, name) {
* Get the docs.json file for a dependency.
*
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} name
* @param {string} packageVersion
* @returns {Promise<unknown>}
Expand Down Expand Up @@ -203,7 +203,7 @@ async function cachePackage(cacheLocation, json) {
* Get the path to where a project file would be cached.
*
* @param {Options} options
* @param {string} elmVersion
* @param {VersionString} elmVersion
* @param {string} name
* @param {string} packageVersion
* @param {string} file
Expand Down
71 changes: 40 additions & 31 deletions lib/template-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const parseElmFolder = path.join(__dirname, '../parseElm');

/**
* @import {Options} from "./types/options"
* @import {ElmVersion} from "./types/elm-version"
* @import {ApplicationElmJson, ApplicationDependencies} from "./types/content"
* @import {VersionString} from "./types/version"
* @import {ApplicationElmJson, ApplicationDependencies, DependencyList} from "./types/content"
*/

module.exports = {
Expand Down Expand Up @@ -68,8 +68,8 @@ ${error.toString().replace(/^Error: /, '')}`,
* Compute the dependencies if we were to replace the version of `stil4m/elm-syntax` with the given one.
*
* @param {Options} options
* @param {ElmVersion} elmVersion
* @param {ElmVersion} elmSyntaxVersion
* @param {VersionString} elmVersion
* @param {VersionString} elmSyntaxVersion
* @returns {Promise<ApplicationElmJson>}
*/
async function addElmSyntax(options, elmVersion, elmSyntaxVersion) {
Expand Down Expand Up @@ -101,8 +101,9 @@ https://github.com/jfmengels/node-elm-review/issues/new
elmVersion,
JSON.stringify(elmJson),
{
'stil4m/elm-syntax': `${elmSyntaxVersion} <= v < ${nextPatchVersion(
elmSyntaxVersion
'stil4m/elm-syntax': `${elmSyntaxVersion} <= v < ${nextVersion(
elmSyntaxVersion,
'patch'
)}`
},
false
Expand All @@ -112,7 +113,7 @@ https://github.com/jfmengels/node-elm-review/issues/new

/**
* @param {Options} options
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
* @param {string} elmJson
* @param {Record<string, string>}extra
* @param {boolean} onlineFirst
Expand Down Expand Up @@ -143,30 +144,38 @@ function solve(options, elmVersion, elmJson, extra, onlineFirst) {
}

/**
* @param {ElmVersion} version
* @returns {ElmVersion} the next major version.
* Ex: 2.13.0 -> 3.0.0
* @param {VersionString} version
* @param {'major' | 'minor' | 'patch'} bump
* @returns {VersionString} the next version.
* Ex: 2.13.0 -> 3.0.0 (major)
* Ex: 2.13.0 -> 2.13.1 (patch)
*/
function nextMajorVersion(version) {
const [major] = version.split('.');
return `${Number.parseInt(major, 10) + 1}.0.0`;
}

/**
* @param {ElmVersion} version
* @returns {ElmVersion} the next patch version.
* Ex: 2.13.0 -> 2.13.1
*/
function nextPatchVersion(version) {
function nextVersion(version, bump) {
const [major, minor, patch] = version.split('.');
return `${major}.${minor}.${Number.parseInt(patch, 10) + 1}`;

const majorNum = Number.parseInt(major, 10);
if (bump === 'major') {
return `${majorNum + 1}.0.0`;
}

const minorNum = Number.parseInt(minor, 10);
if (bump === 'minor') {
return `${majorNum}.${minorNum + 1}.0`;
}

const patchNum = Number.parseInt(patch, 10);
if (bump === 'patch') {
return `${majorNum}.${minorNum}.${patchNum + 1}`;
}

throw new TypeError("TS doesn't infer this, strangely.");
}

/**
* Create a new elm.json with basic `elm-review` dependencies.
*
* @param {Options} options
* @param {ElmVersion} elmVersion
* @param {VersionString} elmVersion
* @returns {ApplicationElmJson}
*/
function createNewReviewElmJson(options, elmVersion) {
Expand Down Expand Up @@ -222,9 +231,9 @@ function createNewReviewElmJson(options, elmVersion) {
/**
* Filter out test-dependencies that are already in the regular dependencies.
*
* @param {{direct: Record<string, string>, indirect: Record<string, string>}} testDependencies
* @param {{direct: Record<string, string>, indirect: Record<string, string>}} regularDependencies
* @returns {{direct: Record<string, string>, indirect: Record<string, string>}}
* @param {ApplicationDependencies} testDependencies
* @param {ApplicationDependencies} regularDependencies
* @returns {ApplicationDependencies}
*/
function filterOutDuplicateDependencies(testDependencies, regularDependencies) {
return {
Expand All @@ -242,9 +251,9 @@ function filterOutDuplicateDependencies(testDependencies, regularDependencies) {
/**
* Filter out test-dependencies that are already in the regular dependencies (only on a section of the dependencies).
*
* @param {Record<string, string>} testDependencies
* @param {Record<string, string>} regularDependencies
* @returns {Record<string, string>}
* @param {DependencyList} testDependencies
* @param {DependencyList} regularDependencies
* @returns {DependencyList}
*/
function filterOutDuplicateDependenciesForSection(
testDependencies,
Expand All @@ -271,7 +280,7 @@ function update(options, elmJson) {
};

for (const [pkg, version] of Object.entries(elmJson.dependencies.direct)) {
extra[pkg] = `${version} <= v < ${nextMajorVersion(version)}`;
extra[pkg] = `${version} <= v < ${nextVersion(version, 'major')}`;
}

delete elmJson.dependencies.direct['jfmengels/elm-review'];
Expand All @@ -298,7 +307,7 @@ function update(options, elmJson) {
/** @type {Record<string, string>} */
const packagesToAdd = {};
for (const [pkg, version] of testDependenciesEntries) {
packagesToAdd[pkg] = `${version} <= v < ${nextMajorVersion(version)}`;
packagesToAdd[pkg] = `${version} <= v < ${nextVersion(version, 'major')}`;
}

const testDependencies = solve(
Expand Down
Loading

0 comments on commit e122b3e

Please sign in to comment.