mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-07 23:40:21 -05:00
af26fbebe6
Co-authored-by: Jessica Sachs <jess@jessicasachs.io> Co-authored-by: Barthélémy Ledoux <bart@cypress.io> Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com> Co-authored-by: Zach Bloomquist <github@chary.us> Co-authored-by: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com> Co-authored-by: ElevateBart <ledouxb@gmail.com> Co-authored-by: Ben Kucera <14625260+Bkucera@users.noreply.github.com>
19 lines
562 B
TypeScript
19 lines
562 B
TypeScript
const propertyGetterNameRe = /\.get\s(.+?)\s/
|
|
|
|
class AnyClass {}
|
|
|
|
export function ensureProp<T> (this: AnyClass, prop: T | undefined, methodSetter): T {
|
|
if (!prop) {
|
|
const obj = {} as Error
|
|
|
|
Error.captureStackTrace(obj, ensureProp)
|
|
const propertyGetterStackLine = obj.stack!.split('\n')[1]
|
|
const matched = propertyGetterStackLine.match(propertyGetterNameRe)
|
|
const propName = matched && matched[1]
|
|
|
|
throw new Error(`${this.constructor.name}#${methodSetter} must first be called before accessing 'this.${propName}'`)
|
|
}
|
|
|
|
return prop
|
|
}
|