|
| 1 | +import type { ExpectExtends, ExpectTrue, ExpectFalse } from "@type-challenges/utils"; |
| 2 | +import type { VisitorObject } from "./generated/visitor.d.ts"; |
| 3 | +import type { Node, CallExpression } from "./generated/types.d.ts"; |
| 4 | + |
| 5 | +// Empty visitor object is allowed |
| 6 | +const emptyVisitor = {}; |
| 7 | +export type _Empty = ExpectTrue<ExpectExtends<VisitorObject, typeof emptyVisitor>>; |
| 8 | + |
| 9 | +// Specific node visitors have a stricter type for the parameter |
| 10 | +const callExpressionVisitor = { |
| 11 | + CallExpression: (_node: CallExpression) => {}, |
| 12 | +}; |
| 13 | +export type _CallExpr = ExpectTrue<ExpectExtends<VisitorObject, typeof callExpressionVisitor>>; |
| 14 | + |
| 15 | +const callExpressionExitVisitor = { |
| 16 | + "CallExpression:exit": (_node: CallExpression) => {}, |
| 17 | +}; |
| 18 | +export type _CallExprExit = ExpectTrue< |
| 19 | + ExpectExtends<VisitorObject, typeof callExpressionExitVisitor> |
| 20 | +>; |
| 21 | + |
| 22 | +const debuggerStatementWrongTypeVisitor = { |
| 23 | + DebuggerStatement: (_node: CallExpression) => {}, |
| 24 | +}; |
| 25 | +export type _DebuggerStmtWrongType = ExpectFalse< |
| 26 | + ExpectExtends<VisitorObject, typeof debuggerStatementWrongTypeVisitor> |
| 27 | +>; |
| 28 | + |
| 29 | +// Node selectors have generic `Node` type for the parameter |
| 30 | +const selectorsVisitor = { |
| 31 | + "FunctionExpression > Identifier": (_node: Node) => {}, |
| 32 | + ":matches(FunctionExpression, FunctionDeclaration)": (_node: Node) => {}, |
| 33 | +}; |
| 34 | +export type _Selectors = ExpectTrue<ExpectExtends<VisitorObject, typeof selectorsVisitor>>; |
| 35 | + |
| 36 | +// Visitor functions can omit the node parameter |
| 37 | +const callExpressionNoParamVisitor = { |
| 38 | + CallExpression: () => {}, |
| 39 | +}; |
| 40 | +export type _CallExprNoParam = ExpectTrue< |
| 41 | + ExpectExtends<VisitorObject, typeof callExpressionNoParamVisitor> |
| 42 | +>; |
| 43 | + |
| 44 | +// Properties of visitor object can be `undefined`. |
| 45 | +// Ideally we'd disallow this, but it's not possible without `exactOptionalPropertyTypes: true` in `tsconfig.json`. |
| 46 | +// Obviously we can't force that on users. |
| 47 | +const callExpressionUndefinedVisitor = { |
| 48 | + CallExpression: undefined, |
| 49 | +}; |
| 50 | +export type _CallExprUndefined = ExpectTrue< |
| 51 | + ExpectExtends<VisitorObject, typeof callExpressionUndefinedVisitor> |
| 52 | +>; |
| 53 | + |
| 54 | +// Other types are not allowed |
| 55 | +const invalidNullVisitor = { |
| 56 | + CallExpression: null, |
| 57 | +}; |
| 58 | +export type _InvalidNull = ExpectFalse<ExpectExtends<VisitorObject, typeof invalidNullVisitor>>; |
| 59 | + |
| 60 | +const invalidObjectVisitor = { |
| 61 | + CallExpression: {}, |
| 62 | +}; |
| 63 | +export type _InvalidObject = ExpectFalse<ExpectExtends<VisitorObject, typeof invalidObjectVisitor>>; |
| 64 | + |
| 65 | +const invalidStringVisitor = { |
| 66 | + CallExpression: "oh dear", |
| 67 | +}; |
| 68 | +export type _InvalidString = ExpectFalse<ExpectExtends<VisitorObject, typeof invalidStringVisitor>>; |
0 commit comments