├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── fixture2.js ├── index.js ├── fixture.js ├── fixture3.js ├── .editorconfig ├── index.test-d.ts ├── test.js ├── package.json ├── license ├── index.d.ts └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | tidelift: npm/caller-path 3 | -------------------------------------------------------------------------------- /fixture2.js: -------------------------------------------------------------------------------- 1 | import fixture from './fixture.js'; 2 | 3 | export default function fixture2({depth = 0} = {}) { 4 | return fixture({depth}); 5 | } 6 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import callerCallsite from 'caller-callsite'; 2 | 3 | export default function callerpath({depth = 0} = {}) { 4 | const callsite = callerCallsite({depth}); 5 | return callsite && callsite.getFileName(); 6 | } 7 | -------------------------------------------------------------------------------- /fixture.js: -------------------------------------------------------------------------------- 1 | import callerPath from './index.js'; 2 | 3 | function foo({depth = 0} = {}) { 4 | return callerPath({depth}); 5 | } 6 | 7 | export default function fixture({depth = 0} = {}) { 8 | return foo({depth}); 9 | } 10 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import callerPath from './index.js'; 3 | 4 | expectType(callerPath()); 5 | expectType(callerPath({depth: 1})); 6 | expectError(callerPath()); 7 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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()), 'test.js'); 9 | t.is(path.basename(fixture2()), 'test.js'); 10 | t.is(path.basename(fixture3()), 'test.js'); 11 | t.is(path.basename(fixture3({depth: 1})), 'test.js'); 12 | t.is(path.basename(fixture3({depth: 2})), 'fixture3.js'); 13 | t.is(path.basename(fixture3({depth: 3})), 'fixture2.js'); 14 | t.is(path.basename(fixture3({depth: 4})), 'fixture.js'); 15 | t.is(path.basename(fixture3({depth: 5})), 'index.js'); 16 | }); 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "caller-path", 3 | "version": "4.0.0", 4 | "description": "Get the path of the caller function", 5 | "license": "MIT", 6 | "repository": "sindresorhus/caller-path", 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 | "path", 30 | "parent", 31 | "callsites", 32 | "callsite", 33 | "stacktrace", 34 | "stack", 35 | "trace", 36 | "function", 37 | "file" 38 | ], 39 | "dependencies": { 40 | "caller-callsite": "^5.0.0" 41 | }, 42 | "devDependencies": { 43 | "ava": "^3.15.0", 44 | "tsd": "^0.17.0", 45 | "typescript": "^4.4.3", 46 | "xo": "^0.45.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | The caller path depth, meaning how many levels we follow back on the stack trace. 4 | 5 | @default 0 6 | 7 | @example 8 | ``` 9 | // foo.ts 10 | import callerPath from 'caller-path'; 11 | 12 | export default function foo() { 13 | console.log(callerPath()); 14 | //=> '/Users/sindresorhus/dev/unicorn/foobar.ts' 15 | console.log(callerPath({depth: 1})); 16 | //=> '/Users/sindresorhus/dev/unicorn/bar.ts' 17 | console.log(callerPath({depth: 2})); 18 | //=> '/Users/sindresorhus/dev/unicorn/foo.ts' 19 | } 20 | 21 | // bar.ts 22 | import foo from './foo.js'; 23 | 24 | export default function bar() => { 25 | foo(); 26 | } 27 | 28 | // foobar.ts 29 | import bar from './bar.js'; 30 | bar(); 31 | ``` 32 | */ 33 | readonly depth?: number; 34 | } 35 | 36 | /** 37 | Get the path of the caller function. 38 | 39 | If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined for some reason, it will return `undefined`. 40 | 41 | @example 42 | ``` 43 | // foo.ts 44 | import callerPath from 'caller-path'; 45 | 46 | export default function foo() { 47 | console.log(callerPath()); 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 callerPath(options?: Options): string | undefined; 57 | 58 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # caller-path 2 | 3 | > Get the path of the caller function 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install caller-path 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | // foo.js 15 | import callerPath from 'caller-path'; 16 | 17 | export default function foo() { 18 | console.log(callerPath()); 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 | If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined for some reason, it will return `undefined`. 30 | 31 | ## API 32 | 33 | ### callerPath(options?) 34 | 35 | Get the path of the caller function. 36 | 37 | ##### depth 38 | 39 | Type: `number`\ 40 | Default: `0` 41 | 42 | The caller path depth, meaning how many levels we follow back on the stack trace. 43 | 44 | For example: 45 | 46 | ```js 47 | // foo.js 48 | import callerPath from 'caller-path'; 49 | 50 | export default function foo() { 51 | console.log(callerPath()); 52 | //=> '/Users/sindresorhus/dev/unicorn/foobar.js' 53 | console.log(callerPath({depth: 1})); 54 | //=> '/Users/sindresorhus/dev/unicorn/bar.js' 55 | console.log(callerPath({depth: 2})); 56 | //=> '/Users/sindresorhus/dev/unicorn/foo.js' 57 | } 58 | ``` 59 | 60 | ```js 61 | // bar.js 62 | import foo from './foo.js'; 63 | 64 | export default function bar() { 65 | foo(); 66 | } 67 | ``` 68 | 69 | ```js 70 | // foobar.js 71 | import bar from './bar.js'; 72 | bar(); 73 | ``` 74 | 75 | --- 76 | 77 |
78 | 79 | Get professional support for this package with a Tidelift subscription 80 | 81 |
82 | 83 | Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. 84 |
85 |
86 | --------------------------------------------------------------------------------