MCPcopy
hub / github.com/vitest-dev/vitest / getUsedProps

Function getUsedProps

packages/runner/src/fixture.ts:637–702  ·  view source on GitHub ↗
(fn: Function, { sourceError, suiteHook }: PropsParserOptions = {})

Source from the content-addressed store, hash-verified

635}
636
637function getUsedProps(fn: Function, { sourceError, suiteHook }: PropsParserOptions = {}): Set<string> {
638 if (kPropNamesSymbol in fn) {
639 return fn[kPropNamesSymbol] as Set<string>
640 }
641
642 const {
643 index: fixturesIndex = 0,
644 original: implementation = fn,
645 } = kPropsSymbol in fn ? fn[kPropsSymbol] as FixturePropsOptions : {}
646 let fnString = filterOutComments(implementation.toString())
647
648 // match lowered async function and strip it off
649 // example code on esbuild-try https://esbuild.github.io/try/#YgAwLjI0LjAALS1zdXBwb3J0ZWQ6YXN5bmMtYXdhaXQ9ZmFsc2UAZQBlbnRyeS50cwBjb25zdCBvID0gewogIGYxOiBhc3luYyAoKSA9PiB7fSwKICBmMjogYXN5bmMgKGEpID0+IHt9LAogIGYzOiBhc3luYyAoYSwgYikgPT4ge30sCiAgZjQ6IGFzeW5jIGZ1bmN0aW9uKGEpIHt9LAogIGY1OiBhc3luYyBmdW5jdGlvbiBmZihhKSB7fSwKICBhc3luYyBmNihhKSB7fSwKCiAgZzE6IGFzeW5jICgpID0+IHt9LAogIGcyOiBhc3luYyAoeyBhIH0pID0+IHt9LAogIGczOiBhc3luYyAoeyBhIH0sIGIpID0+IHt9LAogIGc0OiBhc3luYyBmdW5jdGlvbiAoeyBhIH0pIHt9LAogIGc1OiBhc3luYyBmdW5jdGlvbiBnZyh7IGEgfSkge30sCiAgYXN5bmMgZzYoeyBhIH0pIHt9LAoKICBoMTogYXN5bmMgKCkgPT4ge30sCiAgLy8gY29tbWVudCBiZXR3ZWVuCiAgaDI6IGFzeW5jIChhKSA9PiB7fSwKfQ
650 // __async(this, null, function*
651 // __async(this, arguments, function*
652 // __async(this, [_0, _1], function*
653 if (/__async\((?:this|null), (?:null|arguments|\[[_0-9, ]*\]), function\*/.test(fnString)) {
654 fnString = fnString.split(/__async\((?:this|null),/)[1]
655 }
656 const match = fnString.match(/[^(]*\(([^)]*)/)
657 if (!match) {
658 return memoProps(fn, new Set())
659 }
660
661 const args = splitByComma(match[1])
662 if (!args.length) {
663 return memoProps(fn, new Set())
664 }
665
666 const fixturesArgument = args[fixturesIndex]
667
668 if (!fixturesArgument) {
669 return memoProps(fn, new Set())
670 }
671
672 if (!(fixturesArgument[0] === '{' && fixturesArgument.endsWith('}'))) {
673 const ordinalArgument = ordinal(fixturesIndex + 1)
674 const error = new FixtureParseError(
675 `The ${ordinalArgument} argument inside a fixture must use object destructuring pattern, e.g. ({ task } => {}). `
676 + `Instead, received "${fixturesArgument}".`
677 + `${(suiteHook ? ` If you used internal "suite" task as the ${ordinalArgument} argument previously, access it in the ${ordinal(fixturesIndex + 2)} argument instead.` : '')}`,
678 )
679 if (sourceError) {
680 error.stack = sourceError.stack?.replace(sourceError.message, error.message)
681 }
682 throw error
683 }
684
685 const _first = fixturesArgument.slice(1, -1).replace(/\s/g, '')
686 const props = splitByComma(_first).map((prop) => {
687 return prop.replace(/:.*|=.*/g, '')
688 })
689
690 const last = props.at(-1)
691 if (last && last.startsWith('...')) {
692 const error = new FixtureParseError(
693 `Rest parameters are not supported in fixtures, received "${last}".`,
694 )

Callers 3

parseUserFixturesMethod · 0.85
withFixturesFunction · 0.85
validateSuiteHookFunction · 0.85

Calls 8

filterOutCommentsFunction · 0.90
ordinalFunction · 0.90
memoPropsFunction · 0.85
splitByCommaFunction · 0.85
atMethod · 0.80
toStringMethod · 0.45
testMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected