Skip to content

Commit cd80105

Browse files
authored
a more specific type for nodes that will be used in printPreservingEmptyLines (#1205)
* a more specific type for nodes that will be used in printPreservingEmptyLines function * only StrictAstNode, Identifier, or YulIdentifier can have comments attached
1 parent 6096873 commit cd80105

5 files changed

Lines changed: 13 additions & 11 deletions

File tree

src/slang-comments/handlers/add-collection-first-comment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { util } from 'prettier';
22

3-
import type { Comment, Collection } from '../../slang-nodes/types.d.ts';
3+
import type { Comment, NodeCollection } from '../../slang-nodes/types.d.ts';
44

55
const { addDanglingComment, addLeadingComment } = util;
66

77
export default function addCollectionFirstComment(
8-
node: Collection,
8+
node: NodeCollection,
99
comment: Comment
1010
): void {
1111
if (node.items.length === 0) {

src/slang-comments/handlers/add-collection-last-comment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { util } from 'prettier';
22

3-
import type { Comment, Collection } from '../../slang-nodes/types.d.ts';
3+
import type { Comment, NodeCollection } from '../../slang-nodes/types.d.ts';
44

55
const { addDanglingComment, addTrailingComment } = util;
66

77
export default function addCollectionLastComment(
8-
node: Collection,
8+
node: NodeCollection,
99
comment: Comment
1010
): void {
1111
if (node.items.length === 0) {

src/slang-nodes/MultiplicativeExpression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const multiplicationTryToHug = createHugFunction(['/', '%']);
1414
const divisionTryToHug = createHugFunction(['*', '%']);
1515
const moduloTryToHug = createHugFunction(['*', '/', '%']);
1616

17-
export const printMultiplicativeExpression = printBinaryOperation(
17+
const printMultiplicativeExpression = printBinaryOperation(
1818
createKindCheckFunction([
1919
NonterminalKind.AdditiveExpression,
2020
NonterminalKind.ShiftExpression,

src/slang-nodes/types.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,16 @@ export type StrictAstNode =
460460
| YulPaths
461461
| YulPath;
462462

463+
export type Collection = Extract<StrictAstNode, { items: unknown[] }>;
464+
463465
export type NodeCollection = Extract<
464-
StrictAstNode,
466+
Collection,
465467
{ items: StrictAstNode[] | Identifier[] | YulIdentifier[] }
466468
>;
467469

468-
export type Collection = Extract<
469-
StrictAstNode,
470-
{ items: StrictAstNode[] | Identifier[] | YulIdentifier[] | string[] }
470+
export type LineCollection = Extract<
471+
NodeCollection,
472+
{ items: Extract<StrictAstNode, { variant: StrictAstNode }>[] }
471473
>;
472474

473475
export type BinaryOperation = Extract<

src/slang-printers/print-preserving-empty-lines.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { doc, util } from 'prettier';
33
import { locEnd } from '../slang-utils/loc.js';
44

55
import type { AstPath, Doc, ParserOptions } from 'prettier';
6-
import type { AstNode, NodeCollection } from '../slang-nodes/types.d.ts';
6+
import type { AstNode, LineCollection } from '../slang-nodes/types.d.ts';
77
import type { PrintFunction } from '../types.d.ts';
88

99
const { hardline } = doc.builders;
1010

1111
export function printPreservingEmptyLines(
12-
path: AstPath<NodeCollection>,
12+
path: AstPath<LineCollection>,
1313
print: PrintFunction,
1414
options: ParserOptions<AstNode>
1515
): Doc {

0 commit comments

Comments
 (0)