GithubHelp home page GithubHelp logo

callsites's Introduction

callsites

Get callsites from the V8 stack trace API

Install

$ npm install callsites

Usage

import callsites from 'callsites';

function unicorn() {
	console.log(callsites()[0].getFileName());
	//=> '/Users/sindresorhus/dev/callsites/test.js'
}

unicorn();

API

Returns an array of callsite objects with the following methods:

  • getThis: Returns the value of this.
  • getTypeName: Returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.
  • getFunction: Returns the current function.
  • getFunctionName: Returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
  • getMethodName: Returns the name of the property of this or one of its prototypes that holds the current function.
  • getFileName: If this function was defined in a script returns the name of the script.
  • getLineNumber: If this function was defined in a script returns the current line number.
  • getColumnNumber: If this function was defined in a script returns the current column number
  • getEvalOrigin: If this function was created using a call to eval returns a string representing the location where eval was called.
  • isToplevel: Is this a top-level invocation, that is, is this the global object?
  • isEval: Does this call take place in code defined by a call to eval?
  • isNative: Is this call in native V8 code?
  • isConstructor: Is this a constructor call?

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.

callsites's People

Contributors

benbeattiehood avatar bendingbender avatar coreyfarrell avatar raphinesse avatar samverschueren avatar sindresorhus 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  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  avatar  avatar  avatar

callsites's Issues

`getFileName()` returns the file name with `file://` prefix when "type": "module" is specified

Hello,

With Node 16 and when "type": "module" is specified in package.json, the function getFileName() returns the file name with file:// prefix.

Example with the reproducible code below:
$ node index.js

Result:
file:///Users/myuser/projects/myproject/mytest/index.js

Expected result:
/Users/myuser/projects/myproject/mytest/index.js

Reproducible code:
package.json

{
  "name": "mytest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "callsites": "^4.0.0"
  }
}

index.js

import callsites from 'callsites';

function unicorn() {
	console.log(callsites()[0].getFileName());
}

unicorn();

Side question

You have index.js index.d.ts but why not just use typescript, index.ts?

eslint fails on node-chakra

This happens when I run eslint sample.js (.eslintrc.js & sample.js both exist)

Cannot read config file: .eslintrc.js
Error: Unable to get property 'getFileName' of undefined or null reference
TypeError: Cannot read config file: .eslintrc.js
Error: Unable to get property 'getFileName' of undefined or null reference
   at module.exports (node_modules\caller-path\index.js:5:2)
   at module.exports (node_modules\require-uncached\index.js:11:2)
   at loadJSConfigFile (node_modules\eslint\lib\config\config-file.js:157:9)
   at loadConfigFile (node_modules\eslint\lib\config\config-file.js:197:13)
   at load (node_modules\eslint\lib\config\config-file.js:437:9)
   at loadConfig (node_modules\eslint\lib\config.js:67:13)
   at getLocalConfig (node_modules\eslint\lib\config.js:129:9)
   at Config.prototype.getConfig (node_modules\eslint\lib\config.js:223:9)
   at processText (node_modules\eslint\lib\cli-engine.js:155:5)
   at processFile (node_modules\eslint\lib\cli-engine.js:227:9)

Reading the source code, Error.prepareStackTrace may be a V8-specific trick that won't work on ChakraCore.

No async promise stacks

Too bad V8 does not have anything useful when we try to determine callsite in promise callback

function foo() {
  const sites = callsites()
  // will have just foo()
}
Promise.resolve(42).then(foo)

Few APIs return undefined in strict mode

I wanted to make sure all the APIs return valid values as mentioned in docs. Here is my test case tested on node v6.0.0.

const callsites = require('callsites');
function unicorn() {
    var frame = callsites()[0];
    console.log('getThis : ' + frame.getThis());
    console.log('getTypeName : ' + frame.getTypeName());
    console.log('getFunction : ' + frame.getFunction());
    console.log('getFunctionName : ' + frame.getFunctionName());
    console.log('getMethodName : ' + frame.getMethodName());
    console.log('getFileName : ' + frame.getFileName());
    console.log('getLineNumber : ' + frame.getLineNumber());
    console.log('getColumnNumber : ' + frame.getColumnNumber());
    console.log('getEvalOrigin : ' + frame.getEvalOrigin());
    console.log('isToplevel : ' + frame.isToplevel());
    console.log('isEval : ' + frame.isEval());
    console.log('isNative : ' + frame.isNative());
    console.log('isConstructor : ' + frame.isConstructor());
}

var x = new unicorn();
getThis : undefined
getTypeName : unicorn
getFunction : undefined
getFunctionName : unicorn
getMethodName : null
getFileName : E:\npm\test.js
getLineNumber : 3
getColumnNumber : 17
getEvalOrigin : E:\npm\test.js
isToplevel : false
isEval : false
isNative : false
isConstructor : true

This is happening because v8 uses function.caller to retrieve function and other properties which is not valid in strict mode. If i comment this line from index.js, here is the output I get.

getThis : [object Object]
getTypeName : unicorn
getFunction : function unicorn() {
    var frame = callsites()[0];
    console.log('getThis : ' + frame.getThis());
    console.log('getTypeName : ' + frame.getTypeName());
    console.log('getFunction : ' + frame.getFunction());
    console.log('getFunctionName : ' + frame.getFunctionName());
    console.log('getMethodName : ' + frame.getMethodName());
    console.log('getFileName : ' + frame.getFileName());
    console.log('getLineNumber : ' + frame.getLineNumber());
    console.log('getColumnNumber : ' + frame.getColumnNumber());
    console.log('getEvalOrigin : ' + frame.getEvalOrigin());
    console.log('isToplevel : ' + frame.isToplevel());
    console.log('isEval : ' + frame.isEval());
    console.log('isNative : ' + frame.isNative());
    console.log('isConstructor : ' + frame.isConstructor());
}
getFunctionName : unicorn
getMethodName : null
getFileName : E:\npm\test.js
getLineNumber : 3
getColumnNumber : 17
getEvalOrigin : E:\npm\test.js
isToplevel : false
isEval : false
isNative : false
isConstructor : true

Can you either fix the documentation or the APIs to return appropriately?

License file in repo & npm package

@sindresorhus Hi Sindre, thank you for this package. Would you mind adding the license file to the npm package of v0.2.0 and latest? Without it we're not really able to use it, because it makes the npm package a distribution of proprietary code.

Additionally I noticed the license file does not include the date, which makes it invalid. I'd greatly appreciate it if you could add the dates to the license files in GH repo as well.

Many thanks for your consideration.

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.