Skip to content

Commit

Permalink
fix: knip
Browse files Browse the repository at this point in the history
  • Loading branch information
verytactical committed Dec 26, 2024
1 parent 6de6370 commit ad676d9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 50 deletions.
1 change: 1 addition & 0 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"src/prettyPrinter.ts",
"src/error/display-to-json.ts",
"src/grammar/src-info.ts",
"src/grammar/next/grammar.ts",
".github/workflows/tact*.yml"
],
"ignoreDependencies": ["@tact-lang/ton-abi"]
Expand Down
4 changes: 3 additions & 1 deletion src/grammar/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import type { $ast } from "./grammar";
import { TactCompilationError, throwInternalCompilerError } from "../../errors";
import { SyntaxErrors, syntaxErrorSchema } from "../parser-error";
import { AstSchema, getAstSchema } from "../ast-typed";
import { makeVisitor } from "./util";
import { getSrcInfo, ItemOrigin } from "../src-info";
import { displayToString } from "../../error/display-to-string";
import { makeMakeVisitor } from "../../utils/tricks";

const makeVisitor = makeMakeVisitor("$");

type Context = {
ast: AstSchema;
Expand Down
39 changes: 0 additions & 39 deletions src/grammar/next/util.ts

This file was deleted.

24 changes: 14 additions & 10 deletions src/utils/tricks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,35 @@ type Intersect<T> = (T extends unknown ? (x: T) => 0 : never) extends (
*/
type Unwrap<T> = T extends infer R ? { [K in keyof R]: R[K] } : never;

type Inputs<I> = I extends { kind: infer K }
type Inputs<I, T extends string> = I extends { [Z in T]: infer K }
? K extends string
? Record<K, (input: I) => unknown>
: never
: never;
type Outputs<O> = { [K in keyof O]: (input: never) => O[K] };
type Handlers<I, O> = Unwrap<Intersect<Inputs<I>>> & Outputs<O>;
type Handlers<I, O, T extends string> = Unwrap<Intersect<Inputs<I, T>>> &
Outputs<O>;

/**
* Make visitor for disjoint union (tagged union, discriminated union)
*/
export const makeVisitor =
export const makeMakeVisitor =
<T extends string>(tag: T) =>
<I>() =>
<O>(handlers: Handlers<I, O>) =>
(input: Extract<I, { kind: string }>): O[keyof O] => {
<O>(handlers: Handlers<I, O, T>) =>
(input: Extract<I, { [K in T]: string }>): O[keyof O] => {
const handler = (handlers as Record<string, (input: I) => O[keyof O]>)[
input.kind
input[tag]
];

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (handler) {
return handler(input);
} else {
throwInternalCompilerError(
`Reached impossible case: ${input.kind}`,
`Reached impossible case: ${input[tag]}`,
);
}
};

/**
* Make visitor for disjoint union (tagged union, discriminated union)
*/
export const makeVisitor = makeMakeVisitor("kind");

0 comments on commit ad676d9

Please sign in to comment.