forked from prettier/angular-estree-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform-binary-expression.ts
More file actions
48 lines (41 loc) · 1.2 KB
/
transform-binary-expression.ts
File metadata and controls
48 lines (41 loc) · 1.2 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
import { Binary } from '@angular/compiler';
import type * as babel from '@babel/types';
import { type Transformer } from './transform.ts';
const isAssignmentOperator = (
operator: Binary['operation'],
): operator is babel.AssignmentExpression['operator'] =>
Binary.isAssignmentOperation(operator);
const isLogicalOperator = (
operator: Binary['operation'],
): operator is babel.LogicalExpression['operator'] =>
operator === '&&' || operator === '||' || operator === '??';
export const visitBinary = (
node: Binary,
transformer: Transformer,
):
| babel.LogicalExpression
| babel.AssignmentExpression
| babel.BinaryExpression => {
const { operation: operator } = node;
const [left, right] = transformer.transformChildren<babel.Expression>([
node.left,
node.right,
]);
if (isLogicalOperator(operator)) {
return { type: 'LogicalExpression', operator, left, right };
}
if (isAssignmentOperator(operator)) {
return {
type: 'AssignmentExpression',
left: left as babel.MemberExpression,
right,
operator: operator,
};
}
return {
left,
right,
type: 'BinaryExpression',
operator: operator as babel.BinaryExpression['operator'],
};
};