forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.js
More file actions
175 lines (149 loc) · 3.7 KB
/
ast.js
File metadata and controls
175 lines (149 loc) · 3.7 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const NODES_KEYS = {
attrs: true,
children: true,
cases: true, // plural and select
expression: true, // for expansionCase
};
const NON_ENUMERABLE_PROPERTIES = new Set(["parent"]);
// TODO: typechecking is problematic for this class because of this issue:
// https://github.com/microsoft/TypeScript/issues/26811
class Node {
constructor(nodeOrProperties = {}) {
for (const property of new Set([
...NON_ENUMERABLE_PROPERTIES,
...Object.keys(nodeOrProperties),
])) {
this.setProperty(property, nodeOrProperties[property]);
}
}
setProperty(property, value) {
if (this[property] === value) {
return;
}
if (property in NODES_KEYS) {
value = value.map((node) => this.createChild(node));
}
if (!NON_ENUMERABLE_PROPERTIES.has(property)) {
this[property] = value;
return;
}
Object.defineProperty(this, property, {
value,
enumerable: false,
configurable: true,
});
}
map(fn) {
/** @type{any} */
let newNode;
for (const NODES_KEY in NODES_KEYS) {
const nodes = this[NODES_KEY];
if (nodes) {
const mappedNodes = mapNodesIfChanged(nodes, (node) => node.map(fn));
if (newNode !== nodes) {
if (!newNode) {
// @ts-expect-error
newNode = new Node({ parent: this.parent });
}
newNode.setProperty(NODES_KEY, mappedNodes);
}
}
}
if (newNode) {
for (const key in this) {
if (!(key in NODES_KEYS)) {
newNode[key] = this[key];
}
}
}
return fn(newNode || this);
}
walk(fn) {
for (const NODES_KEY in NODES_KEYS) {
const nodes = this[NODES_KEY];
if (nodes) {
for (let i = 0; i < nodes.length; i++) {
nodes[i].walk(fn);
}
}
}
fn(this);
}
createChild(nodeOrProperties) {
const node =
nodeOrProperties instanceof Node
? nodeOrProperties.clone()
: new Node(nodeOrProperties);
node.setProperty("parent", this);
return node;
}
/**
* @param {Node} [target]
* @param {Object} [node]
*/
insertChildBefore(target, node) {
// @ts-expect-error
this.children.splice(
// @ts-expect-error
this.children.indexOf(target),
0,
this.createChild(node),
);
}
/**
* @param {Node} [child]
*/
removeChild(child) {
// @ts-expect-error
this.children.splice(this.children.indexOf(child), 1);
}
/**
* @param {Node} [target]
* @param {Object} [node]
*/
replaceChild(target, node) {
// @ts-expect-error
this.children[this.children.indexOf(target)] = this.createChild(node);
}
clone() {
return new Node(this);
}
get firstChild() {
// @ts-expect-error
return this.children?.[0];
}
get lastChild() {
// @ts-expect-error
return this.children?.[this.children.length - 1];
}
get prev() {
// @ts-expect-error
return this.parent?.children?.[this.parent.children.indexOf(this) - 1];
}
get next() {
// @ts-expect-error
return this.parent?.children?.[this.parent.children.indexOf(this) + 1];
}
// for element and attribute
get rawName() {
// @ts-expect-error
return this.hasExplicitNamespace ? this.fullName : this.name;
}
get fullName() {
// @ts-expect-error
return this.namespace ? this.namespace + ":" + this.name : this.name;
}
get attrMap() {
return Object.fromEntries(
// @ts-expect-error
this.attrs.map((attr) => [attr.fullName, attr.value]),
);
}
}
function mapNodesIfChanged(nodes, fn) {
const newNodes = nodes.map(fn);
return newNodes.some((newNode, index) => newNode !== nodes[index])
? newNodes
: nodes;
}
export { Node };