Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional array prototype methods #4309

Merged
merged 5 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();