diff --git a/src/slang-nodes/ConditionalExpression.ts b/src/slang-nodes/ConditionalExpression.ts index 21b6eb432..b7a9ce575 100644 --- a/src/slang-nodes/ConditionalExpression.ts +++ b/src/slang-nodes/ConditionalExpression.ts @@ -141,12 +141,14 @@ export class ConditionalExpression extends SlangNode { // We can remove parentheses only because we are sure that the // `condition` must be a single `bool` value. const operandLoc = this.operand.loc; - for ( - let operandSingleExpression = getOperandSingleExpression(this.operand); - operandSingleExpression && - operandSingleExpression.kind !== NonterminalKind.ConditionalExpression; - operandSingleExpression = getOperandSingleExpression(this.operand) - ) { + for (let operandSingleExpression; ; ) { + operandSingleExpression = getOperandSingleExpression(this.operand); + if ( + operandSingleExpression === undefined || + operandSingleExpression.kind === NonterminalKind.ConditionalExpression + ) + break; + this.operand = operandSingleExpression; } this.operand.loc = operandLoc; diff --git a/src/slang-nodes/ModifierInvocation.ts b/src/slang-nodes/ModifierInvocation.ts index 7f7bc2a96..f0fee5fae 100644 --- a/src/slang-nodes/ModifierInvocation.ts +++ b/src/slang-nodes/ModifierInvocation.ts @@ -35,8 +35,7 @@ export class ModifierInvocation extends SlangNode { cleanModifierInvocationArguments(): void { if ( - this.arguments && - this.arguments.kind === NonterminalKind.PositionalArgumentsDeclaration && + this.arguments?.kind === NonterminalKind.PositionalArgumentsDeclaration && this.arguments.isEmpty ) { delete this.arguments; diff --git a/src/slang-printers/print-comments.ts b/src/slang-printers/print-comments.ts index d7cb0f1f2..95be8115c 100644 --- a/src/slang-printers/print-comments.ts +++ b/src/slang-printers/print-comments.ts @@ -1,8 +1,8 @@ import { doc, util } from 'prettier'; import { printComment } from '../slang-comments/printer.js'; import { joinExisting } from '../slang-utils/join-existing.js'; - import { locEnd } from '../slang-utils/loc.js'; + import type { AstPath, Doc, ParserOptions } from 'prettier'; import type { AstNode, @@ -21,8 +21,10 @@ export function printComments( path: AstPath, options: ParserOptions ): Doc[] { - if (node.comments === undefined) return []; - const lastPrintableIndex = node.comments.findLastIndex(isPrintable); + const lastPrintableIndex = (node.comments ?? []).findLastIndex(isPrintable); + if (lastPrintableIndex === -1) { + return []; + } return joinExisting( line, path.map(({ node: comment }, index) => {