-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
literal.js
62 lines (51 loc) · 1.51 KB
/
literal.js
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
"use strict";
const { printString, printNumber } = require("../../common/util.js");
function printLiteral(path, options /*, print*/) {
const node = path.getNode();
switch (node.type) {
case "RegExpLiteral": // Babel 6 Literal split
return printRegex(node);
case "BigIntLiteral":
// babel: node.extra.raw, flow: node.bigint
return printBigInt(node.bigint || node.extra.raw);
case "NumericLiteral": // Babel 6 Literal split
return printNumber(node.extra.raw);
case "StringLiteral": // Babel 6 Literal split
return printString(node.extra.raw, options);
case "NullLiteral": // Babel 6 Literal split
return "null";
case "BooleanLiteral": // Babel 6 Literal split
return String(node.value);
case "DecimalLiteral":
return printNumber(node.value) + "m";
case "Literal": {
if (node.regex) {
return printRegex(node.regex);
}
if (node.bigint) {
return printBigInt(node.raw);
}
if (node.decimal) {
return printNumber(node.decimal) + "m";
}
const { value } = node;
if (typeof value === "number") {
return printNumber(node.raw);
}
if (typeof value === "string") {
return printString(node.raw, options);
}
return String(value);
}
}
}
function printBigInt(raw) {
return raw.toLowerCase();
}
function printRegex({ pattern, flags }) {
flags = [...flags].sort().join("");
return `/${pattern}/${flags}`;
}
module.exports = {
printLiteral,
};