Skip to content

Commit 6e10f9c

Browse files
authored
mainly addressed the repetition of the comment handler and a small review of the handlers in general (#1469)
1 parent c1bc106 commit 6e10f9c

7 files changed

Lines changed: 21 additions & 65 deletions

File tree

src/slang-comments/handler.ts

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,28 @@
11
import handlers from './handlers/index.js';
22

3-
import type { ParserOptions } from 'prettier';
4-
import type { AstNode, Comment } from '../slang-nodes/types.d.ts';
3+
import type { Comment } from '../slang-nodes/types.d.ts';
54

6-
function ownLine(
5+
function handler(
76
comment: Comment,
8-
text: string,
9-
options: ParserOptions<AstNode>,
10-
ast: AstNode,
11-
isLastComment: boolean
7+
text: string
8+
// options: ParserOptions<AstNode>,
9+
// ast: AstNode,
10+
// isLastComment: boolean
1211
): boolean {
1312
const { precedingNode, enclosingNode, followingNode } = comment;
1413
const handlerArguments = {
1514
text,
1615
precedingNode,
1716
enclosingNode,
1817
followingNode,
19-
comment,
20-
ast,
21-
isLastComment
18+
comment
2219
};
2320

2421
return handlers.some((handler) => handler(handlerArguments));
2522
}
2623

27-
function endOfLine(
28-
comment: Comment,
29-
text: string,
30-
options: ParserOptions<AstNode>,
31-
ast: AstNode,
32-
isLastComment: boolean
33-
): boolean {
34-
const { precedingNode, enclosingNode, followingNode } = comment;
35-
const handlerArguments = {
36-
text,
37-
precedingNode,
38-
enclosingNode,
39-
followingNode,
40-
comment,
41-
ast,
42-
isLastComment
43-
};
44-
45-
return handlers.some((handler) => handler(handlerArguments));
46-
}
47-
48-
function remaining(
49-
comment: Comment,
50-
text: string,
51-
options: ParserOptions<AstNode>,
52-
ast: AstNode,
53-
isLastComment: boolean
54-
): boolean {
55-
const { precedingNode, enclosingNode, followingNode } = comment;
56-
const handlerArguments = {
57-
text,
58-
precedingNode,
59-
enclosingNode,
60-
followingNode,
61-
comment,
62-
ast,
63-
isLastComment
64-
};
65-
66-
return handlers.some((handler) => handler(handlerArguments));
67-
}
68-
69-
export const handleComments = { ownLine, endOfLine, remaining };
24+
export const handleComments = {
25+
ownLine: handler,
26+
endOfLine: handler,
27+
remaining: handler
28+
};

src/slang-comments/handlers/handle-else-branch-comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function handleElseBranchComments({
1111
followingNode,
1212
comment
1313
}: HandlerParams): boolean {
14-
if (enclosingNode?.kind !== NonterminalKind.ElseBranch || !followingNode) {
14+
if (enclosingNode?.kind !== NonterminalKind.ElseBranch) {
1515
return false;
1616
}
1717

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function handleIfStatementComments({
1414
followingNode,
1515
comment
1616
}: HandlerParams): boolean {
17-
if (enclosingNode?.kind !== NonterminalKind.IfStatement || !followingNode) {
17+
if (enclosingNode?.kind !== NonterminalKind.IfStatement) {
1818
return false;
1919
}
2020

@@ -43,7 +43,7 @@ export default function handleIfStatementComments({
4343
return true;
4444
}
4545

46-
if (followingNode.kind === NonterminalKind.IfStatement) {
46+
if (followingNode?.kind === NonterminalKind.IfStatement) {
4747
if (followingNode.body.kind === NonterminalKind.Block) {
4848
addCollectionFirstComment(followingNode.body.statements, comment);
4949
} else {

src/slang-comments/handlers/handle-struct-comments.ts renamed to src/slang-comments/handlers/handle-struct-definition-comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import addCollectionLastComment from './add-collection-last-comment.js';
66

77
import type { HandlerParams } from './types.d.ts';
88

9-
export default function handleStructComments({
9+
export default function handleStructDefinitionComments({
1010
text,
1111
precedingNode,
1212
enclosingNode,

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ export default function handleWhileStatementComments({
1414
followingNode,
1515
comment
1616
}: HandlerParams): boolean {
17-
if (
18-
enclosingNode?.kind !== NonterminalKind.WhileStatement ||
19-
!followingNode
20-
) {
17+
if (enclosingNode?.kind !== NonterminalKind.WhileStatement) {
2118
return false;
2219
}
2320

src/slang-comments/handlers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import handleParametersDeclarationComments from './handle-parameters-declaration
1010
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
1111
import handleSourceUnitMembersComments from './handle-source-unit-members-comments.js';
1212
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
13-
import handleStructComments from './handle-struct-comments.js';
13+
import handleStructDefinitionComments from './handle-struct-definition-comments.js';
1414
import handleWhileStatementComments from './handle-while-statement-comments.js';
1515
import handleYulBlockComments from './handle-yul-block-comments.js';
1616

@@ -27,7 +27,7 @@ export default [
2727
handlePositionalArgumentsDeclarationComments,
2828
handleSourceUnitMembersComments,
2929
handleStorageLayoutSpecifierComments,
30-
handleStructComments,
30+
handleStructDefinitionComments,
3131
handleWhileStatementComments,
3232
handleYulBlockComments
3333
];

src/slang-nodes/SlangNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export abstract class SlangNode {
4444
enclosePeripheralComments = false
4545
) {
4646
if (ast instanceof SlangTerminalNode) {
47-
const start = collected.offsets.get(ast.id) || 0;
47+
const start = collected.offsets.get(ast.id) ?? 0;
4848
const end = start + ast.textLength.utf16;
4949
this.loc = {
5050
outerStart: start,
@@ -56,7 +56,7 @@ export abstract class SlangNode {
5656
}
5757
const cst = ast.cst;
5858

59-
const initialOffset = collected.offsets.get(cst.id) || 0;
59+
const initialOffset = collected.offsets.get(cst.id) ?? 0;
6060
let offset = initialOffset;
6161
let triviaLength = 0;
6262
let leadingOffset;

0 commit comments

Comments
 (0)