Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

registration.js

199 lines6.3 KB

1/**
2 * How the oracle rules recognize a test registration and the shared helpers
3 * from oracle-config. Every rule uses these, so that the rules agree on what
4 * a test is.
5 */
6​
7export const testRoots = new Set([`describe`, `fcTest`, `it`, `suite`, `test`])
8export const disablingModifiers = new Set([`fixme`, `skip`, `todo`])
9export const conditionalModifiers = new Set([`runIf`, `skipIf`])
10export const aliasModifiers = new Set([
11 ...disablingModifiers,
12 ...conditionalModifiers,
13 `only`,
14])
15// The members that keep a call a test registration. Playwright's `test` also
16// has calls such as `test.info()` and `test.step()` that register nothing.
17export const registrationMembers = new Set([
18 ...aliasModifiers,
19 `concurrent`,
20 `describe`,
21 `each`,
22 `fails`,
23 `for`,
24 `parallel`,
25 `prop`,
26 `sequential`,
27 `serial`,
28])
29// The shared helpers that return a test registration or its `skip` form.
30export const selectionHelpers = new Set([
31 `oracleCalibrationTest`,
32 `oracleReplayTest`,
33])
34​
35/** The root identifier and member names of a callee such as `it.skip.prop(...)`. */
36export function calleeChain(callee) {
37 const names = []
38 let node = callee
39 for (;;) {
40 if (node.type === `MemberExpression`) {
41 if (node.property.type === `Identifier`) names.unshift(node.property.name)
42 node = node.object
43 } else if (
44 node.type === `CallExpression` &&
45 node.callee.type === `Identifier` &&
46 selectionHelpers.has(node.callee.name) &&
47 node.arguments.length > 0
48 ) {
49 // `oracleReplayTest(it)` returns `it` or `it.skip`, so a registration
50 // made through it is checked as a registration of `it`.
51 node = node.arguments[0]
52 } else if (node.type === `CallExpression`) {
53 node = node.callee
54 } else {
55 break
56 }
57 }
58 return {
59 root: node.type === `Identifier` ? node.name : undefined,
60 rootNode: node,
61 names,
62 }
64​
65/**
66 * Whether an identifier names a test function: `it`, `test`, and the
67 * other test roots, a renamed import of one (`import { it as vitestIt }`),
68 * or a constant bound to one through a selection helper or a member chain
69 * (`const fixedCase = oracleReplayTest(it)`). A registration made through
70 * such a constant is checked as a registration of its test.
71 */
72export function isTestRoot(node, sourceCode, seen = new Set()) {
73 if (node?.type !== `Identifier`) return false
74 if (testRoots.has(node.name)) return true
75 const definition = findVariable(sourceCode.getScope(node), node.name)?.defs[0]
76 if (definition === undefined || seen.has(definition.node)) return false
77 seen.add(definition.node)
78 if (definition.type === `ImportBinding`) {
79 const imported = definition.node.imported
80 return (
81 imported !== undefined && testRoots.has(imported.name ?? imported.value)
82 )
83 }
84 return (
85 definition.type === `Variable` &&
86 definition.parent.kind === `const` &&
87 definition.node.init !== null &&
88 isTestRoot(calleeChain(definition.node.init).rootNode, sourceCode, seen)
89 )
91​
92export function isFunction(node) {
93 return (
94 node?.type === `ArrowFunctionExpression` ||
95 node?.type === `FunctionExpression`
96 )
98​
99export function findVariable(scope, name) {
100 for (let current = scope; current; current = current.upper) {
101 const variable = current.set.get(name)
102 if (variable !== undefined) return variable
103 }
104 return undefined
106​
107/** Whether `source` is an import specifier of the shared oracle-config module. */
108export function isOracleConfigSource(source) {
109 return /(^|\/)oracle-(config|declaration)(\.js)?$/.test(source)
111​
112/** A call to one of `names`, imported from oracle-config. */
113export function isSharedHelperCall(node, names, sourceCode) {
114 if (
115 node?.type !== `CallExpression` ||
116 node.callee.type !== `Identifier` ||
117 !names.has(node.callee.name)
118 ) {
119 return false
120 }
121 const definition = findVariable(sourceCode.getScope(node), node.callee.name)
122 ?.defs[0]
123 return (
124 definition?.type === `ImportBinding` &&
125 isOracleConfigSource(definition.parent.source.value)
126 )
128​
129/**
130 * The test that a call registers, as `{ body }`, or undefined when the call
131 * registers no test. A suite (`describe`) registers no test of its own. The
132 * body is the function, or the name of the function, that the test runs.
133 */
134export function registeredTest(node, sourceCode) {
135 if (node.parent.type === `CallExpression` && node.parent.callee === node) {
136 // `it.each(table)` is the callee of the registration, not one itself.
137 return undefined
138 }
139 const { root, rootNode, names } = calleeChain(node.callee)
140 if (
141 !isTestRoot(rootNode, sourceCode) ||
142 root === `describe` ||
143 root === `suite` ||
144 names.includes(`describe`) ||
145 !names.every((member) => registrationMembers.has(member))
146 ) {
147 return undefined
148 }
149 const body = registrationBody(node.arguments.slice(1), sourceCode)
150 return body === undefined ? undefined : { body }
152​
153/**
154 * The argument that a registration runs. Vitest takes `(name, fn, options)` or
155 * `(name, options, fn)`, so the body is the function wherever it is. Without a
156 * function, the body is a call or member expression that yields one, then an
157 * identifier. An options object, a number, or a value that is not a function
158 * binding is never the body while another argument can be.
159 */
160function registrationBody(args, sourceCode) {
161 return (
162 args.find(isFunction) ??
163 args.find(
164 (argument) =>
165 argument.type === `Identifier` &&
166 isFunctionBinding(argument, sourceCode),
167 ) ??
168 args.find(
169 (argument) =>
170 !plainValueTypes.has(argument.type) && argument.type !== `Identifier`,
171 ) ??
172 args.find((argument) => argument.type === `Identifier`)
173 )
175​
176// Argument types that carry data, not behavior, so they can be options.
177const plainValueTypes = new Set([
178 `Literal`,
179 `ObjectExpression`,
180 `ArrayExpression`,
181 `TemplateLiteral`,
182])
183​
184/**
185 * Whether an identifier names a function, or may. A binding that holds a plain
186 * value is not a function. An import, a parameter, or a call result may be one.
187 */
188function isFunctionBinding(identifier, sourceCode) {
189 const definition = findVariable(
190 sourceCode.getScope(identifier),
191 identifier.name,
192 )?.defs[0]
193 if (definition?.type === `FunctionName`) return true
194 if (definition?.type === `Variable`) {
195 const init = definition.node.init
196 return init !== null && !plainValueTypes.has(init.type)
197 }
198 return definition !== undefined