-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathMemberAccessExpression.ts
More file actions
147 lines (131 loc) · 4.5 KB
/
MemberAccessExpression.ts
File metadata and controls
147 lines (131 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { isLabel } from '../slang-utils/is-label.js';
import { extractVariant } from '../slang-utils/extract-variant.js';
import { isChainableExpression } from '../slang-utils/is-chainable-expression.js';
import { memberAccessChainLabel } from '../slang-printers/print-member-access-chain-item.js';
import { SlangNode } from './SlangNode.js';
import { Expression } from './Expression.js';
import { TerminalNode } from './TerminalNode.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type { AstNode, ChainableExpression, StrictAstNode } from './types.d.ts';
const { group, indent, label, softline } = doc.builders;
const separatorLabel = Symbol('separator');
function isEndOfChain(
node: ChainableExpression,
path: AstPath<StrictAstNode>
): boolean {
for (let i = 1, current = node, parent; ; i++, current = parent) {
parent = path.getNode(i)!;
if (!isChainableExpression(parent)) break;
switch (parent.kind) {
case NonterminalKind.MemberAccessExpression:
// If `parent` is a MemberAccessExpression we are not at the end
// of the chain.
return false;
case NonterminalKind.IndexAccessExpression:
// If `parent` is an IndexAccessExpression and `current` is not
// the operand then it must be the start or the end in which case it is
// the end of the chain.
if (current !== parent.operand) return true;
break;
case NonterminalKind.FunctionCallExpression:
// If `parent` is a FunctionCallExpression and `current` is not
// the operand then it must be and argument in which case it is the end
// of the chain.
if (current !== parent.operand) return true;
break;
}
}
return true;
}
/**
* processChain expects the doc[] of the full chain of MemberAccess.
*
* It uses the separator label to split the chain into 2 arrays.
* The first array is the doc[] corresponding to the first element before the
* first separator.
* The second array contains the rest of the chain.
*
* The second array is grouped and indented, while the first element's
* formatting logic remains separated.
*
* That way the first element can safely split into multiple lines and the rest
* of the chain will continue its formatting rules as normal.
*
* i.e.
* ```
* functionCall(arg1, arg2).rest.of.chain
*
* functionCall(arg1, arg2)
* .long
* .rest
* .of
* .chain
*
* functionCall(
* arg1,
* arg2
* ).rest.of.chain
*
* functionCall(
* arg1,
* arg2
* )
* .long
* .rest
* .of
* .chain
* ```
*
* NOTE: As described in the examples above, the rest of the chain will be grouped
* and try to stay in the same line as the end of the first element.
*
* @param {doc[]} chain is the full chain of MemberAccess
* @returns a processed doc[] with the proper grouping and indentation ready to
* be printed.
*/
function processChain(chain: Doc[]): Doc {
const firstSeparatorIndex = chain.findIndex((element) =>
isLabel(element, separatorLabel)
);
// We wrap the expression in a label in case there is an IndexAccess or
// a FunctionCall following this MemberAccess.
return label(memberAccessChainLabel, [
// The doc[] before the first separator
chain.slice(0, firstSeparatorIndex),
// The doc[] containing the rest of the chain
group(indent(chain.slice(firstSeparatorIndex)))
]);
}
export class MemberAccessExpression extends SlangNode {
readonly kind = NonterminalKind.MemberAccessExpression;
operand: Expression['variant'];
member: TerminalNode;
constructor(
ast: ast.MemberAccessExpression,
collected: CollectedMetadata,
options: ParserOptions<AstNode>
) {
super(ast, collected);
this.operand = extractVariant(
new Expression(ast.operand, collected, options)
);
this.member = new TerminalNode(ast.member, collected);
this.updateMetadata(this.operand);
}
print(path: AstPath<MemberAccessExpression>, print: PrintFunction): Doc {
let operandDoc = path.call(print, 'operand');
if (Array.isArray(operandDoc)) {
operandDoc = operandDoc.flat();
}
const document = [
operandDoc,
label(separatorLabel, [softline, '.']),
path.call(print, 'member')
].flat();
return isEndOfChain(this, path) ? processChain(document) : document;
}
}