From 1232d1edfdb885f66db76a327ba9ea8d1b5ec54d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Sj=C3=B8gren?= Date: Fri, 5 Oct 2018 16:12:45 +0200 Subject: [PATCH] feat(tslib): add tslib helpers to global (#6351) * feat(tslib): add tslib helpers to global * Adds tslib as a dependency to tns-core-modules * Replaces globals/decorators with globals/tslib * Adds support for async/await, rest and spread operators. * refactor: rename tslib to ts-helpers to avoid confusion with npm package --- package.json | 2 +- tests/package.json | 3 +- tns-core-modules/globals/decorators.ts | 32 ------------------- tns-core-modules/globals/globals.ts | 15 +-------- tns-core-modules/globals/ts-helpers.ts | 30 +++++++++++++++++ tns-core-modules/inspector_modules.ios.ts | 2 +- tns-core-modules/package.json | 3 +- .../ui/styling/style-properties.ts | 4 +-- tns-platform-declarations/package.json | 2 +- 9 files changed, 40 insertions(+), 53 deletions(-) delete mode 100644 tns-core-modules/globals/decorators.ts create mode 100644 tns-core-modules/globals/ts-helpers.ts diff --git a/package.json b/package.json index 2be4142f6..00090b12b 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "shelljs": "^0.7.0", "source-map-support": "^0.4.17", "time-grunt": "1.3.0", - "tslib": "^1.7.1", + "tslib": "^1.9.3", "tslint": "^5.4.3", "typedoc": "^0.5.10", "typedoc-plugin-external-module-name": "git://github.com/PanayotCankov/typedoc-plugin-external-module-name.git#with-js", diff --git a/tests/package.json b/tests/package.json index 56a0aa160..b05b37039 100644 --- a/tests/package.json +++ b/tests/package.json @@ -13,7 +13,8 @@ } }, "dependencies": { - "tns-core-modules": "*" + "tns-core-modules": "*", + "tslib": "^1.9.3" }, "devDependencies": { "babel-traverse": "6.9.0", diff --git a/tns-core-modules/globals/decorators.ts b/tns-core-modules/globals/decorators.ts deleted file mode 100644 index 8541d28a3..000000000 --- a/tns-core-modules/globals/decorators.ts +++ /dev/null @@ -1,32 +0,0 @@ -if (typeof (global).__decorate !== "function") { - (global).__decorate = function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof (global).Reflect === "object" && typeof (global).Reflect.decorate === "function") { - r = (global).Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; - } - } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - } -} - -if (typeof (global).__metadata !== "function") { - (global).__metadata = function (k, v) { - if (typeof (global).Reflect === "object" && typeof (global).Reflect.metadata === "function") { - return (global).Reflect.metadata(k, v); - } - }; -} - -if (typeof (global).__param !== "function") { - (global).__param = (global && (global).__param) || function (paramIndex, decorator) { - return function (target, key) { decorator(target, key, paramIndex); } - }; -} diff --git a/tns-core-modules/globals/globals.ts b/tns-core-modules/globals/globals.ts index 15b61854e..d4b6fa6d5 100644 --- a/tns-core-modules/globals/globals.ts +++ b/tns-core-modules/globals/globals.ts @@ -1,18 +1,5 @@ // Required by TypeScript compiler -require("./decorators"); - -// Required by V8 snapshot generator -if (!global.__extends) { - global.__extends = function (d, b) { - for (var p in b) { - if (b.hasOwnProperty(p)) { - d[p] = b[p]; - } - } - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -} +require("./ts-helpers"); // This method iterates all the keys in the source exports object and copies them to the destination exports one. // Note: the method will not check for naming collisions and will override any already existing entries in the destination exports. diff --git a/tns-core-modules/globals/ts-helpers.ts b/tns-core-modules/globals/ts-helpers.ts new file mode 100644 index 000000000..bee3c471e --- /dev/null +++ b/tns-core-modules/globals/ts-helpers.ts @@ -0,0 +1,30 @@ +// Required by V8 snapshot generator +if (!global.__extends) { + global.__extends = function (d, b) { + for (var p in b) { + if (b.hasOwnProperty(p)) { + d[p] = b[p]; + } + } + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +} + +import * as tslib from "tslib"; + +// Bind the tslib helpers to global scope. +// This is needed when we don't use importHelpers, which +// breaks extending native-classes +for (const fnName of Object.keys(tslib)) { + if (typeof tslib[fnName] !== "function") { + continue; + } + + if (fnName in global) { + // Don't override globals that are already defined (ex. __extends) + continue; + } + + global[fnName] = tslib[fnName]; +} diff --git a/tns-core-modules/inspector_modules.ios.ts b/tns-core-modules/inspector_modules.ios.ts index 108b09e8e..6b61ed93e 100644 --- a/tns-core-modules/inspector_modules.ios.ts +++ b/tns-core-modules/inspector_modules.ios.ts @@ -1,5 +1,5 @@ console.log("Loading inspector modules..."); -require("./globals/decorators"); +require("./globals/ts-helpers"); require("./debugger/webinspector-network"); require("./debugger/webinspector-dom"); require("./debugger/webinspector-css"); diff --git a/tns-core-modules/package.json b/tns-core-modules/package.json index 2005b5cbc..edd88a3cd 100644 --- a/tns-core-modules/package.json +++ b/tns-core-modules/package.json @@ -26,7 +26,8 @@ "license": "Apache-2.0", "typings": "tns-core-modules.d.ts", "dependencies": { - "tns-core-modules-widgets": "next" + "tns-core-modules-widgets": "next", + "tslib": "^1.9.3" }, "devDependencies": { "@types/node": "~7.0.5", diff --git a/tns-core-modules/ui/styling/style-properties.ts b/tns-core-modules/ui/styling/style-properties.ts index 0b5d80bb7..185446827 100644 --- a/tns-core-modules/ui/styling/style-properties.ts +++ b/tns-core-modules/ui/styling/style-properties.ts @@ -495,7 +495,7 @@ export function transformConverter(text: string): TransformFunctionsInfo { const usedTransforms = transformations.map(t => t.property); if (!hasDuplicates(usedTransforms)) { - const fullTransformations = Object.assign({}, IDENTITY_TRANSFORMATION); + const fullTransformations = { ...IDENTITY_TRANSFORMATION }; transformations.forEach(transform => { fullTransformations[transform.property] = transform.value; }); @@ -1098,4 +1098,4 @@ export const visibilityProperty = new CssProperty({ target.view.isCollapsed = (newValue === Visibility.COLLAPSE); } }); -visibilityProperty.register(Style); \ No newline at end of file +visibilityProperty.register(Style); diff --git a/tns-platform-declarations/package.json b/tns-platform-declarations/package.json index b13c76a00..3971f8168 100644 --- a/tns-platform-declarations/package.json +++ b/tns-platform-declarations/package.json @@ -32,6 +32,6 @@ }, "homepage": "https://github.com/NativeScript/NativeScript#readme", "devDependencies": { - "typescript": "^2.6.1" + "typescript": "~2.6.1" } }