Skip to content

Commit 86481cc

Browse files
committed
Make typings compatible with @types/node.
Fixes name clashes and uses Node-compatible typings where possible. Changes: - setTimout et al now return NodeJS.Timer instead of number - No "console" module anymore. Everyone uses it through global.console anyway. - We have a typed "global" instance with exposed properties now. Any "freeform" accesses must go through a `(<any>global).blah` cast. - remove tns-core-modules.{base,es6,es2015}.d.ts. Those were needed as workarounds for the ES6/DOM/Node type clashes.
1 parent 3056ce5 commit 86481cc

25 files changed

+238
-307
lines changed

apps/app/css-perf-test/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as application from "application";
22
declare var CACurrentMediaTime;
33

4-
global.time = function(): number {
4+
(<any>global).time = function(): number {
55
if (global.android) {
66
return java.lang.System.nanoTime() / 1000000; // 1 ms = 1000000 ns
77
}
@@ -10,4 +10,4 @@ global.time = function(): number {
1010
}
1111
}
1212

13-
application.start({ moduleName: "css-perf-test/root" });
13+
application.start({ moduleName: "css-perf-test/root" });

apps/app/css-perf-test/main-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
export function navigatedTo(args: ObservableEventData) {
44
setTimeout(() => {
5-
console.log(`Time: ${global.time() - global.startTime} ms`);
5+
console.log(`Time: ${(<any>global).time() - (<any>global).startTime} ms`);
66
});
7-
}
7+
}

apps/app/css-perf-test/root.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Page} from "ui/page";
22

33
export function onTap(args: any) {
4-
global.startTime = global.time();
4+
(<any>global).startTime = (<any>global).time();
55
let page = <Page>args.object.page;
66
page.frame.navigate("css-perf-test/main-page");
7-
}
7+
}

gruntfile.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,9 @@ module.exports = function(grunt) {
8484
});
8585
var combinedDtsPath = path.join(outDir, outFile);
8686
grunt.file.write(combinedDtsPath, dtsLines.join('\n'));
87-
}
88-
87+
};
88+
8989
var generateModulesDts = function generateModulesDts(outDir, srcDir) {
90-
var angularConflicts = ['module.d.ts'];
91-
var angularExcludes = angularConflicts.map(function(file) {
92-
return '!' + file;
93-
});
9490
var dtsFiles = grunt.file.expand({cwd: srcDir }, [
9591
"**/*.d.ts",
9692
//Exclude the d.ts files in the apps folder - these are part of the apps and are already packed there!
@@ -99,24 +95,14 @@ module.exports = function(grunt) {
9995
"!android17.d.ts",
10096
"!**/*.android.d.ts",
10197
"!ios/**",
102-
"!lib.core.d.ts",
103-
"!lib.dom.d.ts",
10498
"!**/*.ios.d.ts",
10599
"!tns-core-modules.d.ts",
106-
"!tns-core-modules.es6.d.ts",
107-
"!tns-core-modules.es2016.d.ts",
108-
"!tns-core-modules.base.d.ts",
109100
"!references.d.ts",
110-
"!webworker.es2016.d.ts"
111-
].concat(localCfg.defaultExcludes).concat(angularExcludes));
101+
].concat(localCfg.defaultExcludes));
112102
dtsFiles.sort();
113103

114-
writeDtsFile(dtsFiles, outDir, 'tns-core-modules/tns-core-modules.base.d.ts');
115-
var es6Files = angularConflicts.concat(['tns-core-modules.base.d.ts']);
116-
writeDtsFile(es6Files, outDir, 'tns-core-modules/tns-core-modules.es6.d.ts');
117-
var allFiles = angularConflicts.concat(['tns-core-modules.base.d.ts']);
118-
writeDtsFile(allFiles, outDir, 'tns-core-modules/tns-core-modules.d.ts');
119-
}
104+
writeDtsFile(dtsFiles, outDir, "tns-core-modules/tns-core-modules.d.ts");
105+
};
120106

121107
// Configure localCfg
122108
var outDir = tsconfig.compilerOptions.outDir || "./bin/dist";
@@ -411,9 +397,9 @@ module.exports = function(grunt) {
411397
});
412398

413399
grunt.registerTask("generate-tns-core-modules-dev-dts", generateModulesDts.bind(null, ".", localCfg.srcTnsCoreModules));
414-
400+
415401
grunt.registerTask("generate-tns-core-modules-dts", generateModulesDts.bind(null, localCfg.outDir, localCfg.outTnsCoreModules));
416-
402+
417403
//aliasing pack-modules for backwards compatibility
418404
grunt.registerTask("pack-modules", [
419405
"compile-modules",

tns-core-modules/application/application-common.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require("globals");
2-
import * as definition from "application";
32
import * as observable from "data/observable";
43
// TODO: Raise event on("livesync") and attach this handler in the ui/frame module.
54
import { NavigationEntry, reloadPage } from "ui/frame";
@@ -62,7 +61,7 @@ export function setResources(res: any) {
6261
resources = res;
6362
}
6463

65-
export var onUncaughtError: (error: definition.NativeScriptError) => void = undefined;
64+
export var onUncaughtError: (error: NativeScriptError) => void = undefined;
6665

6766
export var onLaunch: (context: any) => any = undefined;
6867

@@ -118,9 +117,9 @@ export function parseCss(cssText: string, cssFileName?: string): RuleSet[] {
118117

119118
export function __onLiveSync() {
120119
// Close the error page if available and remove the reference from global context.
121-
if (global.errorPage) {
122-
global.errorPage.closeModal();
123-
global.errorPage = undefined;
120+
if ((<any>global).errorPage) {
121+
(<any>global).errorPage.closeModal();
122+
(<any>global).errorPage = undefined;
124123
}
125124

126125
try {
@@ -132,26 +131,26 @@ export function __onLiveSync() {
132131
// Reload app.css in case it was changed.
133132
loadCss();
134133

135-
global.__onLiveSyncCore();
134+
(<any>global).__onLiveSyncCore();
136135

137136
} catch (ex) {
138137
// Show the error as modal page, save reference to the page in global context.
139138
ensureBuilder();
140-
global.errorPage = builder.parse(`<Page><ScrollView><Label text="${ex}" textWrap="true" style="color: red;" /></ScrollView></Page>`);
141-
global.errorPage.showModal();
139+
(<any>global).errorPage = builder.parse(`<Page><ScrollView><Label text="${ex}" textWrap="true" style="color: red;" /></ScrollView></Page>`);
140+
(<any>global).errorPage.showModal();
142141
}
143142
}
144143

145144
export function __onLiveSyncCore() {
146145
// Reload current page.
147146
reloadPage();
148147
}
149-
global.__onLiveSyncCore = __onLiveSyncCore;
148+
(<any>global).__onLiveSyncCore = __onLiveSyncCore;
150149

151150
export function _onOrientationChanged(){
152151
ensurePlatform();
153152
platform.screen.mainScreen._invalidate();
154153

155154
ensureFileNameResolver();
156155
fileNameResolver._invalidateResolverInstance();
157-
}
156+
}

tns-core-modules/application/application.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ global.__onLiveSync = function () {
334334
loadCss();
335335
};
336336

337-
global.__onUncaughtError = function (error: definition.NativeScriptError) {
337+
global.__onUncaughtError = function (error: NativeScriptError) {
338338
const types: typeof typesModule = require("utils/types");
339339

340340
// TODO: Obsolete this

tns-core-modules/application/application.d.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ declare module "application" {
55
import { RuleSet } from "ui/styling/css-selector";
66
import { NavigationEntry, View, Observable } from "ui/frame";
77

8-
/**
9-
* An extended JavaScript Error which will have the nativeError property initialized in case the error is caused by executing platform-specific code.
10-
*/
11-
export interface NativeScriptError extends Error {
12-
/**
13-
* Represents the native error object.
14-
*/
15-
nativeError: any;
16-
}
17-
188
/**
199
* String value used when hooking to launch event.
2010
*/

tns-core-modules/application/application.ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class IOSApplication implements definition.iOSApplication {
215215
var iosApp = new IOSApplication();
216216
typedExports.ios = iosApp;
217217

218-
global.__onUncaughtError = function (error: definition.NativeScriptError) {
218+
global.__onUncaughtError = function (error: NativeScriptError) {
219219
var types: typeof typesModule = require("utils/types");
220220

221221
// TODO: This should be obsoleted

tns-core-modules/console/console.d.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

tns-core-modules/console/console.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import * as definition from "console";
21
import * as trace from "trace";
32
import * as platform from "platform";
43

5-
export class Console implements definition.Console {
4+
function __message(message: any, level: string) {
5+
if ((<any>global).__consoleMessage) {
6+
(<any>global).__consoleMessage(message, level);
7+
}
8+
}
9+
10+
export class Console {
611
private TAG: string = "JS";
712
private _timers: any;
813
private _stripFirstTwoLinesRegEx: RegExp;
@@ -250,9 +255,7 @@ export class Console implements definition.Console {
250255
Array.prototype.shift.apply(arguments);
251256
let formatedMessage = this.formatParams.apply(this, arguments);
252257
this.error(formatedMessage, trace.messageType.error);
253-
if (global.__consoleMessage) {
254-
global.__consoleMessage(formatedMessage, "error");
255-
}
258+
__message(formatedMessage, "error");
256259
}
257260
}
258261

@@ -263,29 +266,23 @@ export class Console implements definition.Console {
263266
public warn(message: any, ...formatParams: any[]): void {
264267
let formatedMessage = this.formatParams.apply(this, arguments);
265268
this.logMessage(formatedMessage, trace.messageType.warn);
266-
if (global.__consoleMessage) {
267-
global.__consoleMessage(formatedMessage, "warning");
268-
}
269+
__message(formatedMessage, "warning");
269270
}
270271

271272
public error(message: any, ...formatParams: any[]): void {
272273
let formatedMessage = this.formatParams.apply(this, arguments);
273274
this.logMessage(formatedMessage, trace.messageType.error);
274-
if (global.__consoleMessage) {
275-
global.__consoleMessage(formatedMessage, "error")
276-
}
275+
__message(formatedMessage, "error");
277276
}
278277

279278
public log(message: any, ...formatParams: any[]): void {
280279
let formatedMessage = this.formatParams.apply(this, arguments);
281280
this.logMessage(formatedMessage, trace.messageType.log);
282-
if (global.__consoleMessage) {
283-
global.__consoleMessage(formatedMessage, "log")
284-
}
281+
__message(formatedMessage, "log");
285282
}
286283

287284
private logMessage(message: string, messageType: number): void {
288-
if (!global.android) {
285+
if (!(<any>global).android) {
289286
// This case may be entered during heap snapshot where the global.android is not present
290287
return;
291288
}

0 commit comments

Comments
 (0)