forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.js
136 lines (118 loc) · 2.95 KB
/
ast.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
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
"use strict";
const NODES_KEYS = {
attrs: true,
children: true
};
class Node {
constructor(props = {}) {
for (const key of Object.keys(props)) {
const value = props[key];
if (key in NODES_KEYS) {
this._setNodes(key, value);
} else {
this[key] = value;
}
}
}
_setNodes(key, nodes) {
if (nodes !== this[key]) {
this[key] = cloneAndUpdateNodes(nodes, this);
if (key === "attrs") {
setNonEnumerableProperties(this, {
attrMap: this[key].reduce((reduced, attr) => {
reduced[attr.fullName] = attr.value;
return reduced;
}, Object.create(null))
});
}
}
}
map(fn) {
let newNode = null;
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) {
newNode = new Node();
}
newNode._setNodes(NODES_KEY, mappedNodes);
}
}
}
if (newNode) {
for (const key in this) {
if (!(key in NODES_KEYS)) {
newNode[key] = this[key];
}
}
const { index, siblings, prev, next, parent } = this;
setNonEnumerableProperties(newNode, {
index,
siblings,
prev,
next,
parent
});
}
return fn(newNode || this);
}
clone(overrides) {
return new Node(overrides ? Object.assign({}, this, overrides) : this);
}
get firstChild() {
return this.children && this.children.length !== 0
? this.children[0]
: null;
}
get lastChild() {
return this.children && this.children.length !== 0
? this.children[this.children.length - 1]
: null;
}
// for element and attribute
get rawName() {
return this.hasExplicitNamespace ? this.fullName : this.name;
}
get fullName() {
return this.namespace ? this.namespace + ":" + this.name : this.name;
}
}
function mapNodesIfChanged(nodes, fn) {
const newNodes = nodes.map(fn);
return newNodes.some((newNode, index) => newNode !== nodes[index])
? newNodes
: nodes;
}
function cloneAndUpdateNodes(nodes, parent) {
const siblings = nodes.map(
node => (node instanceof Node ? node.clone() : new Node(node))
);
let prev = null;
let current = siblings[0];
let next = siblings[1] || null;
for (let index = 0; index < siblings.length; index++) {
setNonEnumerableProperties(current, {
index,
siblings,
prev,
next,
parent
});
prev = current;
current = next;
next = siblings[index + 2] || null;
}
return siblings;
}
function setNonEnumerableProperties(obj, props) {
const descriptors = Object.keys(props).reduce((reduced, key) => {
reduced[key] = { value: props[key], enumerable: false };
return reduced;
}, {});
Object.defineProperties(obj, descriptors);
}
module.exports = {
Node
};