Repository navigation
Expand file tree
/
Copy pathexample.cjs
More file actions
110 lines (108 loc) · 3.06 KB
/
Copy pathexample.cjs
File metadata and controls
110 lines (108 loc) · 3.06 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
const code = `const element : React.Element = <a href="https://www.google.com">Hello, world!</a>;`;
let parse = require("@babel/parser").parse;
let ast = parse(code, {
// parse in strict mode and allow module declarations
sourceType: "module",
plugins: [
// enable jsx and flow syntax
"jsx",
"flow",
],
});
const skip = (key, value) => {
if (key === "loc" || key === "start" || key === "end" || key === "directives" || key === "comments") {
return undefined;
}
return value;
}
console.log(JSON.stringify(ast, skip, 2));
/* Execution:
➜ babel-learning git:(main) node src/parser/example.cjs
{
"type": "File",
"errors": [],
"program": {
"type": "Program",
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "element",
"typeAnnotation": {
"type": "TypeAnnotation",
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"typeParameters": null,
"id": {
"type": "QualifiedTypeIdentifier",
"qualification": {
"type": "Identifier",
"name": "React"
},
"id": {
"type": "Identifier",
"name": "Element"
}
}
}
}
},
"init": {
"type": "JSXElement",
"openingElement": {
"type": "JSXOpeningElement",
"name": {
"type": "JSXIdentifier",
"name": "a"
},
"attributes": [
{
"type": "JSXAttribute",
"name": {
"type": "JSXIdentifier",
"name": "href"
},
"value": {
"type": "StringLiteral",
"extra": {
"rawValue": "https://www.google.com",
"raw": "\"https://www.google.com\""
},
"value": "https://www.google.com"
}
}
],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"name": {
"type": "JSXIdentifier",
"name": "a"
}
},
"children": [
{
"type": "JSXText",
"extra": {
"rawValue": "Hello, world!",
"raw": "Hello, world!"
},
"value": "Hello, world!"
}
]
}
}
],
"kind": "const"
}
]
}
}
*/