diff --git a/src/slang-comments/handler.ts b/src/slang-comments/handler.ts index 9d5a0dc54..439250267 100644 --- a/src/slang-comments/handler.ts +++ b/src/slang-comments/handler.ts @@ -1,14 +1,13 @@ 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, - ast: AstNode, - isLastComment: boolean + text: string + // options: ParserOptions, + // ast: AstNode, + // isLastComment: boolean ): boolean { const { precedingNode, enclosingNode, followingNode } = comment; const handlerArguments = { @@ -16,54 +15,14 @@ function ownLine( precedingNode, enclosingNode, followingNode, - comment, - ast, - isLastComment + comment }; return handlers.some((handler) => handler(handlerArguments)); } -function endOfLine( - comment: Comment, - text: string, - options: ParserOptions, - 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, - 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 +}; diff --git a/src/slang-comments/handlers/handle-else-branch-comments.ts b/src/slang-comments/handlers/handle-else-branch-comments.ts index 4b8697beb..0352d941a 100644 --- a/src/slang-comments/handlers/handle-else-branch-comments.ts +++ b/src/slang-comments/handlers/handle-else-branch-comments.ts @@ -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; } diff --git a/src/slang-comments/handlers/handle-if-statement-comments.ts b/src/slang-comments/handlers/handle-if-statement-comments.ts index 9e76fa2b7..12e8fbe50 100644 --- a/src/slang-comments/handlers/handle-if-statement-comments.ts +++ b/src/slang-comments/handlers/handle-if-statement-comments.ts @@ -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; } @@ -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 { diff --git a/src/slang-comments/handlers/handle-struct-comments.ts b/src/slang-comments/handlers/handle-struct-definition-comments.ts similarity index 94% rename from src/slang-comments/handlers/handle-struct-comments.ts rename to src/slang-comments/handlers/handle-struct-definition-comments.ts index 90adc260a..bc1c5f228 100644 --- a/src/slang-comments/handlers/handle-struct-comments.ts +++ b/src/slang-comments/handlers/handle-struct-definition-comments.ts @@ -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, diff --git a/src/slang-comments/handlers/handle-while-statement-comments.ts b/src/slang-comments/handlers/handle-while-statement-comments.ts index 9c0065a16..98e511ff0 100644 --- a/src/slang-comments/handlers/handle-while-statement-comments.ts +++ b/src/slang-comments/handlers/handle-while-statement-comments.ts @@ -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; } diff --git a/src/slang-comments/handlers/index.ts b/src/slang-comments/handlers/index.ts index 901d0a767..bcc38ef69 100644 --- a/src/slang-comments/handlers/index.ts +++ b/src/slang-comments/handlers/index.ts @@ -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'; @@ -27,7 +27,7 @@ export default [ handlePositionalArgumentsDeclarationComments, handleSourceUnitMembersComments, handleStorageLayoutSpecifierComments, - handleStructComments, + handleStructDefinitionComments, handleWhileStatementComments, handleYulBlockComments ]; diff --git a/src/slang-nodes/SlangNode.ts b/src/slang-nodes/SlangNode.ts index 65fd95518..3b6350bab 100644 --- a/src/slang-nodes/SlangNode.ts +++ b/src/slang-nodes/SlangNode.ts @@ -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, @@ -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;