vmware / build-tools-for-vmware-aria

Build Tools for VMware Aria provides development and release management tools for implementing automation solutions based on the VMware Aria Suite and VMware Cloud Director. The solution enables Virtual Infrastructure Administrators and Automation Developers to use standard DevOps practices for managing and deploying content.
Other
48 stars 24 forks source link

shim for Array.from() doesn't create copy without mapfn #192

Closed pe1pip closed 1 year ago

pe1pip commented 1 year ago

Description

Array.from() is supposed to create a (shallow) copy, the shim doesn't do that if no map function is provided. This is an issue if the caller expects the original array to be unmodified if elements are pushed to the copy.

Steps to Reproduce

Preconditions: [What are the preconditions to reproduce the issue]

Expected behavior: [What you expect to happen]

A fix could be:

    static arrayFrom(arrayLike: ArrayLike<any>, mapfn?: (v: any, k: number) => any): any[] {
        let array: <any[]>

        if (mapfn) {
            array = arrayLike.map(mapfn);
        }  else {
            array = arrayLike.map(e => e);
                }

        return array;
    }

Actual behavior: [What actually happens]

    static arrayFrom(arrayLike: ArrayLike<any>, mapfn?: (v: any, k: number) => any): any[] {
        let array = <any[]>arrayLike;

        if (mapfn) {
            array = array.map(mapfn);
        }

        return array;
    }

Reproduces how often: [What percentage of the time does it reproduce]

100%

Component/s: [What are the Build Tools for VMware Aria components affected by the issue (e.g. "common/artifact-manager", "maven/plugins/vra-ng", "typescript/vrotest", etc)]

packages/ecmascript/src/Shims.ts

Affects Build/s: [Which are the Build Tools for VMware Aria releases / builds affected by the issue]

All

Environment

Client

Server

Failure Logs

Related issues and PRs

Additional Context

VenelinBakalov commented 1 year ago

I found additional issues - implementation does not work properly when providing a mapping function and calling Array.from() for string, Map and Set. String is returned as string instead of array, the other 2 throw error because they cannot find map.