Comparing version 0.3.0 to 0.4.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const source_map_support_1 = require("source-map-support"); | ||
var source_map_support_1 = require("source-map-support"); | ||
source_map_support_1.install(); | ||
const path = require("path"); | ||
let projectRoot; | ||
var path = require("path"); | ||
var projectRoot; | ||
/** | ||
@@ -19,3 +19,3 @@ * The user may change to expected root of the project | ||
function setProjectRoot(root) { | ||
const prev = projectRoot; | ||
var prev = projectRoot; | ||
projectRoot = root; | ||
@@ -36,7 +36,8 @@ return prev; | ||
*/ | ||
function here(callDepth = 0) { | ||
function here(callDepth) { | ||
if (callDepth === void 0) { callDepth = 0; } | ||
return new TraceLoc(callDepth + 1); | ||
} | ||
exports.here = here; | ||
class TraceLoc { | ||
var TraceLoc = (function () { | ||
/** | ||
@@ -51,3 +52,4 @@ * Mark the "current" location. | ||
*/ | ||
constructor(callDepth = 0) { | ||
function TraceLoc(callDepth) { | ||
if (callDepth === void 0) { callDepth = 0; } | ||
this._func = ""; | ||
@@ -57,5 +59,5 @@ this._file = ""; | ||
this._col = -1; | ||
const saveStackTraceLimit = Error.stackTraceLimit; | ||
var saveStackTraceLimit = Error.stackTraceLimit; | ||
Error.stackTraceLimit = callDepth + 1; | ||
const err = new Error(); | ||
var err = new Error(); | ||
Error.captureStackTrace(err, this.constructor); | ||
@@ -67,31 +69,47 @@ Error.stackTraceLimit = saveStackTraceLimit; | ||
} | ||
get func() { | ||
if (!this._func) { | ||
this.updateLocation(); | ||
} | ||
return this._func; | ||
} | ||
get file() { | ||
if (!this._file) { | ||
this.updateLocation(); | ||
} | ||
return this._file; | ||
} | ||
get line() { | ||
if (this._line < 0) { | ||
this.updateLocation(); | ||
} | ||
return this._line; | ||
} | ||
get col() { | ||
if (this._col < 0) { | ||
this.updateLocation(); | ||
} | ||
return this._col; | ||
} | ||
toString() { | ||
return `${this.func} ${this.file}:${this.line}:${this.col}`; | ||
} | ||
getRelativeFileName(prjRoot, fileName) { | ||
let relative; | ||
Object.defineProperty(TraceLoc.prototype, "func", { | ||
get: function () { | ||
if (!this._func) { | ||
this.updateLocation(); | ||
} | ||
return this._func; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(TraceLoc.prototype, "file", { | ||
get: function () { | ||
if (!this._file) { | ||
this.updateLocation(); | ||
} | ||
return this._file; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(TraceLoc.prototype, "line", { | ||
get: function () { | ||
if (this._line < 0) { | ||
this.updateLocation(); | ||
} | ||
return this._line; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(TraceLoc.prototype, "col", { | ||
get: function () { | ||
if (this._col < 0) { | ||
this.updateLocation(); | ||
} | ||
return this._col; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
TraceLoc.prototype.toString = function () { | ||
return this.func + " " + this.file + ":" + this.line + ":" + this.col; | ||
}; | ||
TraceLoc.prototype.getRelativeFileName = function (prjRoot, fileName) { | ||
var relative; | ||
// If nothing the use __dirname | ||
@@ -109,3 +127,3 @@ if (prjRoot === undefined || prjRoot === null) { | ||
return relative; | ||
} | ||
}; | ||
/** | ||
@@ -115,6 +133,6 @@ * Update the location info | ||
*/ | ||
updateLocation() { | ||
TraceLoc.prototype.updateLocation = function () { | ||
// log(`getLocation: ${this.stackState}`); | ||
if (this.stackState) { | ||
const stack = this.stackState.split("\n"); | ||
var stack = this.stackState.split("\n"); | ||
if (stack.length >= 2) { | ||
@@ -124,4 +142,4 @@ // log(`getLocation: stack[tos]=${stack[stack.length - 1]}`); | ||
// location string is of the form; " at func (file:line:col)" | ||
let r = /.*? at (.*?) \((.*?):(\d+):(\d+)\)/.exec(`${stack[stack.length - 1]}`); | ||
let relative; | ||
var r = /.*? at (.*?) \((.*?):(\d+):(\d+)\)/.exec("" + stack[stack.length - 1]); | ||
var relative = void 0; | ||
if (r && r.length > 4) { | ||
@@ -138,3 +156,3 @@ relative = this.getRelativeFileName(projectRoot, r[2]); | ||
// string has no func and is of the form; " at file:line:col" | ||
r = /.*? at *(.*?):(\d+):(\d+)/.exec(`${stack[stack.length - 1]}`); | ||
r = /.*? at *(.*?):(\d+):(\d+)/.exec("" + stack[stack.length - 1]); | ||
if (r && r.length > 3) { | ||
@@ -153,5 +171,6 @@ relative = this.getRelativeFileName(projectRoot, r[1]); | ||
return this; | ||
} | ||
} | ||
}; | ||
return TraceLoc; | ||
}()); | ||
exports.TraceLoc = TraceLoc; | ||
//# sourceMappingURL=traceloc.js.map |
@@ -9,2 +9,5 @@ export declare class TracingTests { | ||
normalHere(): void; | ||
testMyHere(): void; | ||
testNestingHereBeyond1(): void; | ||
testCustomHere(): void; | ||
anon(): void; | ||
@@ -11,0 +14,0 @@ testSameLineHere(): void; |
@@ -12,10 +12,25 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const alsatian_1 = require("alsatian"); | ||
const traceloc_1 = require("../out/traceloc"); | ||
const os = require("os"); | ||
const path = require("path"); | ||
class TracingTests { | ||
testSetProjectRoot() { | ||
let loc; | ||
let orgVal = traceloc_1.setProjectRoot("weirdValue"); | ||
var alsatian_1 = require("alsatian"); | ||
var traceloc_1 = require("../out/traceloc"); | ||
var os = require("os"); | ||
var path = require("path"); | ||
function myHere() { | ||
return traceloc_1.here(1); | ||
} | ||
function nestHere2() { | ||
return traceloc_1.here(2); | ||
} | ||
function nestHere1() { | ||
return nestHere2(); | ||
} | ||
function customHere() { | ||
var loc = traceloc_1.here(1); | ||
return "customHere: " + loc.func + ":" + loc.line; | ||
} | ||
var TracingTests = (function () { | ||
function TracingTests() { | ||
} | ||
TracingTests.prototype.testSetProjectRoot = function () { | ||
var loc; | ||
var orgVal = traceloc_1.setProjectRoot("weirdValue"); | ||
alsatian_1.Expect(traceloc_1.setProjectRoot(orgVal)).toBe("weirdValue"); | ||
@@ -52,10 +67,10 @@ alsatian_1.Expect(traceloc_1.setProjectRoot(orgVal)).toBe(orgVal); | ||
traceloc_1.setProjectRoot(orgVal); | ||
} | ||
testTracing() { | ||
const loc = traceloc_1.here(); | ||
}; | ||
TracingTests.prototype.testTracing = function () { | ||
var loc = traceloc_1.here(); | ||
alsatian_1.Expect(loc.toString()) | ||
.toBe(`${loc.func} ${loc.file}:${loc.line}:${loc.col}`); | ||
} | ||
testTraceLocEmptyStack() { | ||
const loc = new traceloc_1.TraceLoc(); | ||
.toBe(loc.func + " " + loc.file + ":" + loc.line + ":" + loc.col); | ||
}; | ||
TracingTests.prototype.testTraceLocEmptyStack = function () { | ||
var loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.SpyOnProperty(loc, "stackState").andReturnValue(""); | ||
@@ -66,5 +81,5 @@ alsatian_1.Expect(loc.file).not.toBeTruthy(); | ||
alsatian_1.Expect(loc.col).toBeLessThan(0); | ||
} | ||
testTraceLocShortStack() { | ||
const loc = new traceloc_1.TraceLoc(); | ||
}; | ||
TracingTests.prototype.testTraceLocShortStack = function () { | ||
var loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.SpyOnProperty(loc, "stackState").andReturnValue("Error"); | ||
@@ -75,5 +90,5 @@ alsatian_1.Expect(loc.file).not.toBeTruthy(); | ||
alsatian_1.Expect(loc.col).toBeLessThan(0); | ||
} | ||
testTraceLocNotEnoughFieldsInStack() { | ||
const loc = new traceloc_1.TraceLoc(); | ||
}; | ||
TracingTests.prototype.testTraceLocNotEnoughFieldsInStack = function () { | ||
var loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.SpyOnProperty(loc, "stackState") | ||
@@ -85,5 +100,5 @@ .andReturnValue("Error\n at aFunc (/xyz/file.xx:12)"); | ||
alsatian_1.Expect(loc.col).toBeLessThan(0); | ||
} | ||
normal() { | ||
const loc = new traceloc_1.TraceLoc(); | ||
}; | ||
TracingTests.prototype.normal = function () { | ||
var loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.Expect(loc.func).toBe("TracingTests.normal"); | ||
@@ -93,5 +108,5 @@ alsatian_1.Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
alsatian_1.Expect(loc.col).toBeGreaterThan(0); | ||
} | ||
normalHere() { | ||
const loc = traceloc_1.here(); | ||
}; | ||
TracingTests.prototype.normalHere = function () { | ||
var loc = traceloc_1.here(); | ||
alsatian_1.Expect(loc.func).toBe("TracingTests.normalHere"); | ||
@@ -101,6 +116,27 @@ alsatian_1.Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
alsatian_1.Expect(loc.col).toBeGreaterThan(0); | ||
} | ||
anon() { | ||
}; | ||
TracingTests.prototype.testMyHere = function () { | ||
var locMyHere = myHere(); | ||
var locHere = traceloc_1.here(); | ||
alsatian_1.Expect(locMyHere.func).toBe("TracingTests.testMyHere"); | ||
alsatian_1.Expect(locMyHere.file).toBe("src/traceloc.spec.ts"); | ||
alsatian_1.Expect(locMyHere.line).toBe(locHere.line - 1); | ||
alsatian_1.Expect(locMyHere.col).toBe(locHere.col + 2); | ||
}; | ||
TracingTests.prototype.testNestingHereBeyond1 = function () { | ||
var locNestHere1 = nestHere1(); | ||
var locHere = traceloc_1.here(); | ||
alsatian_1.Expect(locNestHere1.func).toBe("TracingTests.testNestingHereBeyond1"); | ||
alsatian_1.Expect(locNestHere1.file).toBe("src/traceloc.spec.ts"); | ||
alsatian_1.Expect(locNestHere1.line).toBe(locHere.line - 1); | ||
alsatian_1.Expect(locNestHere1.col).toBe(locHere.col); | ||
}; | ||
TracingTests.prototype.testCustomHere = function () { | ||
var locCustomHere = customHere(); | ||
var locHere = traceloc_1.here(); | ||
alsatian_1.Expect(locCustomHere).toBe("customHere: " + locHere.func + ":" + (locHere.line - 1)); | ||
}; | ||
TracingTests.prototype.anon = function () { | ||
(function () { | ||
const loc = traceloc_1.here(); | ||
var loc = traceloc_1.here(); | ||
alsatian_1.Expect(loc.func).not.toBeTruthy(); | ||
@@ -111,6 +147,8 @@ alsatian_1.Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
})(); | ||
} | ||
testSameLineHere() { | ||
let loc1, loc2; // tslint:disable-line | ||
loc1 = traceloc_1.here(), loc2 = traceloc_1.here(); | ||
}; | ||
TracingTests.prototype.testSameLineHere = function () { | ||
var loc1; | ||
var loc2; | ||
loc1 = traceloc_1.here(); | ||
loc2 = traceloc_1.here(); | ||
alsatian_1.Expect(loc1.line).toBeGreaterThan(0); | ||
@@ -120,6 +158,6 @@ alsatian_1.Expect(loc1.line).toBe(loc2.line); | ||
alsatian_1.Expect(loc1.col).toBeLessThan(loc2.col); | ||
} | ||
testAjacentHeres() { | ||
const loc1 = traceloc_1.here(); | ||
const loc2 = traceloc_1.here(); | ||
}; | ||
TracingTests.prototype.testAjacentHeres = function () { | ||
var loc1 = traceloc_1.here(); | ||
var loc2 = traceloc_1.here(); | ||
alsatian_1.Expect(loc2.line).toBe(loc1.line + 1); | ||
@@ -129,6 +167,6 @@ alsatian_1.Expect(loc2.line).toBeGreaterThan(loc1.line); | ||
alsatian_1.Expect(loc1.col).toBe(loc2.col); | ||
} | ||
testHere() { | ||
const loc1 = traceloc_1.here(); // tslint:disable-line | ||
const loc2 = traceloc_1.here(); // tslint:disable-line | ||
}; | ||
TracingTests.prototype.testHere = function () { | ||
var loc1 = traceloc_1.here(); // tslint:disable-line | ||
var loc2 = traceloc_1.here(); // tslint:disable-line | ||
alsatian_1.Expect(loc1.line).toBeGreaterThan(0); | ||
@@ -138,4 +176,5 @@ alsatian_1.Expect(loc2.line).toBe(loc1.line + 2); | ||
alsatian_1.Expect(loc2.col).toBe(2); | ||
} | ||
} | ||
}; | ||
return TracingTests; | ||
}()); | ||
__decorate([ | ||
@@ -184,2 +223,20 @@ alsatian_1.Test(), | ||
__decorate([ | ||
alsatian_1.Test("Test we can have our own here()"), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], TracingTests.prototype, "testMyHere", null); | ||
__decorate([ | ||
alsatian_1.Test("Test we can have nest here beyond 1"), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], TracingTests.prototype, "testNestingHereBeyond1", null); | ||
__decorate([ | ||
alsatian_1.Test("Test custom here that returns string"), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], TracingTests.prototype, "testCustomHere", null); | ||
__decorate([ | ||
alsatian_1.Test("Execute anon"), | ||
@@ -186,0 +243,0 @@ __metadata("design:type", Function), |
{ | ||
"name": "traceloc", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Trace the current location, i.e. filename, line, col ...", | ||
@@ -43,3 +43,3 @@ "main": "./out/traceloc.js", | ||
"test:traceloc": "alsatian out/traceloc.spec.js", | ||
"test": "yarn test:traceloc && yarn test:js", | ||
"test": "yarn test:traceloc && yarn review && yarn test:js", | ||
"review": "tslint ./src/*.ts", | ||
@@ -46,0 +46,0 @@ "coverage": "nyc --reporter=lcov --reporter=html yarn test:traceloc && nyc report", |
{"ts":"2017-04-30T21:09:16.492Z","hz":7164.543892555762,"rme":1.9947557718014683,"inchz":79790153.41384858} | ||
{"ts":"2017-04-30T21:09:37.109Z","hz":7007.510507475657,"rme":2.0546639618210145,"inchz":78773496.36960019} | ||
{"ts":"2017-04-30T21:20:59.219Z","hz":7403.5446385603045,"rme":1.8164463699303437,"inchz":78356465.10354127} | ||
{"ts":"2017-05-06T17:07:21.171Z","hz":7309.747620250317,"rme":1.7821964962786925,"inchz":77738327.1214348} | ||
{"ts":"2017-05-06T17:07:35.504Z","hz":6958.958248636524,"rme":2.7066087768915086,"inchz":76819290.91674113} | ||
{"ts":"2017-05-06T17:07:50.299Z","hz":7173.8744361244135,"rme":1.6137059376368632,"inchz":79604920.73369652} | ||
{"ts":"2017-05-06T17:08:05.016Z","hz":6199.476851168772,"rme":3.6016703967022208,"inchz":74151282.95190042} |
@@ -5,3 +5,3 @@ const Benchmark = require("benchmark-async"); | ||
let outFile; | ||
var outFile; | ||
if (process.argv.length == 3) { | ||
@@ -37,8 +37,8 @@ outFile = process.argv[2]; | ||
//console.log(`suite=${JSON.stringify(suite, null, "\t")}`); | ||
let rme = suite[0].stats.rme; | ||
let hz = suite[0].hz; | ||
let inchz = suite[1].hz; | ||
let jsonDate = (new Date()).toJSON(); | ||
let data = { "ts": jsonDate, "hz": hz, "rme": rme, "inchz": inchz}; | ||
let jsonData = JSON.stringify(data); | ||
var rme = suite[0].stats.rme; | ||
var hz = suite[0].hz; | ||
var inchz = suite[1].hz; | ||
var jsonDate = (new Date()).toJSON(); | ||
var data = { "ts": jsonDate, "hz": hz, "rme": rme, "inchz": inchz}; | ||
var jsonData = JSON.stringify(data); | ||
console.log(`data: ${jsonData}`); | ||
@@ -45,0 +45,0 @@ if (outFile) { |
@@ -1,2 +0,2 @@ | ||
# Trace the current location in a program [](https://travis-ci.org/winksaville/traceloc) | ||
# Trace the current location in a program [](https://travis-ci.org/winksaville/traceloc)[](https://www.bithound.io/github/winksaville/traceloc) | ||
Allows access to the file, func, line and col while executing | ||
@@ -3,0 +3,0 @@ |
@@ -5,3 +5,3 @@ const Expect = require("alsatian").Expect; | ||
setProjectRoot(__dirname); | ||
setProjectRoot("./src"); | ||
@@ -13,3 +13,3 @@ // A self executing anonymous function | ||
Expect(loc.func).not.toBeTruthy(); | ||
Expect(loc.file).toBe("src/traceloc.spec.js"); | ||
Expect(loc.file).toBe("traceloc.spec.js"); | ||
Expect(loc.line).toBeGreaterThan(0); | ||
@@ -23,3 +23,3 @@ Expect(loc.col).toBeGreaterThan(0); | ||
Expect(loc.func).toBe("test"); | ||
Expect(loc.file).toBe("src/traceloc.spec.js"); | ||
Expect(loc.file).toBe("traceloc.spec.js"); | ||
Expect(loc.line).toBeGreaterThan(0); | ||
@@ -26,0 +26,0 @@ Expect(loc.col).toBeGreaterThan(0); |
@@ -12,2 +12,19 @@ import { | ||
function myHere(): ITraceLoc { | ||
return here(1); | ||
} | ||
function nestHere2(): ITraceLoc { | ||
return here(2); | ||
} | ||
function nestHere1(): ITraceLoc { | ||
return nestHere2(); | ||
} | ||
function customHere(): string { | ||
const loc = here(1); | ||
return `customHere: ${loc.func}:${loc.line}`; | ||
} | ||
export class TracingTests { | ||
@@ -19,3 +36,3 @@ | ||
let orgVal = setProjectRoot("weirdValue"); | ||
const orgVal = setProjectRoot("weirdValue"); | ||
Expect(setProjectRoot(orgVal)).toBe("weirdValue"); | ||
@@ -117,2 +134,29 @@ Expect(setProjectRoot(orgVal)).toBe(orgVal); | ||
@Test("Test we can have our own here()") | ||
public testMyHere() { | ||
const locMyHere = myHere(); | ||
const locHere = here(); | ||
Expect(locMyHere.func).toBe("TracingTests.testMyHere"); | ||
Expect(locMyHere.file).toBe("src/traceloc.spec.ts"); | ||
Expect(locMyHere.line).toBe(locHere.line - 1); | ||
Expect(locMyHere.col).toBe(locHere.col + 2); | ||
} | ||
@Test("Test we can have nest here beyond 1") | ||
public testNestingHereBeyond1() { | ||
const locNestHere1 = nestHere1(); | ||
const locHere = here(); | ||
Expect(locNestHere1.func).toBe("TracingTests.testNestingHereBeyond1"); | ||
Expect(locNestHere1.file).toBe("src/traceloc.spec.ts"); | ||
Expect(locNestHere1.line).toBe(locHere.line - 1); | ||
Expect(locNestHere1.col).toBe(locHere.col); | ||
} | ||
@Test("Test custom here that returns string") | ||
public testCustomHere() { | ||
const locCustomHere = customHere(); | ||
const locHere = here(); | ||
Expect(locCustomHere).toBe(`customHere: ${locHere.func}:${locHere.line - 1}`); | ||
} | ||
@Test("Execute anon") | ||
@@ -131,4 +175,5 @@ public anon() { | ||
public testSameLineHere() { | ||
let loc1, loc2: ITraceLoc; // tslint:disable-line | ||
loc1 = here(), loc2 = here(); | ||
let loc1: ITraceLoc; | ||
let loc2: ITraceLoc; | ||
loc1 = here(); loc2 = here(); | ||
Expect(loc1.line).toBeGreaterThan(0); | ||
@@ -161,2 +206,3 @@ Expect(loc1.line).toBe(loc2.line); | ||
} | ||
} |
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"target": "es5", | ||
"module": "commonjs", | ||
@@ -5,0 +5,0 @@ "moduleResolution": "Node", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
100717
31
997