Skip to content

Commit ef8c588

Browse files
committed
Fix bad code ordering in adjustCalendarDate
Re-order code to guarantee that `calendarDate.year` is set before calculating the number of months in that year. The old code would break for any calendar that a) used a constant `era` like `islamic` and b) had a variable number of months in each year like `hebrew`. Today no ICU calendars fit in that Venn diagram, but if any are added in the future this code will break when initializing any calendar using an eraYear/era pair in a property bag. Better to fix it now!
1 parent 08e84c9 commit ef8c588

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/calendar.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,18 +821,22 @@ const nonIsoHelperBase: NonIsoHelperBase = {
821821
if (this.calendarType === 'lunisolar') throw new RangeError('Override required for lunisolar calendars');
822822
let calendarDate = calendarDateParam;
823823
this.validateCalendarDate(calendarDate);
824-
const largestMonth = this.monthsInYear(calendarDate, cache);
825-
let { month, year, eraYear, monthCode } = calendarDate;
826-
827824
// For calendars that always use the same era, set it here so that derived
828825
// calendars won't need to implement this method simply to set the era.
829826
if (this.constantEra) {
830827
// year and eraYear always match when there's only one possible era
831-
if (year === undefined) year = eraYear;
832-
if (eraYear === undefined) eraYear = year;
833-
calendarDate = { ...calendarDate, era: this.constantEra, year, eraYear };
828+
const { year, eraYear } = calendarDate;
829+
calendarDate = {
830+
...calendarDate,
831+
era: this.constantEra,
832+
year: year !== undefined ? year : eraYear,
833+
eraYear: eraYear !== undefined ? eraYear : year
834+
};
834835
}
835836

837+
const largestMonth = this.monthsInYear(calendarDate, cache);
838+
let { month, monthCode } = calendarDate;
839+
836840
({ month, monthCode } = resolveNonLunisolarMonth(calendarDate, overflow, largestMonth));
837841
return { ...calendarDate, month, monthCode };
838842
},

0 commit comments

Comments
 (0)