forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparenthesizerRules.ts
More file actions
693 lines (632 loc) · 32.6 KB
/
parenthesizerRules.ts
File metadata and controls
693 lines (632 loc) · 32.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import {
Associativity,
BinaryExpression,
BinaryOperator,
cast,
compareValues,
Comparison,
ConciseBody,
Expression,
getExpressionAssociativity,
getExpressionPrecedence,
getLeftmostExpression,
getOperatorAssociativity,
getOperatorPrecedence,
identity,
isBinaryExpression,
isBlock,
isCallExpression,
isCommaSequence,
isConditionalTypeNode,
isConstructorTypeNode,
isFunctionOrConstructorTypeNode,
isFunctionTypeNode,
isInferTypeNode,
isIntersectionTypeNode,
isJSDocNullableType,
isLeftHandSideExpression,
isLiteralKind,
isNamedTupleMember,
isNodeArray,
isOptionalChain,
isTypeOperatorNode,
isUnaryExpression,
isUnionTypeNode,
last,
LeftHandSideExpression,
NamedTupleMember,
NewExpression,
NodeArray,
NodeFactory,
OperatorPrecedence,
OuterExpressionKinds,
ParenthesizerRules,
sameMap,
setTextRange,
skipPartiallyEmittedExpressions,
some,
SyntaxKind,
TypeNode,
UnaryExpression,
} from "../_namespaces/ts.js";
/** @internal */
export function createParenthesizerRules(factory: NodeFactory): ParenthesizerRules {
interface BinaryPlusExpression extends BinaryExpression {
cachedLiteralKind: SyntaxKind;
}
let binaryLeftOperandParenthesizerCache: Map<BinaryOperator, (node: Expression) => Expression> | undefined;
let binaryRightOperandParenthesizerCache: Map<BinaryOperator, (node: Expression) => Expression> | undefined;
return {
getParenthesizeLeftSideOfBinaryForOperator,
getParenthesizeRightSideOfBinaryForOperator,
parenthesizeLeftSideOfBinary,
parenthesizeRightSideOfBinary,
parenthesizeExpressionOfComputedPropertyName,
parenthesizeConditionOfConditionalExpression,
parenthesizeBranchOfConditionalExpression,
parenthesizeExpressionOfExportDefault,
parenthesizeExpressionOfNew,
parenthesizeLeftSideOfAccess,
parenthesizeOperandOfPostfixUnary,
parenthesizeOperandOfPrefixUnary,
parenthesizeExpressionsOfCommaDelimitedList,
parenthesizeExpressionForDisallowedComma,
parenthesizeExpressionOfExpressionStatement,
parenthesizeConciseBodyOfArrowFunction,
parenthesizeCheckTypeOfConditionalType,
parenthesizeExtendsTypeOfConditionalType,
parenthesizeConstituentTypesOfUnionType,
parenthesizeConstituentTypeOfUnionType,
parenthesizeConstituentTypesOfIntersectionType,
parenthesizeConstituentTypeOfIntersectionType,
parenthesizeOperandOfTypeOperator,
parenthesizeOperandOfReadonlyTypeOperator,
parenthesizeNonArrayTypeOfPostfixType,
parenthesizeElementTypesOfTupleType,
parenthesizeElementTypeOfTupleType,
parenthesizeTypeOfOptionalType,
parenthesizeTypeArguments,
parenthesizeLeadingTypeArgument,
};
function getParenthesizeLeftSideOfBinaryForOperator(operatorKind: BinaryOperator) {
binaryLeftOperandParenthesizerCache ||= new Map();
let parenthesizerRule = binaryLeftOperandParenthesizerCache.get(operatorKind);
if (!parenthesizerRule) {
parenthesizerRule = node => parenthesizeLeftSideOfBinary(operatorKind, node);
binaryLeftOperandParenthesizerCache.set(operatorKind, parenthesizerRule);
}
return parenthesizerRule;
}
function getParenthesizeRightSideOfBinaryForOperator(operatorKind: BinaryOperator) {
binaryRightOperandParenthesizerCache ||= new Map();
let parenthesizerRule = binaryRightOperandParenthesizerCache.get(operatorKind);
if (!parenthesizerRule) {
parenthesizerRule = node => parenthesizeRightSideOfBinary(operatorKind, /*leftSide*/ undefined, node);
binaryRightOperandParenthesizerCache.set(operatorKind, parenthesizerRule);
}
return parenthesizerRule;
}
/**
* Determines whether the operand to a BinaryExpression needs to be parenthesized.
*
* @param binaryOperator The operator for the BinaryExpression.
* @param operand The operand for the BinaryExpression.
* @param isLeftSideOfBinary A value indicating whether the operand is the left side of the
* BinaryExpression.
*/
function binaryOperandNeedsParentheses(binaryOperator: SyntaxKind, operand: Expression, isLeftSideOfBinary: boolean, leftOperand: Expression | undefined) {
// If the operand has lower precedence, then it needs to be parenthesized to preserve the
// intent of the expression. For example, if the operand is `a + b` and the operator is
// `*`, then we need to parenthesize the operand to preserve the intended order of
// operations: `(a + b) * x`.
//
// If the operand has higher precedence, then it does not need to be parenthesized. For
// example, if the operand is `a * b` and the operator is `+`, then we do not need to
// parenthesize to preserve the intended order of operations: `a * b + x`.
//
// If the operand has the same precedence, then we need to check the associativity of
// the operator based on whether this is the left or right operand of the expression.
//
// For example, if `a / d` is on the right of operator `*`, we need to parenthesize
// to preserve the intended order of operations: `x * (a / d)`
//
// If `a ** d` is on the left of operator `**`, we need to parenthesize to preserve
// the intended order of operations: `(a ** b) ** c`
const binaryOperatorPrecedence = getOperatorPrecedence(SyntaxKind.BinaryExpression, binaryOperator);
const binaryOperatorAssociativity = getOperatorAssociativity(SyntaxKind.BinaryExpression, binaryOperator);
const emittedOperand = skipPartiallyEmittedExpressions(operand);
if (!isLeftSideOfBinary && operand.kind === SyntaxKind.ArrowFunction && binaryOperatorPrecedence > OperatorPrecedence.Assignment) {
// We need to parenthesize arrow functions on the right side to avoid it being
// parsed as parenthesized expression: `a && (() => {})`
return true;
}
const operandPrecedence = getExpressionPrecedence(emittedOperand);
switch (compareValues(operandPrecedence, binaryOperatorPrecedence)) {
case Comparison.LessThan:
// If the operand is the right side of a right-associative binary operation
// and is a yield expression, then we do not need parentheses.
if (
!isLeftSideOfBinary
&& binaryOperatorAssociativity === Associativity.Right
&& operand.kind === SyntaxKind.YieldExpression
) {
return false;
}
return true;
case Comparison.GreaterThan:
return false;
case Comparison.EqualTo:
if (isLeftSideOfBinary) {
// No need to parenthesize the left operand when the binary operator is
// left associative:
// (a*b)/x -> a*b/x
// (a**b)/x -> a**b/x
//
// Parentheses are needed for the left operand when the binary operator is
// right associative:
// (a/b)**x -> (a/b)**x
// (a**b)**x -> (a**b)**x
return binaryOperatorAssociativity === Associativity.Right;
}
else {
if (
isBinaryExpression(emittedOperand)
&& emittedOperand.operatorToken.kind === binaryOperator
) {
// No need to parenthesize the right operand when the binary operator and
// operand are the same and one of the following:
// x*(a*b) => x*a*b
// x|(a|b) => x|a|b
// x&(a&b) => x&a&b
// x^(a^b) => x^a^b
if (operatorHasAssociativeProperty(binaryOperator)) {
return false;
}
// No need to parenthesize the right operand when the binary operator
// is plus (+) if both the left and right operands consist solely of either
// literals of the same kind or binary plus (+) expressions for literals of
// the same kind (recursively).
// "a"+(1+2) => "a"+(1+2)
// "a"+("b"+"c") => "a"+"b"+"c"
if (binaryOperator === SyntaxKind.PlusToken) {
const leftKind = leftOperand ? getLiteralKindOfBinaryPlusOperand(leftOperand) : SyntaxKind.Unknown;
if (isLiteralKind(leftKind) && leftKind === getLiteralKindOfBinaryPlusOperand(emittedOperand)) {
return false;
}
}
}
// No need to parenthesize the right operand when the operand is right
// associative:
// x/(a**b) -> x/a**b
// x**(a**b) -> x**a**b
//
// Parentheses are needed for the right operand when the operand is left
// associative:
// x/(a*b) -> x/(a*b)
// x**(a/b) -> x**(a/b)
const operandAssociativity = getExpressionAssociativity(emittedOperand);
return operandAssociativity === Associativity.Left;
}
}
}
/**
* Determines whether a binary operator is mathematically associative.
*
* @param binaryOperator The binary operator.
*/
function operatorHasAssociativeProperty(binaryOperator: SyntaxKind) {
// The following operators are associative in JavaScript:
// (a*b)*c -> a*(b*c) -> a*b*c
// (a|b)|c -> a|(b|c) -> a|b|c
// (a&b)&c -> a&(b&c) -> a&b&c
// (a^b)^c -> a^(b^c) -> a^b^c
// (a,b),c -> a,(b,c) -> a,b,c
//
// While addition is associative in mathematics, JavaScript's `+` is not
// guaranteed to be associative as it is overloaded with string concatenation.
return binaryOperator === SyntaxKind.AsteriskToken
|| binaryOperator === SyntaxKind.BarToken
|| binaryOperator === SyntaxKind.AmpersandToken
|| binaryOperator === SyntaxKind.CaretToken
|| binaryOperator === SyntaxKind.CommaToken;
}
/**
* This function determines whether an expression consists of a homogeneous set of
* literal expressions or binary plus expressions that all share the same literal kind.
* It is used to determine whether the right-hand operand of a binary plus expression can be
* emitted without parentheses.
*/
function getLiteralKindOfBinaryPlusOperand(node: Expression): SyntaxKind {
node = skipPartiallyEmittedExpressions(node);
if (isLiteralKind(node.kind)) {
return node.kind;
}
if (node.kind === SyntaxKind.BinaryExpression && (node as BinaryExpression).operatorToken.kind === SyntaxKind.PlusToken) {
if ((node as BinaryPlusExpression).cachedLiteralKind !== undefined) {
return (node as BinaryPlusExpression).cachedLiteralKind;
}
const leftKind = getLiteralKindOfBinaryPlusOperand((node as BinaryExpression).left);
const literalKind = isLiteralKind(leftKind)
&& leftKind === getLiteralKindOfBinaryPlusOperand((node as BinaryExpression).right)
? leftKind
: SyntaxKind.Unknown;
(node as BinaryPlusExpression).cachedLiteralKind = literalKind;
return literalKind;
}
return SyntaxKind.Unknown;
}
/**
* Wraps the operand to a BinaryExpression in parentheses if they are needed to preserve the intended
* order of operations.
*
* @param binaryOperator The operator for the BinaryExpression.
* @param operand The operand for the BinaryExpression.
* @param isLeftSideOfBinary A value indicating whether the operand is the left side of the
* BinaryExpression.
*/
function parenthesizeBinaryOperand(binaryOperator: SyntaxKind, operand: Expression, isLeftSideOfBinary: boolean, leftOperand?: Expression) {
const skipped = skipPartiallyEmittedExpressions(operand);
// If the resulting expression is already parenthesized, we do not need to do any further processing.
if (skipped.kind === SyntaxKind.ParenthesizedExpression) {
return operand;
}
return binaryOperandNeedsParentheses(binaryOperator, operand, isLeftSideOfBinary, leftOperand)
? factory.createParenthesizedExpression(operand)
: operand;
}
function parenthesizeLeftSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression): Expression {
return parenthesizeBinaryOperand(binaryOperator, leftSide, /*isLeftSideOfBinary*/ true);
}
function parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression | undefined, rightSide: Expression): Expression {
return parenthesizeBinaryOperand(binaryOperator, rightSide, /*isLeftSideOfBinary*/ false, leftSide);
}
function parenthesizeExpressionOfComputedPropertyName(expression: Expression): Expression {
return isCommaSequence(expression) ? factory.createParenthesizedExpression(expression) : expression;
}
function parenthesizeConditionOfConditionalExpression(condition: Expression): Expression {
const conditionalPrecedence = getOperatorPrecedence(SyntaxKind.ConditionalExpression, SyntaxKind.QuestionToken);
const emittedCondition = skipPartiallyEmittedExpressions(condition);
const conditionPrecedence = getExpressionPrecedence(emittedCondition);
if (compareValues(conditionPrecedence, conditionalPrecedence) !== Comparison.GreaterThan) {
return factory.createParenthesizedExpression(condition);
}
return condition;
}
function parenthesizeBranchOfConditionalExpression(branch: Expression): Expression {
// per ES grammar both 'whenTrue' and 'whenFalse' parts of conditional expression are assignment expressions
// so in case when comma expression is introduced as a part of previous transformations
// if should be wrapped in parens since comma operator has the lowest precedence
const emittedExpression = skipPartiallyEmittedExpressions(branch);
return isCommaSequence(emittedExpression)
? factory.createParenthesizedExpression(branch)
: branch;
}
/**
* [Per the spec](https://tc39.github.io/ecma262/#prod-ExportDeclaration), `export default` accepts _AssigmentExpression_ but
* has a lookahead restriction for `function`, `async function`, and `class`.
*
* Basically, that means we need to parenthesize in the following cases:
*
* - BinaryExpression of CommaToken
* - CommaList (synthetic list of multiple comma expressions)
* - FunctionExpression
* - ClassExpression
*/
function parenthesizeExpressionOfExportDefault(expression: Expression): Expression {
const check = skipPartiallyEmittedExpressions(expression);
let needsParens = isCommaSequence(check);
if (!needsParens) {
switch (getLeftmostExpression(check, /*stopAtCallExpressions*/ false).kind) {
case SyntaxKind.ClassExpression:
case SyntaxKind.FunctionExpression:
needsParens = true;
}
}
return needsParens ? factory.createParenthesizedExpression(expression) : expression;
}
/**
* Wraps an expression in parentheses if it is needed in order to use the expression
* as the expression of a `NewExpression` node.
*/
function parenthesizeExpressionOfNew(expression: Expression): LeftHandSideExpression {
const leftmostExpr = getLeftmostExpression(expression, /*stopAtCallExpressions*/ true);
switch (leftmostExpr.kind) {
case SyntaxKind.CallExpression:
return factory.createParenthesizedExpression(expression);
case SyntaxKind.NewExpression:
return !(leftmostExpr as NewExpression).arguments
? factory.createParenthesizedExpression(expression)
: expression as LeftHandSideExpression; // TODO(rbuckton): Verify this assertion holds
}
return parenthesizeLeftSideOfAccess(expression);
}
/**
* Wraps an expression in parentheses if it is needed in order to use the expression for
* property or element access.
*/
function parenthesizeLeftSideOfAccess(expression: Expression, optionalChain?: boolean): LeftHandSideExpression {
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
// to parenthesize the expression before a dot. The known exception is:
//
// NewExpression:
// new C.x -> not the same as (new C).x
//
const emittedExpression = skipPartiallyEmittedExpressions(expression);
if (
isLeftHandSideExpression(emittedExpression)
&& (emittedExpression.kind !== SyntaxKind.NewExpression || (emittedExpression as NewExpression).arguments)
&& (optionalChain || !isOptionalChain(emittedExpression))
) {
// TODO(rbuckton): Verify whether this assertion holds.
return expression as LeftHandSideExpression;
}
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
return setTextRange(factory.createParenthesizedExpression(expression), expression);
}
function parenthesizeOperandOfPostfixUnary(operand: Expression): LeftHandSideExpression {
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
return isLeftHandSideExpression(operand) ? operand : setTextRange(factory.createParenthesizedExpression(operand), operand);
}
function parenthesizeOperandOfPrefixUnary(operand: Expression): UnaryExpression {
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
return isUnaryExpression(operand) ? operand : setTextRange(factory.createParenthesizedExpression(operand), operand);
}
function parenthesizeExpressionsOfCommaDelimitedList(elements: NodeArray<Expression>): NodeArray<Expression> {
const result = sameMap(elements, parenthesizeExpressionForDisallowedComma);
return setTextRange(factory.createNodeArray(result, elements.hasTrailingComma), elements);
}
function parenthesizeExpressionForDisallowedComma(expression: Expression): Expression {
const emittedExpression = skipPartiallyEmittedExpressions(expression);
const expressionPrecedence = getExpressionPrecedence(emittedExpression);
const commaPrecedence = getOperatorPrecedence(SyntaxKind.BinaryExpression, SyntaxKind.CommaToken);
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
return expressionPrecedence > commaPrecedence ? expression : setTextRange(factory.createParenthesizedExpression(expression), expression);
}
function parenthesizeExpressionOfExpressionStatement(expression: Expression): Expression {
const emittedExpression = skipPartiallyEmittedExpressions(expression);
if (isCallExpression(emittedExpression)) {
const callee = emittedExpression.expression;
const kind = skipPartiallyEmittedExpressions(callee).kind;
if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) {
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
const updated = factory.updateCallExpression(
emittedExpression,
setTextRange(factory.createParenthesizedExpression(callee), callee),
emittedExpression.typeArguments,
emittedExpression.arguments,
);
return factory.restoreOuterExpressions(expression, updated, OuterExpressionKinds.PartiallyEmittedExpressions);
}
}
const leftmostExpressionKind = getLeftmostExpression(emittedExpression, /*stopAtCallExpressions*/ false).kind;
if (leftmostExpressionKind === SyntaxKind.ObjectLiteralExpression || leftmostExpressionKind === SyntaxKind.FunctionExpression) {
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
return setTextRange(factory.createParenthesizedExpression(expression), expression);
}
return expression;
}
function parenthesizeConciseBodyOfArrowFunction(body: Expression): Expression;
function parenthesizeConciseBodyOfArrowFunction(body: ConciseBody): ConciseBody;
function parenthesizeConciseBodyOfArrowFunction(body: ConciseBody): ConciseBody {
if (!isBlock(body) && (isCommaSequence(body) || getLeftmostExpression(body, /*stopAtCallExpressions*/ false).kind === SyntaxKind.ObjectLiteralExpression)) {
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
return setTextRange(factory.createParenthesizedExpression(body), body);
}
return body;
}
// Type[Extends] :
// FunctionOrConstructorType
// ConditionalType[?Extends]
// ConditionalType[Extends] :
// UnionType[?Extends]
// [~Extends] UnionType[~Extends] `extends` Type[+Extends] `?` Type[~Extends] `:` Type[~Extends]
//
// - The check type (the `UnionType`, above) does not allow function, constructor, or conditional types (they must be parenthesized)
// - The extends type (the first `Type`, above) does not allow conditional types (they must be parenthesized). Function and constructor types are fine.
// - The true and false branch types (the second and third `Type` non-terminals, above) allow any type
function parenthesizeCheckTypeOfConditionalType(checkType: TypeNode): TypeNode {
switch (checkType.kind) {
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructorType:
case SyntaxKind.ConditionalType:
return factory.createParenthesizedType(checkType);
}
return checkType;
}
function parenthesizeExtendsTypeOfConditionalType(extendsType: TypeNode): TypeNode {
switch (extendsType.kind) {
case SyntaxKind.ConditionalType:
return factory.createParenthesizedType(extendsType);
}
return extendsType;
}
// UnionType[Extends] :
// `|`? IntersectionType[?Extends]
// UnionType[?Extends] `|` IntersectionType[?Extends]
//
// - A union type constituent has the same precedence as the check type of a conditional type
function parenthesizeConstituentTypeOfUnionType(type: TypeNode) {
switch (type.kind) {
case SyntaxKind.UnionType: // Not strictly necessary, but a union containing a union should have been flattened
case SyntaxKind.IntersectionType: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
return factory.createParenthesizedType(type);
}
return parenthesizeCheckTypeOfConditionalType(type);
}
function parenthesizeConstituentTypesOfUnionType(members: readonly TypeNode[]): NodeArray<TypeNode> {
return factory.createNodeArray(sameMap(members, parenthesizeConstituentTypeOfUnionType));
}
// IntersectionType[Extends] :
// `&`? TypeOperator[?Extends]
// IntersectionType[?Extends] `&` TypeOperator[?Extends]
//
// - An intersection type constituent does not allow function, constructor, conditional, or union types (they must be parenthesized)
function parenthesizeConstituentTypeOfIntersectionType(type: TypeNode) {
switch (type.kind) {
case SyntaxKind.UnionType:
case SyntaxKind.IntersectionType: // Not strictly necessary, but an intersection containing an intersection should have been flattened
return factory.createParenthesizedType(type);
}
return parenthesizeConstituentTypeOfUnionType(type);
}
function parenthesizeConstituentTypesOfIntersectionType(members: readonly TypeNode[]): NodeArray<TypeNode> {
return factory.createNodeArray(sameMap(members, parenthesizeConstituentTypeOfIntersectionType));
}
// TypeOperator[Extends] :
// PostfixType
// InferType[?Extends]
// `keyof` TypeOperator[?Extends]
// `unique` TypeOperator[?Extends]
// `readonly` TypeOperator[?Extends]
//
function parenthesizeOperandOfTypeOperator(type: TypeNode) {
switch (type.kind) {
case SyntaxKind.IntersectionType:
return factory.createParenthesizedType(type);
}
return parenthesizeConstituentTypeOfIntersectionType(type);
}
function parenthesizeOperandOfReadonlyTypeOperator(type: TypeNode) {
switch (type.kind) {
case SyntaxKind.TypeOperator:
return factory.createParenthesizedType(type);
}
return parenthesizeOperandOfTypeOperator(type);
}
// PostfixType :
// NonArrayType
// NonArrayType [no LineTerminator here] `!` // JSDoc
// NonArrayType [no LineTerminator here] `?` // JSDoc
// IndexedAccessType
// ArrayType
//
// IndexedAccessType :
// NonArrayType `[` Type[~Extends] `]`
//
// ArrayType :
// NonArrayType `[` `]`
//
function parenthesizeNonArrayTypeOfPostfixType(type: TypeNode) {
switch (type.kind) {
case SyntaxKind.InferType:
case SyntaxKind.TypeOperator:
case SyntaxKind.TypeQuery: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
return factory.createParenthesizedType(type);
}
return parenthesizeOperandOfTypeOperator(type);
}
// TupleType :
// `[` Elision? `]`
// `[` NamedTupleElementTypes `]`
// `[` NamedTupleElementTypes `,` Elision? `]`
// `[` TupleElementTypes `]`
// `[` TupleElementTypes `,` Elision? `]`
//
// NamedTupleElementTypes :
// Elision? NamedTupleMember
// NamedTupleElementTypes `,` Elision? NamedTupleMember
//
// NamedTupleMember :
// Identifier `?`? `:` Type[~Extends]
// `...` Identifier `:` Type[~Extends]
//
// TupleElementTypes :
// Elision? TupleElementType
// TupleElementTypes `,` Elision? TupleElementType
//
// TupleElementType :
// Type[~Extends] // NOTE: Needs cover grammar to disallow JSDoc postfix-optional
// OptionalType
// RestType
//
// OptionalType :
// Type[~Extends] `?` // NOTE: Needs cover grammar to disallow JSDoc postfix-optional
//
// RestType :
// `...` Type[~Extends]
//
function parenthesizeElementTypesOfTupleType(types: readonly (TypeNode | NamedTupleMember)[]): NodeArray<TypeNode> {
return factory.createNodeArray(sameMap(types, parenthesizeElementTypeOfTupleType));
}
function parenthesizeElementTypeOfTupleType(type: TypeNode | NamedTupleMember): TypeNode {
if (hasJSDocPostfixQuestion(type)) return factory.createParenthesizedType(type);
return type;
}
function hasJSDocPostfixQuestion(type: TypeNode | NamedTupleMember): boolean {
if (isJSDocNullableType(type)) return type.postfix;
if (isNamedTupleMember(type)) return hasJSDocPostfixQuestion(type.type);
if (isFunctionTypeNode(type) || isConstructorTypeNode(type) || isTypeOperatorNode(type)) return hasJSDocPostfixQuestion(type.type);
if (isConditionalTypeNode(type)) return hasJSDocPostfixQuestion(type.falseType);
if (isUnionTypeNode(type)) return hasJSDocPostfixQuestion(last(type.types));
if (isIntersectionTypeNode(type)) return hasJSDocPostfixQuestion(last(type.types));
if (isInferTypeNode(type)) return !!type.typeParameter.constraint && hasJSDocPostfixQuestion(type.typeParameter.constraint);
return false;
}
function parenthesizeTypeOfOptionalType(type: TypeNode): TypeNode {
if (hasJSDocPostfixQuestion(type)) return factory.createParenthesizedType(type);
return parenthesizeNonArrayTypeOfPostfixType(type);
}
// function parenthesizeMemberOfElementType(member: TypeNode): TypeNode {
// switch (member.kind) {
// case SyntaxKind.UnionType:
// case SyntaxKind.IntersectionType:
// case SyntaxKind.FunctionType:
// case SyntaxKind.ConstructorType:
// return factory.createParenthesizedType(member);
// }
// return parenthesizeMemberOfConditionalType(member);
// }
// function parenthesizeElementTypeOfArrayType(member: TypeNode): TypeNode {
// switch (member.kind) {
// case SyntaxKind.TypeQuery:
// case SyntaxKind.TypeOperator:
// case SyntaxKind.InferType:
// return factory.createParenthesizedType(member);
// }
// return parenthesizeMemberOfElementType(member);
// }
function parenthesizeLeadingTypeArgument(node: TypeNode) {
return isFunctionOrConstructorTypeNode(node) && node.typeParameters ? factory.createParenthesizedType(node) : node;
}
function parenthesizeOrdinalTypeArgument(node: TypeNode, i: number) {
return i === 0 ? parenthesizeLeadingTypeArgument(node) : node;
}
function parenthesizeTypeArguments(typeArguments: NodeArray<TypeNode> | undefined): NodeArray<TypeNode> | undefined {
if (some(typeArguments)) {
return factory.createNodeArray(sameMap(typeArguments, parenthesizeOrdinalTypeArgument));
}
}
}
/** @internal */
export const nullParenthesizerRules: ParenthesizerRules = {
getParenthesizeLeftSideOfBinaryForOperator: _ => identity,
getParenthesizeRightSideOfBinaryForOperator: _ => identity,
parenthesizeLeftSideOfBinary: (_binaryOperator, leftSide) => leftSide,
parenthesizeRightSideOfBinary: (_binaryOperator, _leftSide, rightSide) => rightSide,
parenthesizeExpressionOfComputedPropertyName: identity,
parenthesizeConditionOfConditionalExpression: identity,
parenthesizeBranchOfConditionalExpression: identity,
parenthesizeExpressionOfExportDefault: identity,
parenthesizeExpressionOfNew: expression => cast(expression, isLeftHandSideExpression),
parenthesizeLeftSideOfAccess: expression => cast(expression, isLeftHandSideExpression),
parenthesizeOperandOfPostfixUnary: operand => cast(operand, isLeftHandSideExpression),
parenthesizeOperandOfPrefixUnary: operand => cast(operand, isUnaryExpression),
parenthesizeExpressionsOfCommaDelimitedList: nodes => cast(nodes, isNodeArray),
parenthesizeExpressionForDisallowedComma: identity,
parenthesizeExpressionOfExpressionStatement: identity,
parenthesizeConciseBodyOfArrowFunction: identity,
parenthesizeCheckTypeOfConditionalType: identity,
parenthesizeExtendsTypeOfConditionalType: identity,
parenthesizeConstituentTypesOfUnionType: nodes => cast(nodes, isNodeArray),
parenthesizeConstituentTypeOfUnionType: identity,
parenthesizeConstituentTypesOfIntersectionType: nodes => cast(nodes, isNodeArray),
parenthesizeConstituentTypeOfIntersectionType: identity,
parenthesizeOperandOfTypeOperator: identity,
parenthesizeOperandOfReadonlyTypeOperator: identity,
parenthesizeNonArrayTypeOfPostfixType: identity,
parenthesizeElementTypesOfTupleType: nodes => cast(nodes, isNodeArray),
parenthesizeElementTypeOfTupleType: identity,
parenthesizeTypeOfOptionalType: identity,
parenthesizeTypeArguments: nodes => nodes && cast(nodes, isNodeArray),
parenthesizeLeadingTypeArgument: identity,
};