Skip to content

Commit 7eb71b4

Browse files
committed
move utility functions to their own files
1 parent 159a78e commit 7eb71b4

6 files changed

Lines changed: 37 additions & 37 deletions

File tree

src/slang-comments/printer.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
1-
import { doc } from 'prettier';
21
import { isComment } from '../slang-utils/is-comment.js';
32

43
import type { AstPath, Doc } from 'prettier';
5-
import type { AstNode, BlockComment } from '../slang-nodes';
6-
7-
const { hardline, join } = doc.builders;
8-
9-
export function isIndentableBlockComment(comment: BlockComment): boolean {
10-
// If the comment has multiple lines and every line starts with a star
11-
// we can fix the indentation of each line. The stars in the `/*` and
12-
// `*/` delimiters are not included in the comment value, so add them
13-
// back first.
14-
const lines = comment.value.slice(1, -1).split('\n');
15-
return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*');
16-
}
17-
18-
export function printIndentableBlockComment(comment: BlockComment): Doc {
19-
const lines = comment.value.split('\n');
20-
21-
return join(
22-
hardline,
23-
lines.map((line, index) =>
24-
index === 0
25-
? line.trimEnd()
26-
: ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}`
27-
)
28-
);
29-
}
4+
import type { AstNode } from '../slang-nodes';
305

316
export function printComment(commentPath: AstPath<AstNode>): Doc {
327
const comment = commentPath.getNode()!;

src/slang-nodes/MultiLineComment.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { TerminalNode } from '@nomicfoundation/slang/cst/index.js';
22
import { TerminalKind } from '@nomicfoundation/slang/kinds/index.js';
33
import { doc } from 'prettier';
4-
import {
5-
isIndentableBlockComment,
6-
printIndentableBlockComment
7-
} from '../slang-comments/printer.js';
4+
import { isIndentableBlockComment } from '../slang-utils/is-indentable-block-comment.js';
5+
import { printIndentableBlockComment } from '../slang-printers/print-indentable-block-comment.js';
86

97
import type { Doc } from 'prettier';
108
import type { BaseComment, Location, SlangNode } from '../types';

src/slang-nodes/MultiLineNatSpecComment.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { TerminalNode } from '@nomicfoundation/slang/cst/index.js';
22
import { TerminalKind } from '@nomicfoundation/slang/kinds/index.js';
33
import { doc } from 'prettier';
4-
import {
5-
isIndentableBlockComment,
6-
printIndentableBlockComment
7-
} from '../slang-comments/printer.js';
4+
import { isIndentableBlockComment } from '../slang-utils/is-indentable-block-comment.js';
5+
import { printIndentableBlockComment } from '../slang-printers/print-indentable-block-comment.js';
86

97
import type { Doc } from 'prettier';
108
import type { BaseComment, Location, SlangNode } from '../types';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { doc } from 'prettier';
2+
3+
import type { Doc } from 'prettier';
4+
import type { BlockComment } from '../slang-nodes';
5+
6+
const { hardline, join } = doc.builders;
7+
8+
export function printIndentableBlockComment(comment: BlockComment): Doc {
9+
const lines = comment.value.split('\n');
10+
11+
return join(
12+
hardline,
13+
lines.map((line, index) =>
14+
index === 0
15+
? line.trimEnd()
16+
: ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}`
17+
)
18+
);
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BlockComment } from '../slang-nodes';
2+
3+
export function isIndentableBlockComment(comment: BlockComment): boolean {
4+
// If the comment has multiple lines and every line starts with a star
5+
// we can fix the indentation of each line. The stars in the `/*` and
6+
// `*/` delimiters are not included in the comment value, so add them
7+
// back first.
8+
const lines = comment.value.slice(1, -1).split('\n');
9+
return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*');
10+
}

src/slang-utils/metadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function getLeadingOffset(children: Node[]): number {
3838
export function getNodeMetadata(
3939
ast: SlangAstNode,
4040
initialOffset: number,
41-
includePeripheralComments = false
41+
enclosePeripheralComments = false
4242
): Metadata {
4343
if (typeof initialOffset === 'undefined') {
4444
throw new Error("Can't initiate metadata with an undefined initialOffset");
@@ -86,10 +86,10 @@ export function getNodeMetadata(
8686
return offsetsArray;
8787
}, []);
8888

89-
const leadingOffset = includePeripheralComments
89+
const leadingOffset = enclosePeripheralComments
9090
? 0
9191
: getLeadingOffset(children);
92-
const trailingOffset = includePeripheralComments
92+
const trailingOffset = enclosePeripheralComments
9393
? 0
9494
: getLeadingOffset(children.reverse());
9595
const loc = {

0 commit comments

Comments
 (0)