📄 getTSConfigRootDirFromStack¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 2 |
| 📦 Imports | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts
📦 Imports¶
| Name | Source |
|---|---|
path |
node:path |
fileURLToPath |
node:url |
Functions¶
getTSConfigRootDirFromStack(): string | undefined¶
Infers the tsconfigRootDir from the current call stack, using the V8 API.
See https://v8.dev/docs/stack-trace-api
This API is implemented in Deno and Bun as well.
Raw JSDoc
Calls:
Error.captureStackTracegetStackcallSite.getFileNamestackFrameFilePathOrUrl.startsWithfileURLToPath (from node:url)path.parse/^eslint\.config\.(c|m)?(j|t)s$/.testparsedPath.dir.replaceAll
Internal Comments:
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- stack is set by captureStackTrace (x2)
// ESM seem to return a file URL, so we'll convert it to a file path. (x2)
// AFAICT this isn't documented in the v8 API docs, but it seems to be the case. (x2)
// See https://github.com/typescript-eslint/typescript-eslint/issues/11429 (x2)
// workaround for https://github.com/typescript-eslint/typescript-eslint/issues/11530
// (caused by https://github.com/unjs/jiti/issues/397)
Code
export function getTSConfigRootDirFromStack(): string | undefined {
function getStack(): NodeJS.CallSite[] {
const stackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = Infinity;
const prepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, structuredStackTrace) => structuredStackTrace;
const dummyObject: { stack?: NodeJS.CallSite[] } = {};
Error.captureStackTrace(dummyObject, getTSConfigRootDirFromStack);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- stack is set by captureStackTrace
const rv = dummyObject.stack!;
Error.prepareStackTrace = prepareStackTrace;
Error.stackTraceLimit = stackTraceLimit;
return rv;
}
for (const callSite of getStack()) {
const stackFrameFilePathOrUrl = callSite.getFileName();
if (!stackFrameFilePathOrUrl) {
continue;
}
// ESM seem to return a file URL, so we'll convert it to a file path.
// AFAICT this isn't documented in the v8 API docs, but it seems to be the case.
// See https://github.com/typescript-eslint/typescript-eslint/issues/11429
const stackFrameFilePath = stackFrameFilePathOrUrl.startsWith('file://')
? fileURLToPath(stackFrameFilePathOrUrl)
: stackFrameFilePathOrUrl;
const parsedPath = path.parse(stackFrameFilePath);
if (/^eslint\.config\.(c|m)?(j|t)s$/.test(parsedPath.base)) {
if (process.platform === 'win32') {
// workaround for https://github.com/typescript-eslint/typescript-eslint/issues/11530
// (caused by https://github.com/unjs/jiti/issues/397)
return parsedPath.dir.replaceAll('/', path.sep);
}
return parsedPath.dir;
}
}
return undefined;
}
Internal helpers¶
Declared inside another function in this file.
getStack(): NodeJS.CallSite[]¶
Returns: NodeJS.CallSite[]
Calls:
Error.captureStackTrace
Internal Comments:
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- stack is set by captureStackTrace (x2)
Code
function getStack(): NodeJS.CallSite[] {
const stackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = Infinity;
const prepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, structuredStackTrace) => structuredStackTrace;
const dummyObject: { stack?: NodeJS.CallSite[] } = {};
Error.captureStackTrace(dummyObject, getTSConfigRootDirFromStack);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- stack is set by captureStackTrace
const rv = dummyObject.stack!;
Error.prepareStackTrace = prepareStackTrace;
Error.stackTraceLimit = stackTraceLimit;
return rv;
}
Generated by Syntax Scribe