Skip to content

Commit ad7e2e3

Browse files
authored
Remove Call, GetMethod, HasOwnProperty (from es-abstract) usage. (#52)
1 parent f885253 commit ad7e2e3

File tree

4 files changed

+49
-62
lines changed

4 files changed

+49
-62
lines changed

lib/calendar.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const MathFloor = Math.floor;
3131
const ObjectAssign = Object.assign;
3232
const ObjectEntries = Object.entries;
3333
const ObjectKeys = Object.keys;
34+
const ReflectApply = Reflect.apply;
3435

3536
const impl = {};
3637

@@ -1565,7 +1566,8 @@ const makeHelperGregorian = (id, originalEras) => {
15651566
if (month === undefined) calendarDate = { ...calendarDate, month: monthCodeNumberPart(monthCode) };
15661567
this.validateCalendarDate(calendarDate);
15671568
calendarDate = this.completeEraYear(calendarDate);
1568-
calendarDate = ES.Call(nonIsoHelperBase.adjustCalendarDate, this, [calendarDate, cache, overflow]);
1569+
// TODO this can become `super` later.
1570+
calendarDate = ReflectApply(nonIsoHelperBase.adjustCalendarDate, this, [calendarDate, cache, overflow]);
15691571
return calendarDate;
15701572
},
15711573
estimateIsoDate(this: typeof helperGregorian & NonIsoHelperBase, calendarDate) {

lib/ecmascript.ts

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ const ObjectAssign = Object.assign;
1313
const ObjectCreate = Object.create;
1414
const ObjectDefineProperty = Object.defineProperty;
1515
const ObjectIs = Object.is;
16+
const ReflectApply = Reflect.apply;
1617

1718
import { DEBUG } from './debug';
1819
import bigInt from 'big-integer';
19-
import Call from 'es-abstract/2020/Call.js';
20-
import GetMethod from 'es-abstract/2020/GetMethod.js';
2120
import IsInteger from 'es-abstract/2020/IsInteger.js';
2221
import ToInteger from 'es-abstract/2020/ToInteger.js';
2322
import ToLength from 'es-abstract/2020/ToLength.js';
2423
import ToNumber from 'es-abstract/2020/ToNumber.js';
2524
import ToPrimitive from 'es-abstract/2020/ToPrimitive.js';
2625
import ToString from 'es-abstract/2020/ToString.js';
2726
import Type from 'es-abstract/2020/Type.js';
28-
import HasOwnProperty from 'es-abstract/2020/HasOwnProperty.js';
2927

3028
import { GetIntrinsic } from './intrinsicclass';
3129
import {
@@ -154,9 +152,6 @@ const SINGULAR_PLURAL_UNITS = [
154152
import * as PARSE from './regex';
155153

156154
const ES2020 = {
157-
Call,
158-
GetMethod,
159-
HasOwnProperty,
160155
IsInteger,
161156
ToInteger,
162157
ToLength,
@@ -1522,8 +1517,9 @@ export const ES = ObjectAssign({}, ES2020, {
15221517
return new TemporalCalendar('iso8601');
15231518
},
15241519
CalendarFields: (calendar, fieldNames) => {
1525-
const fields = ES.GetMethod(calendar, 'fields');
1526-
if (fields !== undefined) fieldNames = ES.Call(fields, calendar, [fieldNames]);
1520+
if (calendar.fields) {
1521+
fieldNames = calendar.fields(fieldNames);
1522+
}
15271523
const result = [];
15281524
for (const name of fieldNames) {
15291525
if (ES.Type(name) !== 'String') throw new TypeError('bad return from calendar.fields()');
@@ -1532,107 +1528,95 @@ export const ES = ObjectAssign({}, ES2020, {
15321528
return result;
15331529
},
15341530
CalendarMergeFields: (calendar, fields, additionalFields) => {
1535-
const mergeFields = ES.GetMethod(calendar, 'mergeFields');
1536-
if (mergeFields === undefined) return { ...fields, ...additionalFields };
1537-
const result = ES.Call(mergeFields, calendar, [fields, additionalFields]);
1531+
const calMergeFields = calendar.mergeFields;
1532+
if (!calMergeFields) {
1533+
return { ...fields, ...additionalFields };
1534+
}
1535+
const result = Reflect.apply(calMergeFields, calendar, [fields, additionalFields]);
15381536
if (ES.Type(result) !== 'Object') throw new TypeError('bad return from calendar.mergeFields()');
15391537
return result;
15401538
},
15411539
CalendarDateAdd: (calendar, date, duration, options, dateAdd) => {
15421540
if (dateAdd === undefined) {
1543-
dateAdd = ES.GetMethod(calendar, 'dateAdd');
1541+
dateAdd = calendar.dateAdd;
15441542
}
1545-
const result = ES.Call(dateAdd, calendar, [date, duration, options]);
1543+
const result = ReflectApply(dateAdd, calendar, [date, duration, options]);
15461544
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
15471545
return result;
15481546
},
15491547
CalendarDateUntil: (calendar, date, otherDate, options, dateUntil) => {
15501548
if (dateUntil === undefined) {
1551-
dateUntil = ES.GetMethod(calendar, 'dateUntil');
1549+
dateUntil = calendar.dateUntil;
15521550
}
1553-
const result = ES.Call(dateUntil, calendar, [date, otherDate, options]);
1551+
const result = ReflectApply(dateUntil, calendar, [date, otherDate, options]);
15541552
if (!ES.IsTemporalDuration(result)) throw new TypeError('invalid result');
15551553
return result;
15561554
},
15571555
CalendarYear: (calendar, dateLike) => {
1558-
const year = ES.GetMethod(calendar, 'year');
1559-
const result = ES.Call(year, calendar, [dateLike]);
1556+
const result = calendar.year(dateLike);
15601557
if (result === undefined) {
15611558
throw new RangeError('calendar year result must be an integer');
15621559
}
15631560
return ES.ToIntegerThrowOnInfinity(result);
15641561
},
15651562
CalendarMonth: (calendar, dateLike) => {
1566-
const month = ES.GetMethod(calendar, 'month');
1567-
const result = ES.Call(month, calendar, [dateLike]);
1563+
const result = calendar.month(dateLike);
15681564
if (result === undefined) {
15691565
throw new RangeError('calendar month result must be a positive integer');
15701566
}
15711567
return ES.ToPositiveInteger(result);
15721568
},
15731569
CalendarMonthCode: (calendar, dateLike) => {
1574-
const monthCode = ES.GetMethod(calendar, 'monthCode');
1575-
const result = ES.Call(monthCode, calendar, [dateLike]);
1570+
const result = calendar.monthCode(dateLike);
15761571
if (result === undefined) {
15771572
throw new RangeError('calendar monthCode result must be a string');
15781573
}
15791574
return ES.ToString(result);
15801575
},
15811576
CalendarDay: (calendar, dateLike) => {
1582-
const day = ES.GetMethod(calendar, 'day');
1583-
const result = ES.Call(day, calendar, [dateLike]);
1577+
const result = calendar.day(dateLike);
15841578
if (result === undefined) {
15851579
throw new RangeError('calendar day result must be a positive integer');
15861580
}
15871581
return ES.ToPositiveInteger(result);
15881582
},
15891583
CalendarEra: (calendar, dateLike) => {
1590-
const era = ES.GetMethod(calendar, 'era');
1591-
let result = ES.Call(era, calendar, [dateLike]);
1584+
let result = calendar.era(dateLike);
15921585
if (result !== undefined) {
15931586
result = ES.ToString(result);
15941587
}
15951588
return result;
15961589
},
15971590
CalendarEraYear: (calendar, dateLike) => {
1598-
const eraYear = ES.GetMethod(calendar, 'eraYear');
1599-
let result = ES.Call(eraYear, calendar, [dateLike]);
1591+
let result = calendar.eraYear(dateLike);
16001592
if (result !== undefined) {
16011593
result = ES.ToIntegerThrowOnInfinity(result);
16021594
}
16031595
return result;
16041596
},
16051597
CalendarDayOfWeek: (calendar, dateLike) => {
1606-
const dayOfWeek = ES.GetMethod(calendar, 'dayOfWeek');
1607-
return ES.Call(dayOfWeek, calendar, [dateLike]);
1598+
return calendar.dayOfWeek(dateLike);
16081599
},
16091600
CalendarDayOfYear: (calendar, dateLike) => {
1610-
const dayOfYear = ES.GetMethod(calendar, 'dayOfYear');
1611-
return ES.Call(dayOfYear, calendar, [dateLike]);
1601+
return calendar.dayOfYear(dateLike);
16121602
},
16131603
CalendarWeekOfYear: (calendar, dateLike) => {
1614-
const weekOfYear = ES.GetMethod(calendar, 'weekOfYear');
1615-
return ES.Call(weekOfYear, calendar, [dateLike]);
1604+
return calendar.weekOfYear(dateLike);
16161605
},
16171606
CalendarDaysInWeek: (calendar, dateLike) => {
1618-
const daysInWeek = ES.GetMethod(calendar, 'daysInWeek');
1619-
return ES.Call(daysInWeek, calendar, [dateLike]);
1607+
return calendar.daysInWeek(dateLike);
16201608
},
16211609
CalendarDaysInMonth: (calendar, dateLike) => {
1622-
const daysInMonth = ES.GetMethod(calendar, 'daysInMonth');
1623-
return ES.Call(daysInMonth, calendar, [dateLike]);
1610+
return calendar.daysInMonth(dateLike);
16241611
},
16251612
CalendarDaysInYear: (calendar, dateLike) => {
1626-
const daysInYear = ES.GetMethod(calendar, 'daysInYear');
1627-
return ES.Call(daysInYear, calendar, [dateLike]);
1613+
return calendar.daysInYear(dateLike);
16281614
},
16291615
CalendarMonthsInYear: (calendar, dateLike) => {
1630-
const monthsInYear = ES.GetMethod(calendar, 'monthsInYear');
1631-
return ES.Call(monthsInYear, calendar, [dateLike]);
1616+
return calendar.monthsInYear(dateLike);
16321617
},
16331618
CalendarInLeapYear: (calendar, dateLike) => {
1634-
const inLeapYear = ES.GetMethod(calendar, 'inLeapYear');
1635-
return ES.Call(inLeapYear, calendar, [dateLike]);
1619+
return calendar.inLeapYear(dateLike);
16361620
},
16371621

16381622
ToTemporalCalendar: (calendarLike) => {
@@ -1684,20 +1668,17 @@ export const ES = ObjectAssign({}, ES2020, {
16841668
}
16851669
},
16861670
DateFromFields: (calendar, fields, options) => {
1687-
const dateFromFields = ES.GetMethod(calendar, 'dateFromFields');
1688-
const result = ES.Call(dateFromFields, calendar, [fields, options]);
1671+
const result = calendar.dateFromFields(fields, options);
16891672
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
16901673
return result;
16911674
},
16921675
YearMonthFromFields: (calendar, fields, options) => {
1693-
const yearMonthFromFields = ES.GetMethod(calendar, 'yearMonthFromFields');
1694-
const result = ES.Call(yearMonthFromFields, calendar, [fields, options]);
1676+
const result = calendar.yearMonthFromFields(fields, options);
16951677
if (!ES.IsTemporalYearMonth(result)) throw new TypeError('invalid result');
16961678
return result;
16971679
},
16981680
MonthDayFromFields: (calendar, fields, options) => {
1699-
const monthDayFromFields = ES.GetMethod(calendar, 'monthDayFromFields');
1700-
const result = ES.Call(monthDayFromFields, calendar, [fields, options]);
1681+
const result = calendar.monthDayFromFields(fields, options);
17011682
if (!ES.IsTemporalMonthDay(result)) throw new TypeError('invalid result');
17021683
return result;
17031684
},
@@ -1742,11 +1723,11 @@ export const ES = ObjectAssign({}, ES2020, {
17421723
);
17431724
},
17441725
GetOffsetNanosecondsFor: (timeZone, instant) => {
1745-
let getOffsetNanosecondsFor = ES.GetMethod(timeZone, 'getOffsetNanosecondsFor');
1726+
let getOffsetNanosecondsFor = timeZone.getOffsetNanosecondsFor;
17461727
if (getOffsetNanosecondsFor === undefined) {
17471728
getOffsetNanosecondsFor = GetIntrinsic('%Temporal.TimeZone.prototype.getOffsetNanosecondsFor%');
17481729
}
1749-
const offsetNs = ES.Call(getOffsetNanosecondsFor, timeZone, [instant]);
1730+
const offsetNs = Reflect.apply(getOffsetNanosecondsFor, timeZone, [instant]);
17501731
if (typeof offsetNs !== 'number') {
17511732
throw new TypeError('bad return from getOffsetNanosecondsFor');
17521733
}
@@ -1916,8 +1897,7 @@ export const ES = ObjectAssign({}, ES2020, {
19161897
}
19171898
},
19181899
GetPossibleInstantsFor: (timeZone, dateTime) => {
1919-
const getPossibleInstantsFor = ES.GetMethod(timeZone, 'getPossibleInstantsFor');
1920-
const possibleInstants = ES.Call(getPossibleInstantsFor, timeZone, [dateTime]);
1900+
const possibleInstants = timeZone.getPossibleInstantsFor(dateTime);
19211901
const result = [];
19221902
for (const instant of possibleInstants) {
19231903
if (!ES.IsTemporalInstant(instant)) {
@@ -2698,8 +2678,8 @@ export const ES = ObjectAssign({}, ES2020, {
26982678
{
26992679
if (!calendar) throw new RangeError('a starting point is required for months balancing');
27002680
// balance years down to months
2701-
const dateAdd = ES.GetMethod(calendar, 'dateAdd');
2702-
const dateUntil = ES.GetMethod(calendar, 'dateUntil');
2681+
const dateAdd = calendar.dateAdd;
2682+
const dateUntil = calendar.dateUntil;
27032683
while (MathAbs(years) > 0) {
27042684
const addOptions = ObjectCreate(null);
27052685
const newRelativeTo = ES.CalendarDateAdd(calendar, relativeTo, oneYear, addOptions, dateAdd);
@@ -2802,10 +2782,10 @@ export const ES = ObjectAssign({}, ES2020, {
28022782
}
28032783

28042784
// balance months up to years
2805-
const dateAdd = ES.GetMethod(calendar, 'dateAdd');
2785+
const dateAdd = calendar.dateAdd;
28062786
const addOptions = ObjectCreate(null);
28072787
newRelativeTo = ES.CalendarDateAdd(calendar, relativeTo, oneYear, addOptions, dateAdd);
2808-
const dateUntil = ES.GetMethod(calendar, 'dateUntil');
2788+
const dateUntil = calendar.dateUntil;
28092789
const untilOptions = ObjectCreate(null);
28102790
untilOptions.largestUnit = 'month';
28112791
let untilResult = ES.CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil);
@@ -3387,7 +3367,7 @@ export const ES = ObjectAssign({}, ES2020, {
33873367
);
33883368
const dateDuration1 = new TemporalDuration(y1, mon1, w1, d1, 0, 0, 0, 0, 0, 0);
33893369
const dateDuration2 = new TemporalDuration(y2, mon2, w2, d2, 0, 0, 0, 0, 0, 0);
3390-
const dateAdd = ES.GetMethod(calendar, 'dateAdd');
3370+
const dateAdd = calendar.dateAdd;
33913371
const firstAddOptions = ObjectCreate(null);
33923372
const intermediate = ES.CalendarDateAdd(calendar, datePart, dateDuration1, firstAddOptions, dateAdd);
33933373
const secondAddOptions = ObjectCreate(null);
@@ -3928,7 +3908,7 @@ export const ES = ObjectAssign({}, ES2020, {
39283908
// convert months and weeks to days by calculating difference(
39293909
// relativeTo + years, relativeTo + { years, months, weeks })
39303910
const yearsDuration = new TemporalDuration(years);
3931-
const dateAdd = ES.GetMethod(calendar, 'dateAdd');
3911+
const dateAdd = calendar.dateAdd;
39323912
const firstAddOptions = ObjectCreate(null);
39333913
const yearsLater = ES.CalendarDateAdd(calendar, relativeTo, yearsDuration, firstAddOptions, dateAdd);
39343914
const yearsMonthsWeeks = new TemporalDuration(years, months, weeks);
@@ -3979,7 +3959,7 @@ export const ES = ObjectAssign({}, ES2020, {
39793959
// convert weeks to days by calculating difference(relativeTo +
39803960
// { years, months }, relativeTo + { years, months, weeks })
39813961
const yearsMonths = new TemporalDuration(years, months);
3982-
const dateAdd = ES.GetMethod(calendar, 'dateAdd');
3962+
const dateAdd = calendar.dateAdd;
39833963
const firstAddOptions = ObjectCreate(null);
39843964
const yearsMonthsLater = ES.CalendarDateAdd(calendar, relativeTo, yearsMonths, firstAddOptions, dateAdd);
39853965
const yearsMonthsWeeks = new TemporalDuration(years, months, weeks);

lib/intl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const descriptor = (value) => {
4242

4343
const IntlDateTimeFormat = globalThis.Intl.DateTimeFormat;
4444
const ObjectAssign = Object.assign;
45+
const ObjectHasOwnProperty = Object.prototype.hasOwnProperty;
46+
const ReflectApply = Reflect.apply;
4547

4648
// Construction of built-in Intl.DateTimeFormat objects is sloooooow,
4749
// so we'll only create those instances when we need them.
@@ -90,7 +92,9 @@ export function DateTimeFormat(
9092
if (hasOptions) {
9193
const clonedResolved = ObjectAssign({}, ro);
9294
for (const prop in clonedResolved) {
93-
if (!ES.HasOwnProperty(options, prop)) delete clonedResolved[prop];
95+
if (!ReflectApply(ObjectHasOwnProperty, options, [prop])) {
96+
delete clonedResolved[prop];
97+
}
9498
}
9599
this[OPTIONS] = clonedResolved;
96100
} else {

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"es2015.collection",
2626
"es2015.symbol.wellknown",
2727
"es2015.core",
28+
"ES2015.Reflect",
2829
"ES2016.Array.Include",
2930
"ES2017.Intl",
3031
"ES2017.Object",

0 commit comments

Comments
 (0)