Skip to content

Commit

Permalink
feat(api): cleanup api (#2)
Browse files Browse the repository at this point in the history
* Make fields on `CallId` non-optional 
* Rename `fileName` -> `file`, since it could also be a file URL (instead of a file name)
  • Loading branch information
nicojs authored Feb 19, 2021
1 parent a8eaad4 commit a5142d1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/call-id.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface CallId {
fileName: string | null;
column: number | null;
line: number | null;
file: string;
column: number;
line: number;
}
4 changes: 2 additions & 2 deletions src/stack-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function tryParseEval(line: string): CallId | null {
return (
maybeMatch && {
column: 0,
fileName: maybeMatch[1],
file: maybeMatch[1],
line: parseInt(maybeMatch[2], 10),
}
);
Expand All @@ -31,7 +31,7 @@ function tryParseNormal(line: string): CallId | null {
const maybeMatch = stackLineRegex.exec(line);
return (
maybeMatch && {
fileName: maybeMatch[1],
file: maybeMatch[1],
line: parseInt(maybeMatch[2], 10),
column: parseInt(maybeMatch[3], 10),
}
Expand Down
6 changes: 2 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export function toFileName(input: string | undefined | null): string | null {
if (input === undefined || input === null) {
return null;
} else if (input.startsWith('file:')) {
export function toFileName(input: string): string {
if (input.startsWith('file:')) {
const url = new URL(input);
if (url.protocol === 'file:') {
return url.pathname;
Expand Down
20 changes: 12 additions & 8 deletions src/v8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export function getCallIdV8(distance = 0): CallId | null {

function tryParseEvalOrigin(callSite: NodeJS.CallSite): CallId | null {
//"eval at <anonymous> (file:///home/nicojs/github/call-id/test/node.test.js:66:12)"
const origin = /.*\(([^)]*):(\d+):(\d+)\)/.exec(
const origin = /.*\(([^)]+):(\d+):(\d+)\)/.exec(
callSite.getEvalOrigin() ?? ''
);
if (origin) {
return {
fileName: toFileName(origin[1]),
file: toFileName(origin[1]),
line: parseInt(origin[2], 10),
column: parseInt(origin[3], 10),
};
Expand All @@ -47,10 +47,14 @@ function tryParseEvalOrigin(callSite: NodeJS.CallSite): CallId | null {
}

function parseCallSite(callSite: NodeJS.CallSite): CallId | null {
//"eval at <anonymous> (file:///home/nicojs/github/call-id/test/node.test.js:66:12)"
return {
column: callSite.getColumnNumber(),
line: callSite.getLineNumber(),
fileName: toFileName(callSite.getFileName()),
};
const fileUrl = callSite.getFileName();
const line = callSite.getLineNumber();
if (fileUrl !== null && line !== null) {
return {
column: callSite.getColumnNumber() ?? 0,
line,
file: toFileName(fileUrl),
};
}
return null;
}
7 changes: 3 additions & 4 deletions test/templates/browser.test.js.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const { expect } = chai;

// A JS file in order to be sure of the line and column numbers
describe('browser', () => {
const fileName = import.meta.url;



const FILE_NAME = import.meta.url;
const STRICT_EVAL_COLUMNS = !!Error.captureStackTrace; // in firefox we don't know the exact column when using `eval`
const LINE_OFFSET = 9;

%TESTS%
});
7 changes: 3 additions & 4 deletions test/templates/node.test.cjs.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const { getCallId } = require('../..');

// A JS file in order to be sure of the line and column numbers
describe('node cjs', () => {
const fileName = __filename;



const FILE_NAME = __filename;
const STRICT_EVAL_COLUMNS = true;
const LINE_OFFSET = 9;

%TESTS%
});
7 changes: 3 additions & 4 deletions test/templates/node.test.js.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { getCallId } from 'call-id';

// A JS file in order to be sure of the line and column numbers
describe('node esm', () => {
const fileName = new URL(import.meta.url).pathname;



const FILE_NAME = new URL(import.meta.url).pathname;
const STRICT_EVAL_COLUMNS = true;
const LINE_OFFSET = 9;

%TESTS%
});
27 changes: 12 additions & 15 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const LINE_OFFSET = 10;
const STRICT_EVAL_COLUMNS = !!Error.captureStackTrace; // in firefox we don't know the exact column when using `eval`

// These tests get loaded in the templated files at "templates". Should be loaded at line LINE_OFFSET
it('should give the correct location when called from a function', () => {
function act() {
Expand All @@ -10,9 +7,9 @@ it('should give the correct location when called from a function', () => {
* @type {import('../../dist/call-id').CallId}
*/
const expected = {
fileName,
file: FILE_NAME,
column: 10,
line: 17 + LINE_OFFSET,
line: 14 + LINE_OFFSET,
};
expect(act()).deep.eq(expected);
});
Expand All @@ -28,9 +25,9 @@ it('should give the correct location when called with a higher distance function
* @type {import('../../dist/call-id').CallId}
*/
const expected = {
fileName,
file: FILE_NAME,
column: 10,
line: 35 + LINE_OFFSET,
line: 32 + LINE_OFFSET,
};
expect(act()).deep.eq(expected);
});
Expand All @@ -40,9 +37,9 @@ it('should allow distance 0', () => {
* @type {import('../../dist/call-id').CallId}
*/
const expected = {
fileName,
file: FILE_NAME,
column: 10,
line: 47 + LINE_OFFSET,
line: 44 + LINE_OFFSET,
};
expect(getCallId(0)).deep.eq(expected);
});
Expand All @@ -56,9 +53,9 @@ it('should work with `eval`', () => {
* @type {import('../../dist/call-id').CallId}
*/
const expected = {
fileName,
file: FILE_NAME,
column: STRICT_EVAL_COLUMNS ? 10 : 0,
line: 63 + LINE_OFFSET,
line: 60 + LINE_OFFSET,
};
expect(eval('act()')).deep.eq(expected);
});
Expand All @@ -72,9 +69,9 @@ it('should work with `new Function`', () => {
* @type {import('../../dist/call-id').CallId}
*/
const expected = {
fileName,
file: FILE_NAME,
column: STRICT_EVAL_COLUMNS ? 10 : 0,
line: 80 + LINE_OFFSET,
line: 77 + LINE_OFFSET,
};
// eslint-disable-next-line @typescript-eslint/no-implied-eval
expect(new Function('act', 'return act()')(act)).deep.eq(expected);
Expand All @@ -89,9 +86,9 @@ it('should work with `eval -> eval`', () => {
* @type {import('../../dist/call-id').CallId}
*/
const expected = {
fileName,
file: FILE_NAME,
column: STRICT_EVAL_COLUMNS ? 10 : 0,
line: 97 + LINE_OFFSET,
line: 94 + LINE_OFFSET,
};
// eslint-disable-next-line @typescript-eslint/no-implied-eval
expect(eval('eval("act()")')).deep.eq(expected);
Expand Down
4 changes: 3 additions & 1 deletion test/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare const fileName: string;
declare const getCallId: typeof import('../src/index.js').getCallId;
declare const expect: typeof import('chai').expect;
declare const LINE_OFFSET: number;
declare const STRICT_EVAL_COLUMNS: boolean; // in firefox we don't know the exact column when using `eval`
declare const FILE_NAME: string;

0 comments on commit a5142d1

Please sign in to comment.