Skip to content

Commit f205f00

Browse files
committed
Polyfill: Remove dead code, convert guards to assertions
1 parent be4e0ef commit f205f00

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

polyfill/lib/calendar.mjs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import {
5555
} from './primordials.mjs';
5656

5757
import Call from 'es-abstract/2024/Call.js';
58-
import Type from 'es-abstract/2024/Type.js';
5958

6059
import { assert, assertNotReached } from './assert.mjs';
6160
import * as ES from './ecmascript.mjs';
@@ -611,10 +610,8 @@ function makeDayShiftedIsoToCalendarDate(cycleDays, cycleYears) {
611610
const approxDaysBeyond = MathAbs(isoDate.year - direction * 2000) * 365;
612611
let numCycles = MathMax(1, MathFloor(approxDaysBeyond / cycleDays));
613612
let safeIsoDate = ES.AddDaysToISODate(isoDate, -numCycles * cycleDays * direction);
614-
while (compareISODateToLegacyDateRange(safeIsoDate) !== 0) {
615-
numCycles++;
616-
safeIsoDate = ES.AddDaysToISODate(isoDate, -numCycles * cycleDays * direction);
617-
}
613+
/* c8 ignore next */
614+
assert(compareISODateToLegacyDateRange(safeIsoDate) === 0, 'numCycles calculation should be correct');
618615
const yearShift = numCycles * cycleYears * direction;
619616
const result = nonIsoHelperBase.isoToCalendarDate.call(this, safeIsoDate, cache);
620617
const adjusted = { ...result, year: result.year + yearShift };
@@ -780,11 +777,9 @@ const nonIsoHelperBase = {
780777
}
781778
if (this.checkIcuBugs) this.checkIcuBugs(isoDate);
782779
const calendarDate = this.adjustCalendarDate(result, cache, 'constrain', true);
783-
if (calendarDate.year === undefined) throw new RangeErrorCtor(`Missing year converting ${JSONStringify(isoDate)}`);
784-
if (calendarDate.month === undefined) {
785-
throw new RangeErrorCtor(`Missing month converting ${JSONStringify(isoDate)}`);
786-
}
787-
if (calendarDate.day === undefined) throw new RangeErrorCtor(`Missing day converting ${JSONStringify(isoDate)}`);
780+
/* c8 ignore next */ assert(calendarDate.year !== undefined, `Missing year converting ${JSONStringify(isoDate)}`);
781+
/* c8 ignore next */ assert(calendarDate.month !== undefined, `Missing month converting ${JSONStringify(isoDate)}`);
782+
/* c8 ignore next */ assert(calendarDate.day !== undefined, `Missing day converting ${JSONStringify(isoDate)}`);
788783
cache.set(key, calendarDate);
789784
// Also cache the reverse mapping
790785
const cacheReverse = (overflow) => {
@@ -803,11 +798,7 @@ const nonIsoHelperBase = {
803798
if (month === undefined && monthCode === undefined) throw new TypeErrorCtor('month or monthCode is required');
804799
if (day === undefined) throw new RangeErrorCtor('Missing day');
805800
if (monthCode !== undefined) {
806-
if (typeof monthCode !== 'string') {
807-
throw new RangeErrorCtor(
808-
`monthCode must be a string, not ${Call(StringPrototypeToLowerCase, Type(monthCode), [])}`
809-
);
810-
}
801+
/* c8 ignore next */ assert(typeof monthCode === 'string', `monthCode must be a string, not ${typeof monthCode}`);
811802
const { monthNumber } = ParseMonthCode(monthCode);
812803
if (monthNumber < 1 || monthNumber > 13) throw new RangeErrorCtor(`Invalid monthCode: ${monthCode}`);
813804
}
@@ -1032,15 +1023,15 @@ const nonIsoHelperBase = {
10321023
}
10331024
cache.set(key, isoEstimate);
10341025
if (keyOriginal) cache.set(keyOriginal, isoEstimate);
1035-
if (
1036-
date.year === undefined ||
1037-
date.month === undefined ||
1038-
date.day === undefined ||
1039-
date.monthCode === undefined ||
1040-
(CalendarSupportsEra(this.id) && (date.era === undefined || date.eraYear === undefined))
1041-
) {
1042-
throw new RangeErrorCtor('Unexpected missing property');
1043-
}
1026+
/* c8 ignore next 6 */
1027+
assert(
1028+
date.year !== undefined &&
1029+
date.month !== undefined &&
1030+
date.day !== undefined &&
1031+
date.monthCode !== undefined &&
1032+
(!CalendarSupportsEra(this.id) || (date.era !== undefined && date.eraYear !== undefined)),
1033+
'Unexpected missing property'
1034+
);
10441035
return isoEstimate;
10451036
},
10461037
compareCalendarDates(date1, date2) {
@@ -1915,24 +1906,10 @@ const helperBuddhist = ObjectAssign(
19151906
makeHelperSameMonthDayAsGregorian('buddhist', [{ code: 'be', isoEpoch: { year: -542, month: 1, day: 1 } }])
19161907
);
19171908

1918-
const helperGregory = ObjectAssign(
1919-
makeHelperSameMonthDayAsGregorian('gregory', [
1920-
{ code: 'ce', isoEpoch: { year: 1, month: 1, day: 1 } },
1921-
{ code: 'bce', reverseOf: 'ce' }
1922-
]),
1923-
{
1924-
reviseIntlEra(calendarDate /*, isoDate*/) {
1925-
let { era, eraYear } = calendarDate;
1926-
// Firefox 96 introduced a bug where the `'short'` format of the era
1927-
// option mistakenly returns the one-letter (narrow) format instead. The
1928-
// code below handles either the correct or Firefox-buggy format. See
1929-
// https://bugzilla.mozilla.org/show_bug.cgi?id=1752253
1930-
if (era === 'b') era = 'bce';
1931-
if (era === 'a') era = 'ce';
1932-
return { era, eraYear };
1933-
}
1934-
}
1935-
);
1909+
const helperGregory = makeHelperSameMonthDayAsGregorian('gregory', [
1910+
{ code: 'ce', isoEpoch: { year: 1, month: 1, day: 1 } },
1911+
{ code: 'bce', reverseOf: 'ce' }
1912+
]);
19361913

19371914
const helperJapanese = ObjectAssign(
19381915
// NOTE: Only the 5 modern eras (Meiji and later) are included. For dates

0 commit comments

Comments
 (0)