├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixture.js ├── fixture2.js ├── fixture3.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | tidelift: npm/caller-callsite 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixture.js: -------------------------------------------------------------------------------- 1 | import callerCallsite from './index.js'; 2 | 3 | function foo({depth = 0} = {}) { 4 | return callerCallsite({depth}); 5 | } 6 | 7 | export default function fixture({depth = 0} = {}) { 8 | return foo({depth}); 9 | } 10 | -------------------------------------------------------------------------------- /fixture2.js: -------------------------------------------------------------------------------- 1 | import fixture from './fixture.js'; 2 | 3 | export default function fixture2({depth = 0} = {}) { 4 | return fixture({depth}); 5 | } 6 | -------------------------------------------------------------------------------- /fixture3.js: -------------------------------------------------------------------------------- 1 | import fixture2 from './fixture2.js'; 2 | 3 | function foo({depth = 0} = {}) { 4 | return fixture2({depth}); 5 | } 6 | 7 | export default function fixture3({depth = 0} = {}) { 8 | return foo({depth}); 9 | } 10 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {CallSite} from 'callsites'; 2 | 3 | export interface Options { 4 | /** 5 | The callsite depth, meaning how many levels we follow back on the stack trace. 6 | 7 | @default 0 8 | 9 | @example 10 | ``` 11 | // foo.ts 12 | import callerCallsite from 'caller-callsite'; 13 | 14 | export default function foo() { 15 | console.log(callerCallsite().getFileName()); 16 | //=> '/Users/sindresorhus/dev/unicorn/foobar.ts' 17 | console.log(callerCallsite({depth: 1}).getFileName()); 18 | //=> '/Users/sindresorhus/dev/unicorn/bar.ts' 19 | console.log(callerCallsite({depth: 2}).getFileName()); 20 | //=> '/Users/sindresorhus/dev/unicorn/foo.ts' 21 | } 22 | 23 | // bar.ts 24 | import foo from './foo.js'; 25 | 26 | export default function bar() { 27 | foo(); 28 | } 29 | 30 | // foobar.ts 31 | import bar from './bar.js'; 32 | bar(); 33 | ``` 34 | */ 35 | readonly depth?: number; 36 | } 37 | 38 | /** 39 | Get the [callsite](https://github.com/sindresorhus/callsites#api) of the caller function. 40 | 41 | @example 42 | ```js 43 | // foo.ts 44 | import callerCallsite from 'caller-callsite'; 45 | 46 | export default function foo() { 47 | console.log(callerCallsite().getFileName()); 48 | //=> '/Users/sindresorhus/dev/unicorn/bar.ts' 49 | } 50 | 51 | // bar.ts 52 | import foo from './foo.js'; 53 | foo(); 54 | ``` 55 | */ 56 | export default function callerCallsite(options?: Options): CallSite | undefined; 57 | 58 | export {CallSite} from 'callsites'; 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import callsites from 'callsites'; 2 | 3 | export default function callerCallsite({depth = 0} = {}) { 4 | const callers = []; 5 | const callerFileSet = new Set(); 6 | 7 | for (const callsite of callsites()) { 8 | const fileName = callsite.getFileName(); 9 | const hasReceiver = callsite.getTypeName() !== null && fileName !== null; 10 | 11 | if (!callerFileSet.has(fileName)) { 12 | callerFileSet.add(fileName); 13 | callers.unshift(callsite); 14 | } 15 | 16 | if (hasReceiver) { 17 | return callers[depth]; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import callerCallsite, {CallSite} from './index.js'; 3 | 4 | expectType(callerCallsite()); 5 | expectType(callerCallsite({depth: 1})); 6 | expectError(callerCallsite()); 7 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "caller-callsite", 3 | "version": "5.0.0", 4 | "description": "Get the callsite of the caller function", 5 | "license": "MIT", 6 | "repository": "sindresorhus/caller-callsite", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "caller", 27 | "calling", 28 | "module", 29 | "parent", 30 | "callsites", 31 | "callsite", 32 | "stacktrace", 33 | "stack", 34 | "trace", 35 | "function", 36 | "file" 37 | ], 38 | "dependencies": { 39 | "callsites": "^4.0.0" 40 | }, 41 | "devDependencies": { 42 | "ava": "^3.15.0", 43 | "tsd": "^0.17.0", 44 | "typescript": "^4.4.3", 45 | "xo": "^0.45.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # caller-callsite 2 | 3 | > Get the [callsite](https://github.com/sindresorhus/callsites#api) of the caller function 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install caller-callsite 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | // foo.js 15 | import callerCallsite from 'caller-callsite'; 16 | 17 | export default function foo() { 18 | console.log(callerCallsite().getFileName()); 19 | //=> '/Users/sindresorhus/dev/unicorn/bar.js' 20 | } 21 | ``` 22 | 23 | ```js 24 | // bar.js 25 | import foo from './foo.js'; 26 | foo(); 27 | ``` 28 | 29 | ## API 30 | 31 | ### callerCallsite(options?) 32 | 33 | Returns a [`callsite`](https://github.com/sindresorhus/callsites#api) object. 34 | 35 | #### options 36 | 37 | Type: `object` 38 | 39 | ##### depth 40 | 41 | Type: `number`\ 42 | Default: `0` 43 | 44 | The callsite depth, meaning how many levels we follow back on the stack trace. 45 | 46 | For example: 47 | 48 | ```js 49 | // foo.js 50 | import callerCallsite from 'caller-callsite'; 51 | 52 | export default function foo() { 53 | console.log(callerCallsite().getFileName()); 54 | //=> '/Users/sindresorhus/dev/unicorn/foobar.js' 55 | console.log(callerCallsite({depth: 1}).getFileName()); 56 | //=> '/Users/sindresorhus/dev/unicorn/bar.js' 57 | console.log(callerCallsite({depth: 2}).getFileName()); 58 | //=> '/Users/sindresorhus/dev/unicorn/foo.js' 59 | } 60 | ``` 61 | 62 | ```js 63 | // bar.js 64 | import foo from './foo.js'; 65 | 66 | export default function foo() { 67 | foo(); 68 | } 69 | ``` 70 | 71 | ```js 72 | // foobar.js 73 | import bar from './bar.js'; 74 | bar(); 75 | ``` 76 | 77 | --- 78 | 79 |
80 | 81 | Get professional support for this package with a Tidelift subscription 82 | 83 |
84 | 85 | Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. 86 |
87 |
88 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import test from 'ava'; 3 | import fixture from './fixture.js'; 4 | import fixture2 from './fixture2.js'; 5 | import fixture3 from './fixture3.js'; 6 | 7 | test('main', t => { 8 | t.is(path.basename(fixture().getFileName()), 'test.js'); 9 | t.is(path.basename(fixture2().getFileName()), 'test.js'); 10 | t.is(path.basename(fixture3().getFileName()), 'test.js'); 11 | t.is(path.basename(fixture3({depth: 1}).getFileName()), 'test.js'); 12 | t.is(path.basename(fixture3({depth: 2}).getFileName()), 'fixture3.js'); 13 | t.is(path.basename(fixture3({depth: 3}).getFileName()), 'fixture2.js'); 14 | t.is(path.basename(fixture3({depth: 4}).getFileName()), 'fixture.js'); 15 | t.is(path.basename(fixture3({depth: 5}).getFileName()), 'index.js'); 16 | }); 17 | --------------------------------------------------------------------------------