@@ -3,6 +3,7 @@ import { doc } from 'prettier';
33import { isLabel } from '../slang-utils/is-label.js' ;
44import { extractVariant } from '../slang-utils/extract-variant.js' ;
55import { isChainableExpression } from '../slang-utils/is-chainable-expression.js' ;
6+ import { memberAccessChainLabel } from '../slang-printers/print-member-access-chain-item.js' ;
67import { SlangNode } from './SlangNode.js' ;
78import { Expression } from './Expression.js' ;
89import { TerminalNode } from './TerminalNode.js' ;
@@ -14,6 +15,8 @@ import type { AstNode, ChainableExpression, StrictAstNode } from './types.d.ts';
1415
1516const { group, indent, label, softline } = doc . builders ;
1617
18+ const separatorLabel = Symbol ( 'separator' ) ;
19+
1720function isEndOfChain (
1821 node : ChainableExpression ,
1922 path : AstPath < StrictAstNode >
@@ -91,13 +94,13 @@ function isEndOfChain(
9194 * be printed.
9295 */
9396function processChain ( chain : Doc [ ] ) : Doc {
94- const firstSeparatorIndex = chain . findIndex (
95- ( element ) => isLabel ( element ) && element . label === 'separator'
97+ const firstSeparatorIndex = chain . findIndex ( ( element ) =>
98+ isLabel ( element , separatorLabel )
9699 ) ;
97100
98101 // We wrap the expression in a label in case there is an IndexAccess or
99102 // a FunctionCall following this MemberAccess.
100- return label ( 'MemberAccessChain' , [
103+ return label ( memberAccessChainLabel , [
101104 // The doc[] before the first separator
102105 chain . slice ( 0 , firstSeparatorIndex ) ,
103106 // The doc[] containing the rest of the chain
@@ -128,9 +131,14 @@ export class MemberAccessExpression extends SlangNode {
128131 }
129132
130133 print ( path : AstPath < MemberAccessExpression > , print : PrintFunction ) : Doc {
134+ let operandDoc = path . call ( print , 'operand' ) ;
135+ if ( Array . isArray ( operandDoc ) ) {
136+ operandDoc = operandDoc . flat ( ) ;
137+ }
138+
131139 const document = [
132- path . call ( print , 'operand' ) ,
133- label ( 'separator' , [ softline , '.' ] ) ,
140+ operandDoc ,
141+ label ( separatorLabel , [ softline , '.' ] ) ,
134142 path . call ( print , 'member' )
135143 ] . flat ( ) ;
136144
0 commit comments