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
38 lines (32 loc) · 1.02 KB
/
transform-binary-expression.ts
File metadata and controls
38 lines (32 loc) · 1.02 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
import { Binary } from '@angular/compiler';
import type * as babel from '@babel/types';
import { type NodeTransformer } from '../node-transformer.ts';
const isLogicalOperator = (
operator: Binary['operation'],
): operator is babel.LogicalExpression['operator'] =>
operator === '&&' || operator === '||' || operator === '??';
export const visitBinary = (
node: Binary,
transformer: NodeTransformer,
):
| 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 (Binary.isAssignmentOperation(operator)) {
return {
type: 'AssignmentExpression',
left: left as babel.AssignmentExpression['left'],
right,
operator: operator,
};
}
return { left, right, type: 'BinaryExpression', operator: operator };
};