From fee7ab06c7e05c2db2bdd85353a0e2dce23b0f0b Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Sat, 25 Nov 2017 01:44:20 +0700 Subject: [PATCH] added sourcemaps support + sample library, disable `member-ordering` ts rule --- package.json | 4 +++ src/main.ts | 19 +++++++---- src/types.d.ts | 2 ++ src/utils/ErrorMapper.ts | 70 ++++++++++++++++++++++++++++++++++++++++ tslint.json | 1 + 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/types.d.ts create mode 100644 src/utils/ErrorMapper.ts diff --git a/package.json b/package.json index 0971aa6..f69d2c5 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "homepage": "https://github.com/screepers/screeps-typescript-starter#readme", "devDependencies": { "@types/lodash": "^4.14.85", + "@types/source-map": "^0.5.2", "rollup": "^0.51.8", "rollup-plugin-clean": "^1.0.0", "rollup-plugin-commonjs": "^8.2.6", @@ -27,5 +28,8 @@ "tslint": "^5.8.0", "typed-screeps": "^1.0.4", "typescript": "^2.6.1" + }, + "dependencies": { + "source-map": "^0.6.1" } } diff --git a/src/main.ts b/src/main.ts index 67efd57..0f3ce84 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,16 @@ +import { ErrorMapper } from "./utils/ErrorMapper"; + export function loop() { - - // Clear non-existing creep memory. - for(let name in Memory.creeps) { - if(!Game.creeps[name]) { - delete Memory[name]; + try { + // Clear non-existing creep memory. + for (const name in Memory.creeps) { + if (!Game.creeps[name]) { + delete Memory[name]; + } } - } - console.log(`Current tick is ${Game.time}`); + console.log(`Current tick is ${Game.time}`); + } catch (e) { + console.error(ErrorMapper.sourceMappedStackTrace(e)); + } } diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..4065722 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,2 @@ +// type shim for nodejs' `require()` syntax +declare const require: (module: string) => any; diff --git a/src/utils/ErrorMapper.ts b/src/utils/ErrorMapper.ts new file mode 100644 index 0000000..91475ab --- /dev/null +++ b/src/utils/ErrorMapper.ts @@ -0,0 +1,70 @@ +// tslint:disable:no-conditional-assignment +import {SourceMapConsumer} from "source-map"; + +export class ErrorMapper { + // Cache consumer + private static _consumer?: SourceMapConsumer; + + public static get consumer(): SourceMapConsumer { + if (this._consumer == null) { + this._consumer = new SourceMapConsumer(require("main.js.map")); + } + + return this._consumer; + } + + // Cache previously mapped traces to improve performance + public static cache: { [key: string]: string } = {}; + + /** + * Generates a stack trace using a source map generate original symbol names. + * + * WARNING - EXTREMELY high CPU cost for first call after reset - >30 CPU! Use sparingly! + * (Consecutive calls after a reset are more reasonable, ~0.1 CPU/ea) + * + * @param {Error | string} error The error or original stack trace + * @returns {string} The source-mapped stack trace + */ + public static sourceMappedStackTrace(error: Error | string): string { + const stack: string = error instanceof Error ? error.stack as string : error; + if (this.cache.hasOwnProperty(stack)) { + return this.cache[stack]; + } + + const re = /^\s+at\s+(.+?\s+)?\(?([0-z._\-\\\/]+):(\d+):(\d+)\)?$/gm; + let match: RegExpExecArray | null; + let outStack = error.toString(); + + while (match = re.exec(stack)) { + if (match[2] === "main") { + const pos = this.consumer.originalPositionFor({ + line: parseInt(match[3], 10), + column: parseInt(match[4], 10) + }); + + if (pos.line != null) { + if (pos.name) { + outStack += `\n at ${pos.name} (${pos.source}:${pos.line}:${pos.column})`; + } else { + if (match[1]) { + // no original source file name known - use file name from given trace + outStack += `\n at ${match[1]} (${pos.source}:${pos.line}:${pos.column})`; + } else { + // no original source file name known or in given trace - omit name + outStack += `\n at ${pos.source}:${pos.line}:${pos.column}`; + } + } + } else { + // no known position + break; + } + } else { + // no more parseable lines + break; + } + } + + this.cache[stack] = outStack; + return outStack; + } +} diff --git a/tslint.json b/tslint.json index 15d37c2..4f1b5bc 100644 --- a/tslint.json +++ b/tslint.json @@ -5,6 +5,7 @@ "rules": { "forin": false, "interface-name": [true, "never-prefix"], + "member-ordering": [false], "no-console": [false], "no-namespace": [true, "allow-declarations"], "trailing-comma": [true, {"multiline": "never", "singleline": "never"}],