Skip to content

Commit

Permalink
feat: add additional array prototype methods (#4309)
Browse files Browse the repository at this point in the history
* feat: add additional array prototype methods

* add string prototype toString, toLocalString (not inherited from object)

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Dec 23, 2021
1 parent e17a379 commit 473568c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/ast/nodes/shared/ArrayPrototype.ts
Expand Up @@ -128,6 +128,8 @@ export const ARRAY_PROTOTYPE = new ObjectEntity(
filter: METHOD_CALLS_ARG_DEOPTS_SELF_RETURNS_NEW_ARRAY,
find: METHOD_CALLS_ARG_DEOPTS_SELF_RETURNS_UNKNOWN,
findIndex: METHOD_CALLS_ARG_DEOPTS_SELF_RETURNS_NUMBER,
flat: METHOD_DEOPTS_SELF_RETURNS_NEW_ARRAY,
flatMap: METHOD_CALLS_ARG_DEOPTS_SELF_RETURNS_NEW_ARRAY,
forEach: METHOD_CALLS_ARG_DEOPTS_SELF_RETURNS_UNKNOWN,
includes: METHOD_RETURNS_BOOLEAN,
indexOf: METHOD_RETURNS_NUMBER,
Expand All @@ -145,6 +147,8 @@ export const ARRAY_PROTOTYPE = new ObjectEntity(
some: METHOD_CALLS_ARG_DEOPTS_SELF_RETURNS_BOOLEAN,
sort: METHOD_CALLS_ARG_MUTATES_SELF_RETURNS_SELF,
splice: METHOD_MUTATES_SELF_RETURNS_NEW_ARRAY,
toLocaleString: METHOD_RETURNS_STRING,
toString: METHOD_RETURNS_STRING,
unshift: METHOD_MUTATES_SELF_RETURNS_NUMBER,
values: METHOD_DEOPTS_SELF_RETURNS_UNKNOWN
} as unknown as PropertyMap,
Expand Down
Expand Up @@ -39,6 +39,10 @@ _findArray[0].effect();
const _findIndexArray = [{ effect() {} }];
_findIndexArray.findIndex(element => (element.effect = () => console.log(1)));
_findIndexArray[0].effect();
[1].flatMap(() => console.log(1) || 1);
const _flatMapArray = [{ effect() {} }];
_flatMapArray.flatMap(element => (element.effect = () => console.log(1)));
_flatMapArray[0].effect();
[1].forEach(() => console.log(1) || true);
const _forEachArray = [{ effect() {} }];
_forEachArray.forEach(element => (element.effect = () => console.log(1)));
Expand Down
13 changes: 11 additions & 2 deletions test/form/samples/builtin-prototypes/array-expression/main.js
Expand Up @@ -37,6 +37,8 @@ const _entriesArray = [{ effect() {} }];
[..._entriesArray.entries()][0][1].effect = () => console.log(1);
_entriesArray[0].effect();

const _flat = [].flat().join(',').trim();
const _flat2 = [].flat(1).join(',').trim();
const _includes = [].includes(1).valueOf();
const _indexOf = [].indexOf(1).toPrecision(1);
const _join = [].join(',').trim();
Expand Down Expand Up @@ -78,6 +80,12 @@ const _findIndexArray = [{ effect() {} }];
_findIndexArray.findIndex(element => (element.effect = () => console.log(1)));
_findIndexArray[0].effect();

const _flatMap = [1].flatMap(() => 1).join(',');
const _flatMapEffect = [1].flatMap(() => console.log(1) || 1);
const _flatMapArray = [{ effect() {} }];
_flatMapArray.flatMap(element => (element.effect = () => console.log(1)));
_flatMapArray[0].effect();

const _forEach = [1].forEach(() => {});
const _forEachEffect = [1].forEach(() => console.log(1) || true);
const _forEachArray = [{ effect() {} }];
Expand Down Expand Up @@ -131,10 +139,11 @@ exported.splice(0);
const _unshift = [1].unshift(0).toPrecision(1);
exported.unshift(0);

const _toLocaleString = [1].toLocaleString().trim();
const _toString = [1].toString().trim();

// inherited
const _hasOwnProperty = [1].hasOwnProperty('toString').valueOf();
const _isPrototypeOf = [1].isPrototypeOf([]).valueOf();
const _propertyIsEnumerable = [1].propertyIsEnumerable('toString').valueOf();
const _toLocaleString = [1].toLocaleString().trim();
const _toString = [1].toString().trim();
const _valueOf = [1].valueOf();

0 comments on commit 473568c

Please sign in to comment.