Skip to content

Commit

Permalink
enable Debug in semver
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Nov 16, 2022
1 parent 48da966 commit eaf8e83
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/compiler/semver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { compareStringsCaseSensitive, compareValues, emptyArray, /* every, */ isArray, map, some, trimString } from "./core";
import { compareStringsCaseSensitive, compareValues, emptyArray, every, isArray, map, some, trimString } from "./core";
import { Comparison } from "./corePublic";
// import { Debug } from "./debug";
import { Debug } from "./debug";

// https://semver.org/#spec-item-2
// > A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative
Expand All @@ -17,14 +17,14 @@ const versionRegExp = /^(0|[1-9]\d*)(?:\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*)(?:\-([a-z
// > alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty. Numeric identifiers
// > MUST NOT include leading zeroes.
const prereleaseRegExp = /^(?:0|[1-9]\d*|[a-z-][a-z0-9-]*)(?:\.(?:0|[1-9]\d*|[a-z-][a-z0-9-]*))*$/i;
// const prereleasePartRegExp = /^(?:0|[1-9]\d*|[a-z-][a-z0-9-]*)$/i;
const prereleasePartRegExp = /^(?:0|[1-9]\d*|[a-z-][a-z0-9-]*)$/i;

// https://semver.org/#spec-item-10
// > Build metadata MAY be denoted by appending a plus sign and a series of dot separated
// > identifiers immediately following the patch or pre-release version. Identifiers MUST
// > comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty.
const buildRegExp = /^[a-z0-9-]+(?:\.[a-z0-9-]+)*$/i;
// const buildPartRegExp = /^[a-z0-9-]+$/i;
const buildPartRegExp = /^[a-z0-9-]+$/i;

// https://semver.org/#spec-item-9
// > Numeric identifiers MUST NOT include leading zeroes.
Expand All @@ -48,23 +48,19 @@ export class Version {
constructor(major: number, minor?: number, patch?: number, prerelease?: string | readonly string[], build?: string | readonly string[]);
constructor(major: number | string, minor = 0, patch = 0, prerelease: string | readonly string[] = "", build: string | readonly string[] = "") {
if (typeof major === "string") {
// const result = Debug.checkDefined(tryParseComponents(major), "Invalid version");
const result = tryParseComponents(major);
if (result === undefined) {
throw "Invalid version"; // eslint-disable-line no-throw-literal
}
const result = Debug.checkDefined(tryParseComponents(major), "Invalid version");
({ major, minor, patch, prerelease, build } = result);
}

// Debug.assert(major >= 0, "Invalid argument: major");
// Debug.assert(minor >= 0, "Invalid argument: minor");
// Debug.assert(patch >= 0, "Invalid argument: patch");
Debug.assert(major >= 0, "Invalid argument: major");
Debug.assert(minor >= 0, "Invalid argument: minor");
Debug.assert(patch >= 0, "Invalid argument: patch");

const prereleaseArray = prerelease ? isArray(prerelease) ? prerelease : prerelease.split(".") : emptyArray;
const buildArray = build ? isArray(build) ? build : build.split(".") : emptyArray;

// Debug.assert(every(prereleaseArray, s => prereleasePartRegExp.test(s)), "Invalid argument: prerelease");
// Debug.assert(every(buildArray, s => buildPartRegExp.test(s)), "Invalid argument: build");
Debug.assert(every(prereleaseArray, s => prereleasePartRegExp.test(s)), "Invalid argument: prerelease");
Debug.assert(every(buildArray, s => buildPartRegExp.test(s)), "Invalid argument: build");

this.major = major;
this.minor = minor;
Expand Down Expand Up @@ -107,8 +103,7 @@ export class Version {
case "major": return new Version(this.major + 1, 0, 0);
case "minor": return new Version(this.major, this.minor + 1, 0);
case "patch": return new Version(this.major, this.minor, this.patch + 1);
// default: return Debug.assertNever(field);
default: throw field;
default: return Debug.assertNever(field);
}
}

Expand Down Expand Up @@ -200,8 +195,7 @@ export class VersionRange {
private _alternatives: readonly (readonly Comparator[])[];

constructor(spec: string) {
// this._alternatives = spec ? Debug.checkDefined(parseRange(spec), "Invalid range spec.") : emptyArray;
this._alternatives = spec ? parseRange(spec) || emptyArray : emptyArray;
this._alternatives = spec ? Debug.checkDefined(parseRange(spec), "Invalid range spec.") : emptyArray;
}

static tryParse(text: string) {
Expand Down Expand Up @@ -410,8 +404,7 @@ function testComparator(version: Version, operator: Comparator["operator"], oper
case ">": return cmp > 0;
case ">=": return cmp >= 0;
case "=": return cmp === 0;
// default: return Debug.assertNever(operator);
default: throw operator;
default: return Debug.assertNever(operator);
}
}

Expand Down

0 comments on commit eaf8e83

Please sign in to comment.