From 7f6df836afde8fee51ccd0a32a46301738de0b6a Mon Sep 17 00:00:00 2001 From: Klaus Date: Wed, 16 Jul 2025 17:32:14 +0100 Subject: [PATCH 1/2] a more specific type for nodes that will be used in printPreservingEmptyLines function --- src/slang-nodes/MultiplicativeExpression.ts | 2 +- src/slang-nodes/types.d.ts | 10 ++++++---- src/slang-printers/print-preserving-empty-lines.ts | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/slang-nodes/MultiplicativeExpression.ts b/src/slang-nodes/MultiplicativeExpression.ts index f0e480a54..87a2e76a5 100644 --- a/src/slang-nodes/MultiplicativeExpression.ts +++ b/src/slang-nodes/MultiplicativeExpression.ts @@ -14,7 +14,7 @@ const multiplicationTryToHug = createHugFunction(['/', '%']); const divisionTryToHug = createHugFunction(['*', '%']); const moduloTryToHug = createHugFunction(['*', '/', '%']); -export const printMultiplicativeExpression = printBinaryOperation( +const printMultiplicativeExpression = printBinaryOperation( createKindCheckFunction([ NonterminalKind.AdditiveExpression, NonterminalKind.ShiftExpression, diff --git a/src/slang-nodes/types.d.ts b/src/slang-nodes/types.d.ts index 3227e19e0..c8756e9fb 100644 --- a/src/slang-nodes/types.d.ts +++ b/src/slang-nodes/types.d.ts @@ -460,14 +460,16 @@ export type StrictAstNode = | YulPaths | YulPath; +export type Collection = Extract; + export type NodeCollection = Extract< - StrictAstNode, + Collection, { items: StrictAstNode[] | Identifier[] | YulIdentifier[] } >; -export type Collection = Extract< - StrictAstNode, - { items: StrictAstNode[] | Identifier[] | YulIdentifier[] | string[] } +export type LineCollection = Extract< + NodeCollection, + { items: Extract[] } >; export type BinaryOperation = Extract< diff --git a/src/slang-printers/print-preserving-empty-lines.ts b/src/slang-printers/print-preserving-empty-lines.ts index ae5d67be7..bd43449db 100644 --- a/src/slang-printers/print-preserving-empty-lines.ts +++ b/src/slang-printers/print-preserving-empty-lines.ts @@ -3,13 +3,13 @@ import { doc, util } from 'prettier'; import { locEnd } from '../slang-utils/loc.js'; import type { AstPath, Doc, ParserOptions } from 'prettier'; -import type { AstNode, NodeCollection } from '../slang-nodes/types.d.ts'; +import type { AstNode, LineCollection } from '../slang-nodes/types.d.ts'; import type { PrintFunction } from '../types.d.ts'; const { hardline } = doc.builders; export function printPreservingEmptyLines( - path: AstPath, + path: AstPath, print: PrintFunction, options: ParserOptions ): Doc { From d6efc16e7dd0a084a8ee45c709198bbbb092e476 Mon Sep 17 00:00:00 2001 From: Klaus Date: Wed, 16 Jul 2025 17:40:28 +0100 Subject: [PATCH 2/2] only StrictAstNode, Identifier, or YulIdentifier can have comments attached --- src/slang-comments/handlers/add-collection-first-comment.ts | 4 ++-- src/slang-comments/handlers/add-collection-last-comment.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/slang-comments/handlers/add-collection-first-comment.ts b/src/slang-comments/handlers/add-collection-first-comment.ts index 03e21449e..1900498c8 100644 --- a/src/slang-comments/handlers/add-collection-first-comment.ts +++ b/src/slang-comments/handlers/add-collection-first-comment.ts @@ -1,11 +1,11 @@ import { util } from 'prettier'; -import type { Comment, Collection } from '../../slang-nodes/types.d.ts'; +import type { Comment, NodeCollection } from '../../slang-nodes/types.d.ts'; const { addDanglingComment, addLeadingComment } = util; export default function addCollectionFirstComment( - node: Collection, + node: NodeCollection, comment: Comment ): void { if (node.items.length === 0) { diff --git a/src/slang-comments/handlers/add-collection-last-comment.ts b/src/slang-comments/handlers/add-collection-last-comment.ts index b52fc3e72..6b288b996 100644 --- a/src/slang-comments/handlers/add-collection-last-comment.ts +++ b/src/slang-comments/handlers/add-collection-last-comment.ts @@ -1,11 +1,11 @@ import { util } from 'prettier'; -import type { Comment, Collection } from '../../slang-nodes/types.d.ts'; +import type { Comment, NodeCollection } from '../../slang-nodes/types.d.ts'; const { addDanglingComment, addTrailingComment } = util; export default function addCollectionLastComment( - node: Collection, + node: NodeCollection, comment: Comment ): void { if (node.items.length === 0) {