Skip to content

Commit

Permalink
Update tslib to support new __spreadArray helper (#133)
Browse files Browse the repository at this point in the history
* Update tslib to support new __spreadArray helper

* Ensure that the modules exports are validated in tests

* Adds spreadArray to the module exports

Co-authored-by: Orta <[email protected]>
  • Loading branch information
rbuckton and orta authored Dec 18, 2020
1 parent cff487d commit 88a77b8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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[];
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,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
Original file line number Diff line number Diff line change
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 @@ -186,12 +187,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 @@ -200,6 +203,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 @@ -282,6 +291,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

0 comments on commit 88a77b8

Please sign in to comment.