Coder Social home page Coder Social logo

caller-callsite's Introduction

caller-callsite

Get the callsite of the caller function

Install

npm install caller-callsite

Usage

// foo.js
import callerCallsite from 'caller-callsite';

export default function foo() {
	console.log(callerCallsite().getFileName());
	//=> '/Users/sindresorhus/dev/unicorn/bar.js'
}
// bar.js
import foo from './foo.js';
foo();

API

callerCallsite(options?)

Returns a callsite object.

options

Type: object

depth

Type: number
Default: 0

The callsite depth, meaning how many levels we follow back on the stack trace.

For example:

// foo.js
import callerCallsite from 'caller-callsite';

export default function foo() {
	console.log(callerCallsite().getFileName());
	//=> '/Users/sindresorhus/dev/unicorn/foobar.js'
	console.log(callerCallsite({depth: 1}).getFileName());
	//=> '/Users/sindresorhus/dev/unicorn/bar.js'
	console.log(callerCallsite({depth: 2}).getFileName());
	//=> '/Users/sindresorhus/dev/unicorn/foo.js'
}
// bar.js
import foo from './foo.js';

export default function foo() {
	foo();
}
// foobar.js
import bar from './bar.js';
bar();

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

caller-callsite's People

Contributors

bendingbender avatar dideler avatar litomore avatar richienb avatar sindresorhus avatar tomhughes avatar yaodingyd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

caller-callsite's Issues

Why does this module reverse the logical call stack order?

The descriptions for the depth property reads:

The callsite depth, meaning how many levels we follow back on the stack trace.

The topmost item on the call stack ist the currently executing function, if I go one item back, or lower, I will find the stack frame of my caller, then my caller's caller, and so on. So my expectation, which was also corroborated by the description of the depth property above, was that I would get my own call site for depth: 0, my caller's for depth: 1, my caller's caller's site for depth: 2 and so on.

But from all I could see, this module completely reverses the call stack with depth: 0 giving the bottommost call site (Node's entry function) and depth: N the site of the call to caller-callsite.

In any use-case I can think of, one would need to walk the stack downwards from the function calling caller-callsite. However, this is extremely awkward to do with the way depth works currently.

Is there any reason things work as they do now?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.