Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/oxlint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@arethetypeswrong/core": "catalog:",
"@babel/code-frame": "^7.28.6",
"@napi-rs/cli": "catalog:",
"@type-challenges/utils": "^0.1.1",
"@types/babel__code-frame": "^7.27.0",
"@types/esquery": "^1.5.4",
"@types/estree": "^1.0.8",
Expand Down
68 changes: 68 additions & 0 deletions apps/oxlint/src-js/visitor.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { ExpectExtends, ExpectTrue, ExpectFalse } from "@type-challenges/utils";
import type { VisitorObject } from "./generated/visitor.d.ts";
import type { Node, CallExpression } from "./generated/types.d.ts";

// Empty visitor object is allowed
const emptyVisitor = {};
export type _Empty = ExpectTrue<ExpectExtends<VisitorObject, typeof emptyVisitor>>;

// Specific node visitors have a stricter type for the parameter
const callExpressionVisitor = {
CallExpression: (_node: CallExpression) => {},
};
export type _CallExpr = ExpectTrue<ExpectExtends<VisitorObject, typeof callExpressionVisitor>>;

const callExpressionExitVisitor = {
"CallExpression:exit": (_node: CallExpression) => {},
};
export type _CallExprExit = ExpectTrue<
ExpectExtends<VisitorObject, typeof callExpressionExitVisitor>
>;

const debuggerStatementWrongTypeVisitor = {
DebuggerStatement: (_node: CallExpression) => {},
};
export type _DebuggerStmtWrongType = ExpectFalse<
ExpectExtends<VisitorObject, typeof debuggerStatementWrongTypeVisitor>
>;

// Node selectors have generic `Node` type for the parameter
const selectorsVisitor = {
"FunctionExpression > Identifier": (_node: Node) => {},
":matches(FunctionExpression, FunctionDeclaration)": (_node: Node) => {},
};
export type _Selectors = ExpectTrue<ExpectExtends<VisitorObject, typeof selectorsVisitor>>;

// Visitor functions can omit the node parameter
const callExpressionNoParamVisitor = {
CallExpression: () => {},
};
export type _CallExprNoParam = ExpectTrue<
ExpectExtends<VisitorObject, typeof callExpressionNoParamVisitor>
>;

// Properties of visitor object can be `undefined`.
// Ideally we'd disallow this, but it's not possible without `exactOptionalPropertyTypes: true` in `tsconfig.json`.
// Obviously we can't force that on users.
const callExpressionUndefinedVisitor = {
CallExpression: undefined,
};
export type _CallExprUndefined = ExpectTrue<
ExpectExtends<VisitorObject, typeof callExpressionUndefinedVisitor>
>;

// Other types are not allowed
const invalidNullVisitor = {
CallExpression: null,
};
export type _InvalidNull = ExpectFalse<ExpectExtends<VisitorObject, typeof invalidNullVisitor>>;

const invalidObjectVisitor = {
CallExpression: {},
};
export type _InvalidObject = ExpectFalse<ExpectExtends<VisitorObject, typeof invalidObjectVisitor>>;

const invalidStringVisitor = {
CallExpression: "oh dear",
};
export type _InvalidString = ExpectFalse<ExpectExtends<VisitorObject, typeof invalidStringVisitor>>;
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading