Skip to content

Commit cd83465

Browse files
committed
test(linter/plugins): add types test for VisitorObject
1 parent a7ff967 commit cd83465

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

apps/oxlint/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@arethetypeswrong/core": "catalog:",
4040
"@babel/code-frame": "^7.28.6",
4141
"@napi-rs/cli": "catalog:",
42+
"@type-challenges/utils": "^0.1.1",
4243
"@types/babel__code-frame": "^7.27.0",
4344
"@types/esquery": "^1.5.4",
4445
"@types/estree": "^1.0.8",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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>>;

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)