Skip to content

Commit 159a78e

Browse files
committed
Comments now are AstNodes, cleaning up types and logic
1 parent 77f5c56 commit 159a78e

25 files changed

Lines changed: 291 additions & 137 deletions

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type {
1717
SupportLanguage
1818
} from 'prettier';
1919
import type { AstNode } from './slang-nodes';
20-
import type { Comment } from './types';
2120

2221
const parserName = 'slang';
2322
const astFormat = 'slang-ast';
@@ -54,10 +53,10 @@ const solidityCanAttachComment = (node: { type: string }): boolean =>
5453
typeof node.type === 'string' &&
5554
node.type !== 'BlockComment' &&
5655
node.type !== 'LineComment';
57-
const canAttachComment = (node: AstNode | Comment): boolean =>
56+
const canAttachComment = (node: AstNode): boolean =>
5857
typeof node !== 'string' &&
5958
typeof node !== 'undefined' &&
60-
node.kind &&
59+
node.kind && // Make sure it's not Location
6160
!isComment(node);
6261

6362
// https://prettier.io/docs/en/plugins.html#printers

src/slang-comments/handler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import handlers from './handlers/index.js';
22

33
import type { ParserOptions } from 'prettier';
4-
import type { AstNode } from '../slang-nodes';
5-
import type { Comment } from '../types';
4+
import type { AstNode, Comment } from '../slang-nodes';
65

76
function ownLine(
87
comment: Comment,

src/slang-comments/handlers/add-hub-node-first-comment.ts

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

3-
import type { HubNode } from '../../slang-nodes';
4-
import type { Comment } from '../../types';
3+
import type { Comment, HubNode } from '../../slang-nodes';
54

65
const { addDanglingComment, addLeadingComment } = util;
76

src/slang-comments/handlers/add-hub-node-last-comment.ts

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

3-
import type { HubNode } from '../../slang-nodes';
4-
import type { Comment } from '../../types';
3+
import type { Comment, HubNode } from '../../slang-nodes';
54

65
const { addDanglingComment, addTrailingComment } = util;
76

src/slang-comments/handlers/handle-if-statement-comments.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,9 @@ import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-c
44
import addHubNodeFirstComment from './add-hub-node-first-comment.js';
55

66
import type { HandlerParams } from './types';
7-
import type { StatementVariant } from '../../slang-nodes/index.js';
8-
import type { Comment } from '../../types.js';
97

108
const { addLeadingComment, addTrailingComment } = util;
119

12-
function addIfStatementBodyFirstComment(
13-
node: StatementVariant,
14-
comment: Comment
15-
): void {
16-
if (node.kind === NonterminalKind.Block) {
17-
addHubNodeFirstComment(node.statements, comment);
18-
} else {
19-
addLeadingComment(node, comment);
20-
}
21-
}
22-
2310
export default function handleIfStatementComments({
2411
text,
2512
precedingNode,
@@ -54,7 +41,11 @@ export default function handleIfStatementComments({
5441
}
5542

5643
if (followingNode.kind === NonterminalKind.IfStatement) {
57-
addIfStatementBodyFirstComment(followingNode.body.variant, comment);
44+
if (followingNode.body.variant.kind === NonterminalKind.Block) {
45+
addHubNodeFirstComment(followingNode.body.variant.statements, comment);
46+
} else {
47+
addLeadingComment(followingNode.body.variant, comment);
48+
}
5849
return true;
5950
}
6051

src/slang-comments/handlers/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { StrictAstNode } from '../../slang-nodes';
2-
import type { Comment } from '../../types';
1+
import type { Comment, StrictAstNode } from '../../slang-nodes';
32

43
interface HandlerParams {
54
text: string;

src/slang-comments/printer.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { doc } from 'prettier';
2-
import { isBlockComment, isLineComment } from '../slang-utils/is-comment.js';
2+
import { isComment } from '../slang-utils/is-comment.js';
33

44
import type { AstPath, Doc } from 'prettier';
5-
import type { AstNode } from '../slang-nodes';
6-
import type { Comment } from '../types';
5+
import type { AstNode, BlockComment } from '../slang-nodes';
76

8-
const { hardline, join, literalline } = doc.builders;
7+
const { hardline, join } = doc.builders;
98

10-
function isIndentableBlockComment(comment: Comment): boolean {
9+
export function isIndentableBlockComment(comment: BlockComment): boolean {
1110
// If the comment has multiple lines and every line starts with a star
1211
// we can fix the indentation of each line. The stars in the `/*` and
1312
// `*/` delimiters are not included in the comment value, so add them
@@ -16,7 +15,7 @@ function isIndentableBlockComment(comment: Comment): boolean {
1615
return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*');
1716
}
1817

19-
function printIndentableBlockComment(comment: Comment): Doc {
18+
export function printIndentableBlockComment(comment: BlockComment): Doc {
2019
const lines = comment.value.split('\n');
2120

2221
return join(
@@ -29,19 +28,11 @@ function printIndentableBlockComment(comment: Comment): Doc {
2928
);
3029
}
3130

32-
export function printComment(commentPath: AstPath<AstNode | Comment>): Doc {
31+
export function printComment(commentPath: AstPath<AstNode>): Doc {
3332
const comment = commentPath.getNode()!;
3433

35-
if (isLineComment(comment)) {
36-
return comment.value.trimEnd();
37-
}
38-
39-
if (isBlockComment(comment)) {
40-
if (isIndentableBlockComment(comment)) {
41-
return printIndentableBlockComment(comment);
42-
}
43-
44-
return join(literalline, comment.value.split('\n'));
34+
if (isComment(comment)) {
35+
return comment.print();
4536
}
4637

4738
throw new Error(`Not a comment: ${JSON.stringify(comment)}`);

src/slang-nodes/Identifier.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { TerminalNode } from '@nomicfoundation/slang/cst/index.js';
22
import { TerminalKind } from '@nomicfoundation/slang/kinds/index.js';
33

44
import type { Doc } from 'prettier';
5-
import type { AstLocation, Comment, SlangNode } from '../types';
5+
import type { Location, SlangNode } from '../types';
6+
import type { Comment } from '.';
67

78
export class Identifier implements SlangNode {
89
readonly kind = TerminalKind.Identifier;
910

1011
comments: Comment[];
1112

12-
loc: AstLocation;
13+
loc: Location;
1314

1415
value: string;
1516

@@ -19,9 +20,7 @@ export class Identifier implements SlangNode {
1920
this.comments = [];
2021
this.loc = {
2122
start: offset,
22-
end: offset + ast.textLength.utf16,
23-
leadingOffset: 0,
24-
trailingOffset: 0
23+
end: offset + ast.textLength.utf16
2524
};
2625
}
2726

src/slang-nodes/ModifierDefinition.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const objectConfig = {
1717
enumerable: true,
1818
configurable: true
1919
};
20+
2021
export class ModifierDefinition implements SlangNode {
2122
readonly kind = NonterminalKind.ModifierDefinition;
2223

src/slang-nodes/ModifierInvocation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ModifierInvocation implements SlangNode {
4646
this.arguments.variant.kind ===
4747
NonterminalKind.PositionalArgumentsDeclaration &&
4848
this.arguments.variant.arguments.items.length === 0 && // no arguments
49-
!ast.arguments!.variant.cst.children().some((child) => isComment(child)) // no comments
49+
!ast.arguments!.variant.cst.children().some((child) => isComment(child)) // no comments, at this point we need to check the CST
5050
) {
5151
this.arguments = undefined;
5252
}

0 commit comments

Comments
 (0)