Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 12 additions & 53 deletions src/slang-comments/handler.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,28 @@
import handlers from './handlers/index.js';

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

function ownLine(
function handler(
comment: Comment,
text: string,
options: ParserOptions<AstNode>,
ast: AstNode,
isLastComment: boolean
text: string
// options: ParserOptions<AstNode>,
// ast: AstNode,
// isLastComment: boolean
): boolean {
const { precedingNode, enclosingNode, followingNode } = comment;
const handlerArguments = {
text,
precedingNode,
enclosingNode,
followingNode,
comment,
ast,
isLastComment
comment
};

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

function endOfLine(
comment: Comment,
text: string,
options: ParserOptions<AstNode>,
ast: AstNode,
isLastComment: boolean
): boolean {
const { precedingNode, enclosingNode, followingNode } = comment;
const handlerArguments = {
text,
precedingNode,
enclosingNode,
followingNode,
comment,
ast,
isLastComment
};

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

function remaining(
comment: Comment,
text: string,
options: ParserOptions<AstNode>,
ast: AstNode,
isLastComment: boolean
): boolean {
const { precedingNode, enclosingNode, followingNode } = comment;
const handlerArguments = {
text,
precedingNode,
enclosingNode,
followingNode,
comment,
ast,
isLastComment
};

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

export const handleComments = { ownLine, endOfLine, remaining };
export const handleComments = {
ownLine: handler,
endOfLine: handler,
remaining: handler
};
2 changes: 1 addition & 1 deletion src/slang-comments/handlers/handle-else-branch-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function handleElseBranchComments({
followingNode,
comment
}: HandlerParams): boolean {
if (enclosingNode?.kind !== NonterminalKind.ElseBranch || !followingNode) {
if (enclosingNode?.kind !== NonterminalKind.ElseBranch) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/slang-comments/handlers/handle-if-statement-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function handleIfStatementComments({
followingNode,
comment
}: HandlerParams): boolean {
if (enclosingNode?.kind !== NonterminalKind.IfStatement || !followingNode) {
if (enclosingNode?.kind !== NonterminalKind.IfStatement) {
return false;
}

Expand Down Expand Up @@ -43,7 +43,7 @@ export default function handleIfStatementComments({
return true;
}

if (followingNode.kind === NonterminalKind.IfStatement) {
if (followingNode?.kind === NonterminalKind.IfStatement) {
if (followingNode.body.kind === NonterminalKind.Block) {
addCollectionFirstComment(followingNode.body.statements, comment);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import addCollectionLastComment from './add-collection-last-comment.js';

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

export default function handleStructComments({
export default function handleStructDefinitionComments({
text,
precedingNode,
enclosingNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export default function handleWhileStatementComments({
followingNode,
comment
}: HandlerParams): boolean {
if (
enclosingNode?.kind !== NonterminalKind.WhileStatement ||
!followingNode
) {
if (enclosingNode?.kind !== NonterminalKind.WhileStatement) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/slang-comments/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import handleParametersDeclarationComments from './handle-parameters-declaration
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
import handleSourceUnitMembersComments from './handle-source-unit-members-comments.js';
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
import handleStructComments from './handle-struct-comments.js';
import handleStructDefinitionComments from './handle-struct-definition-comments.js';
import handleWhileStatementComments from './handle-while-statement-comments.js';
import handleYulBlockComments from './handle-yul-block-comments.js';

Expand All @@ -27,7 +27,7 @@ export default [
handlePositionalArgumentsDeclarationComments,
handleSourceUnitMembersComments,
handleStorageLayoutSpecifierComments,
handleStructComments,
handleStructDefinitionComments,
handleWhileStatementComments,
handleYulBlockComments
];
4 changes: 2 additions & 2 deletions src/slang-nodes/SlangNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class SlangNode {
enclosePeripheralComments = false
) {
if (ast instanceof SlangTerminalNode) {
const start = collected.offsets.get(ast.id) || 0;
const start = collected.offsets.get(ast.id) ?? 0;
const end = start + ast.textLength.utf16;
this.loc = {
outerStart: start,
Expand All @@ -56,7 +56,7 @@ export abstract class SlangNode {
}
const cst = ast.cst;

const initialOffset = collected.offsets.get(cst.id) || 0;
const initialOffset = collected.offsets.get(cst.id) ?? 0;
let offset = initialOffset;
let triviaLength = 0;
let leadingOffset;
Expand Down