forked from prettier/angular-estree-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform-literal.ts
More file actions
41 lines (39 loc) · 1.03 KB
/
transform-literal.ts
File metadata and controls
41 lines (39 loc) · 1.03 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
import {
type LiteralPrimitive,
type RegularExpressionLiteral,
} from '@angular/compiler';
import type * as babel from '@babel/types';
export const visitLiteralPrimitive = (
node: LiteralPrimitive,
):
| babel.BooleanLiteral
| babel.NumericLiteral
| babel.NullLiteral
| babel.StringLiteral
| babel.Identifier => {
const { value } = node;
switch (typeof value) {
case 'boolean':
return { type: 'BooleanLiteral', value };
case 'number':
return { type: 'NumericLiteral', value };
case 'object':
return { type: 'NullLiteral' };
case 'string':
return { type: 'StringLiteral', value };
case 'undefined':
return { type: 'Identifier', name: 'undefined' };
/* c8 ignore next 4 */
default:
throw new Error(
`Unexpected 'LiteralPrimitive' value type ${typeof value}`,
);
}
};
export const visitRegularExpressionLiteral = (
node: RegularExpressionLiteral,
): babel.RegExpLiteral => ({
type: 'RegExpLiteral',
pattern: node.body,
flags: node.flags ?? '',
});