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

Update tslib to support new __spreadArray helper #133

Merged
merged 3 commits into from Dec 18, 2020
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
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Expand Up @@ -21,3 +21,6 @@ jobs:
- name: Run tests
run: node ./test/runTests.js

- name: Run tests
run: node ./test/validateModuleExportsMatchCommonJS/index.js
if: matrix.node-version == '14.x'
2 changes: 2 additions & 0 deletions modules/index.js
Expand Up @@ -14,6 +14,7 @@ const {
__read,
__spread,
__spreadArrays,
__spreadArray,
__await,
__asyncGenerator,
__asyncDelegator,
Expand All @@ -39,6 +40,7 @@ export {
__read,
__spread,
__spreadArrays,
__spreadArray,
__await,
__asyncGenerator,
__asyncDelegator,
Expand Down
8 changes: 1 addition & 7 deletions test/validateModuleExportsMatchCommonJS/index.js
@@ -1,12 +1,6 @@
// When on node 14, it validates that all of the commonjs exports
// This file can only run on node 14+, it validates that all of the commonjs exports
// are correctly re-exported for es modules importers.

const nodeMajor = Number(process.version.split(".")[0].slice(1))
if (nodeMajor < 14) {
console.log("Skipping because node does not support module exports.")
process.exit(0)
}

// ES Modules import via the ./modules folder
import * as esTSLib from "../../modules/index.js"

Expand Down
3 changes: 3 additions & 0 deletions tslib.d.ts
Expand Up @@ -23,8 +23,11 @@ export declare function __generator(thisArg: any, body: Function): any;
export declare function __exportStar(m: any, o: any): void;
export declare function __values(o: any): any;
export declare function __read(o: any, n?: number): any[];
/** @deprecated since TypeScript 4.2 */
export declare function __spread(...args: any[][]): any[];
/** @deprecated since TypeScript 4.2 */
export declare function __spreadArrays(...args: any[][]): any[];
export declare function __spreadArray(to: any[], from: any[]): any[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more correct:

Suggested change
export declare function __spreadArray(to: any[], from: any[]): any[];
export declare function __spreadArray<T, U>(to: T[], from: ReadonlyArray<U>): Array<T | U>;

Although this would be even more correct, it depends on TypeScript 4.0:

export declare function __spreadArray<T extends any[], U extends readonly any[]>(to: T, from: U): [...T, ...U];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types here are fairly simplistic. If we were going to add generics here we would want to add them to other functions as well, but that's out of scope for now.

export declare function __await(v: any): any;
export declare function __asyncGenerator(thisArg: any, _arguments: any, generator: Function): any;
export declare function __asyncDelegator(o: any): any;
Expand Down
10 changes: 9 additions & 1 deletion tslib.es6.js
Expand Up @@ -144,19 +144,27 @@ export function __read(o, n) {
return ar;
}

/** @deprecated */
export function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}

/** @deprecated */
export function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
}

export function __spreadArray(to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
}

export function __await(v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
Expand Down
10 changes: 10 additions & 0 deletions tslib.js
Expand Up @@ -26,6 +26,7 @@ var __values;
var __read;
var __spread;
var __spreadArrays;
var __spreadArray;
var __await;
var __asyncGenerator;
var __asyncDelegator;
Expand Down Expand Up @@ -184,12 +185,14 @@ var __createBinding;
return ar;
};

/** @deprecated */
__spread = function () {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
};

/** @deprecated */
__spreadArrays = function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
Expand All @@ -198,6 +201,12 @@ var __createBinding;
return r;
};

__spreadArray = function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};

__await = function (v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
};
Expand Down Expand Up @@ -280,6 +289,7 @@ var __createBinding;
exporter("__read", __read);
exporter("__spread", __spread);
exporter("__spreadArrays", __spreadArrays);
exporter("__spreadArray", __spreadArray);
exporter("__await", __await);
exporter("__asyncGenerator", __asyncGenerator);
exporter("__asyncDelegator", __asyncDelegator);
Expand Down