Skip to content

JSDoc spread-annotations messes with code generation #7749

Closed

Description

TypeScript Version:

1.8.7

Code

Transpiling _apply.js of lodash-es@4.6.1 with the command

tsc --allowJs _apply.js --out apply.js -m amd

breaks the code. Apparently, the (arguably invalid) @param {...*} args JSDoc comment causes the TypeScript compiler to generate code as if the parameter declaration was apply(func, thisArg, ...args), although it is not.

/**
 * A faster alternative to `Function#apply`, this function invokes `func`
 * with the `this` binding of `thisArg` and the arguments of `args`.
 *
 * @private
 * @param {Function} func The function to invoke.
 * @param {*} thisArg The `this` binding of `func`.
 * @param {...*} args The arguments to invoke `func` with.
 * @returns {*} Returns the result of `func`.
 */
function apply(func, thisArg, args) {
  var length = args.length;
  switch (length) {
    case 0: return func.call(thisArg);
    case 1: return func.call(thisArg, args[0]);
    case 2: return func.call(thisArg, args[0], args[1]);
    case 3: return func.call(thisArg, args[0], args[1], args[2]);
  }
  return func.apply(thisArg, args);
}

export default apply;

Expected behavior:

define("_apply", ["require", "exports"], function (require, exports) {
    "use strict";
    /**
     * A faster alternative to `Function#apply`, this function invokes `func`
     * with the `this` binding of `thisArg` and the arguments of `args`.
     *
     * @private
     * @param {Function} func The function to invoke.
     * @param {*} thisArg The `this` binding of `func`.
     * @param {...*} args The arguments to invoke `func` with.
     * @returns {*} Returns the result of `func`.
     */
    function apply(func, thisArg, args) {
        var length = args.length;
        switch (length) {
            case 0: return func.call(thisArg);
            case 1: return func.call(thisArg, args[0]);
            case 2: return func.call(thisArg, args[0], args[1]);
            case 3: return func.call(thisArg, args[0], args[1], args[2]);
        }
        return func.apply(thisArg, args);
    }
    exports.__esModule = true;
    exports["default"] = apply;
});

Actual behavior:

define("_apply", ["require", "exports"], function (require, exports) {
    "use strict";
    /**
     * A faster alternative to `Function#apply`, this function invokes `func`
     * with the `this` binding of `thisArg` and the arguments of `args`.
     *
     * @private
     * @param {Function} func The function to invoke.
     * @param {*} thisArg The `this` binding of `func`.
     * @param {...*} args The arguments to invoke `func` with.
     * @returns {*} Returns the result of `func`.
     */
    function apply(func, thisArg) {
        var args = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            args[_i - 2] = arguments[_i];
        }
        var length = args.length;
        switch (length) {
            case 0: return func.call(thisArg);
            case 1: return func.call(thisArg, args[0]);
            case 2: return func.call(thisArg, args[0], args[1]);
            case 3: return func.call(thisArg, args[0], args[1], args[2]);
        }
        return func.apply(thisArg, args);
    }
    exports.__esModule = true;
    exports["default"] = apply;
});

I've not been able to reproduce this in the TypeScript playground (currently running 1.8.2), which suggests a regression somewhere between 1.8.2 and 1.8.7.

Activity

  1. sheetalkamat commented on Apr 8, 2016

    @sheetalkamat
    Member

    Oskar Segersvärd (@Victorystick)

    Your annotation is for variable number of arguments:

        /**
         * A faster alternative to `Function#apply`, this function invokes `func`
         * with the `this` binding of `thisArg` and the arguments of `args`.
         *
         * @private
         * @param {Function} func The function to invoke.
         * @param {*} thisArg The `this` binding of `func`.
         * @param {...*} args The arguments to invoke `func` with.
         * @returns {*} Returns the result of `func`.
         */

    May be using this instead is what you want to make args as array?:

        /**
         * A faster alternative to `Function#apply`, this function invokes `func`
         * with the `this` binding of `thisArg` and the arguments of `args`.
         *
         * @private
         * @param {Function} func The function to invoke.
         * @param {*} thisArg The `this` binding of `func`.
         * @param {Array<*>} args The arguments to invoke `func` with.
         * @returns {*} Returns the result of `func`.
         */
  2. mhegazy commented on Apr 19, 2016

    @mhegazy
    Contributor

    Sheetal Nandi (@sheetalkamat), we should not use the JSDoc comments for emit transformations. this is not what the user expects. the work around is fine, but we need to fix this for the compiler.

  3. sheetalkamat commented on Apr 19, 2016

    @sheetalkamat
    Member

    Mohamed Hegazy (@mhegazy), noted, will work on the fix.

  4. added
    Fixed
    on Apr 20, 2016
  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFixed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions