diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index b18bae5..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ -sudo: false language: node_js node_js: - - '6' - - '4' + - '12' + - '10' + - '8' diff --git a/fixture.js b/fixture.js index f0b9d9f..cf3b359 100644 --- a/fixture.js +++ b/fixture.js @@ -1,7 +1,7 @@ 'use strict'; -function foo() { - return require('./')(); +function foo({depth = 0} = {}) { + return require('.')({depth}); } -module.exports = () => foo(); +module.exports = ({depth = 0} = {}) => foo({depth}); diff --git a/fixture2.js b/fixture2.js index 91c3725..db36150 100644 --- a/fixture2.js +++ b/fixture2.js @@ -1,2 +1,2 @@ 'use strict'; -module.exports = () => require('./fixture')(); +module.exports = ({depth = 0} = {}) => require('./fixture')({depth}); diff --git a/fixture3.js b/fixture3.js index b3ff18c..f64ff42 100644 --- a/fixture3.js +++ b/fixture3.js @@ -1,7 +1,7 @@ 'use strict'; -function foo() { - return require('./fixture2')(); +function foo({depth = 0} = {}) { + return require('./fixture2')({depth}); } -module.exports = () => foo(); +module.exports = ({depth = 0} = {}) => foo({depth}); diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..586eff3 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,59 @@ +declare namespace callerPath { + interface Options { + /** + The caller path depth, meaning how many levels we follow back on the stack trace. + + @default 0 + + @example + ``` + // foo.ts + import callerPath = require('caller-path'); + + module.exports = () => { + console.log(callerPath()); + //=> '/Users/sindresorhus/dev/unicorn/foobar.ts' + console.log(callerPath({depth: 1})); + //=> '/Users/sindresorhus/dev/unicorn/bar.ts' + console.log(callerPath({depth: 2})); + //=> '/Users/sindresorhus/dev/unicorn/foo.ts' + } + + // bar.ts + import foo = require('./foo'); + + module.exports = () => { + foo(); + } + + // foobar.ts + import bar = require('./bar'); + bar(); + ``` + */ + readonly depth?: number; + } +} + +/** +Get the path of the caller function. + +@example +``` +// foo.ts +import callerPath = require('caller-path'); + +export default () => { + console.log(callerPath()); + //=> '/Users/sindresorhus/dev/unicorn/bar.ts' +} + +// bar.ts +import foo from './foo'; +foo(); +``` +If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined, it will return `undefined`. +*/ +declare function callerPath(options?: callerPath.Options): string | undefined; + +export = callerPath; diff --git a/index.js b/index.js index e5900f0..0cad4c9 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ 'use strict'; const callerCallsite = require('caller-callsite'); -module.exports = () => callerCallsite().getFileName(); +module.exports = ({depth = 0} = {}) => callerCallsite({depth}).getFileName(); diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..fc8583c --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,6 @@ +import {expectType, expectError} from 'tsd'; +import callerPath = require('.'); + +expectType(callerPath()); +expectType(callerPath({depth: 1})); +expectError(callerPath()); diff --git a/license b/license index 654d0bf..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) -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: +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: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -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. +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. diff --git a/package.json b/package.json index bc61fec..bcf3008 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,14 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=4" + "node": ">=8" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "caller", @@ -33,13 +34,11 @@ "file" ], "dependencies": { - "caller-callsite": "^2.0.0" + "caller-callsite": "^4.1.0" }, "devDependencies": { - "ava": "*", - "xo": "*" - }, - "xo": { - "esnext": true + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index a4e4177..b10abc6 100644 --- a/readme.md +++ b/readme.md @@ -30,6 +30,51 @@ const foo = require('./foo'); foo(); ``` +If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined, it will return `undefined`. + +## API + +### callerPath(options?) + +Get the path of the caller function. + +##### depth + +Type: `number`
+Default: `0` + +The caller path depth, meaning how many levels we follow back on the stack trace. + +For example: + +```js +// foo.js +const callerPath = require('caller-path'); + +module.exports = () => { + console.log(callerPath()); + //=> '/Users/sindresorhus/dev/unicorn/foobar.js' + console.log(callerPath({depth: 1})); + //=> '/Users/sindresorhus/dev/unicorn/bar.js' + console.log(callerPath({depth: 2})); + //=> '/Users/sindresorhus/dev/unicorn/foo.js' +} +``` + +```js +// bar.js +const foo = require('./foo'); + +module.exports = () => { + foo(); +} +``` + +```js +// foobar.js +const bar = require('./bar'); +bar(); +``` --- diff --git a/test.js b/test.js index 9cba378..3547ddc 100644 --- a/test.js +++ b/test.js @@ -4,8 +4,13 @@ import fixture from './fixture'; import fixture2 from './fixture2'; import fixture3 from './fixture3'; -test(t => { +test('main', t => { t.is(path.basename(fixture()), 'test.js'); t.is(path.basename(fixture2()), 'test.js'); t.is(path.basename(fixture3()), 'test.js'); + t.is(path.basename(fixture3({depth: 1})), 'test.js'); + t.is(path.basename(fixture3({depth: 2})), 'fixture3.js'); + t.is(path.basename(fixture3({depth: 3})), 'fixture2.js'); + t.is(path.basename(fixture3({depth: 4})), 'fixture.js'); + t.is(path.basename(fixture3({depth: 5})), 'index.js'); });