-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runtime-handler): add warning for optional context vars (#317)
- Loading branch information
Showing
5 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/runtime-handler/src/dev-runtime/utils/getCodeLocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os from 'os'; | ||
import path from 'path'; | ||
|
||
export type CodeLocation = | ||
| { | ||
line: null; | ||
filePath: null; | ||
column: null; | ||
toString(): 'unknown'; | ||
} | ||
| { | ||
line: number; | ||
column: number; | ||
filePath: string; | ||
toString(): string; | ||
}; | ||
|
||
export const UNKNOWN_LOCATION = { | ||
line: null, | ||
filePath: null, | ||
column: null, | ||
toString: (): 'unknown' => { | ||
return 'unknown'; | ||
}, | ||
}; | ||
|
||
const STACK_TRACE_REGEX = /.*\((?<fullFilePath>.*):(?<line>\d+):(?<column>\d+)\)$/i; | ||
|
||
export function getCodeLocation( | ||
options: { relativeFrom?: string; offset?: number } = {} | ||
): CodeLocation { | ||
options = { relativeFrom: undefined, offset: 0, ...options }; | ||
const fullStackTrace = new Error().stack; | ||
if (typeof fullStackTrace !== 'string') { | ||
return UNKNOWN_LOCATION; | ||
} | ||
|
||
const stackLines = fullStackTrace.split(os.EOL); | ||
// add two to the offset because the first line is "Error" and the next is the location of this function. | ||
const locationLine = stackLines[(options.offset || 0) + 2]; | ||
|
||
if (!locationLine) { | ||
return UNKNOWN_LOCATION; | ||
} | ||
|
||
const match = STACK_TRACE_REGEX.exec(locationLine); | ||
if (match === null) { | ||
return UNKNOWN_LOCATION; | ||
} | ||
|
||
const line = parseInt(match.groups?.line || '0', 0); | ||
const column = parseInt(match.groups?.column || '0', 0); | ||
let filePath = match.groups?.fullFilePath || 'unknown'; | ||
|
||
if (options.relativeFrom) { | ||
filePath = path.relative(options.relativeFrom, filePath); | ||
} | ||
|
||
const stringVersion = `${filePath}:${line}:${column}`; | ||
|
||
return { | ||
line, | ||
column, | ||
filePath, | ||
toString: () => stringVersion, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters