From 3874f6c255e0756dd2f0db30d0eabe845ee8a051 Mon Sep 17 00:00:00 2001 From: hsiwe <46127681+Hsiwe@users.noreply.github.com> Date: Sun, 5 Nov 2023 16:32:15 +0300 Subject: [PATCH] Format --- dev/test/astUtils.test.ts | 24 +++++++++++++----------- src/parse/ast/utils.ts | 37 +++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/dev/test/astUtils.test.ts b/dev/test/astUtils.test.ts index b89c9335cf..05e73f7b63 100644 --- a/dev/test/astUtils.test.ts +++ b/dev/test/astUtils.test.ts @@ -1,18 +1,20 @@ -import { describe, it} from "mocha"; -import type { astToString } from "../../src/parse/ast/utils.js"; -import { attest } from "../attest/main.js"; +import { describe, it } from "mocha" +import type { astToString } from "../../src/parse/ast/utils.js" +import { attest } from "../attest/main.js" -describe('astToString', () => { - it('no parentheses if nested ast is an array', () => { - const t = {} as unknown as astToString<[["number", "[]"], "|", "number"]> +describe("astToString", () => { + it("no parentheses if nested ast is an array", () => { + const t = {} as unknown as astToString< + [["number", "[]"], "|", "number"] + > attest(t).typed as "'number[]|number'" - }); - it('parentheses if nested ast is an infix expression', () => { + }) + it("parentheses if nested ast is an infix expression", () => { const t = {} as unknown as astToString<[["0", "|", "1"], "|", "string"]> attest(t).typed as "'(0|1)|string'" - }); + }) it('defaults to "..." if input is bad', () => { const t = {} as unknown as astToString<["0", "///", "1"]> attest(t).typed as "'...'" - }); -}); \ No newline at end of file + }) +}) diff --git a/src/parse/ast/utils.ts b/src/parse/ast/utils.ts index a38a14e586..5c13501484 100644 --- a/src/parse/ast/utils.ts +++ b/src/parse/ast/utils.ts @@ -2,24 +2,25 @@ import type { List, Literalable } from "../../utils/generics.js" import type { Scanner } from "../string/shift/scanner.js" import type { InfixExpression, PostfixExpression } from "./ast.js" -export type astToString = `'${astToStringRecurse}'`; - -type astToStringRecurse = - ast extends PostfixExpression - ? operator extends "[]" - ? `${groupAst}[]` - : never +export type astToString = `'${astToStringRecurse}'` + +type astToStringRecurse = ast extends PostfixExpression< + infer operator, + infer operand +> + ? operator extends "[]" + ? `${groupAst}[]` + : never : ast extends InfixExpression - ? operator extends "&" | "|" | "%" | Scanner.Comparator - ? `${groupAst}${operator}${groupAst}` - : never + ? operator extends "&" | "|" | "%" | Scanner.Comparator + ? `${groupAst}${operator}${groupAst}` + : never : ast extends Literalable - ? `${ast extends bigint ? `${ast}n` : ast}` - : "..." - + ? `${ast extends bigint ? `${ast}n` : ast}` + : "..." -type groupAst = ast extends List - ? ast[1] extends "[]" - ? astToStringRecurse - : `(${astToStringRecurse})` - : astToStringRecurse \ No newline at end of file +type groupAst = ast extends List + ? ast[1] extends "[]" + ? astToStringRecurse + : `(${astToStringRecurse})` + : astToStringRecurse