sindresorhus/caller-path

Integer as caller depth argument? #4

dy posted onGitHub

Hi @sindresorhus!

Awesome package, I am using it in save-file and in upcoming audio release.

Unfortunately it turns out that oftentimes we need not the caller path, but caller's caller path. For example, I expect path to be the invocator's path, not the saveAs path:

//test/index.js

const saveAs = require('save-file')

//so I expect the file to be placed into `test` directory
//but caller-path inside `saveAs` returns `saveAs` path `.../node_modules/save-file/index.js`
saveAs(buf, 'my-record.wav')

What I do now is use callsites directly here:

var callerPath = callsites()[1].getFileName()

What I'd suggest is passing an id of the caller for that purpose:

var cpath = callerPath(1)

That would not break the current behaviour and at the same time would allow to specify caller. If you need a PR I can come up with one.

Thank you for your time, sincerely,


Sure, good idea. Pull request welcome. I think it should be callerPath({depth: 1}) for clarity.

posted by sindresorhus almost 8 years ago

Oh, I tried to resolve that, but turns out I don't really understand the logic of caller-callsite. If I do in test.js

let callsite = require('caller-callsite');

function x() {
    return callsite();
}
function y() {
    return x();
}

y().getFunctionName() //we get 'x'

But if I place fixture4.js:

let callsite = require('caller-callsite');

function x() {
    return callsite();
}
function y() {
    return x();
}

and then run test.js:

let f4 = require('./fixture4')

f4().getFunctionName() //we get null

I am afraid I am lost, so

PS. Also with slow wi-fi ava took like 15 minutes to install :cry:

posted by dy almost 8 years ago

@issuehuntfest has funded $40.00 to this issue. See it on IssueHunt

posted by IssueHuntBot about 6 years ago

@sindresorhus has rewarded $36.00 to @yaodingyd. See it on IssueHunt

  • :moneybag: Total deposit: $40.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $4.00
posted by issuehunt-app[bot] over 5 years ago

Fund this Issue

$40.00
Rewarded

Rewarded pull request

Recent activities

yaodingyd was rewarded by sindresorhus for sindresorhus/caller-path# 4
over 5 years ago
yaodingyd submitted an output to  sindresorhus/ caller-path# 4
over 5 years ago