-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (131 loc) · 3.08 KB
/
index.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
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
// @flow
'use strict';
/*::
type Print = mixed => string;
type Indent = string => string;
type Colors = {
comment: {close: string, open: string},
content: {close: string, open: string},
prop: {close: string, open: string},
tag: {close: string, open: string},
value: {close: string, open: string},
};
export type Options = {
edgeSpacing: string,
highlight: boolean,
indent: number,
min: boolean,
spacing: string,
};
type Position = {
line: number,
column: number,
};
type Location = {
start: Position,
end: Position,
};
type Node = {
type: string,
loc: Location,
[key: string]: mixed,
};
type Path = {
type: string,
node: Node,
[key: string]: mixed,
};
*/
function isNumber(val) {
return typeof val === 'number';
}
function isObject(val) {
return val !== null && typeof val === 'object';
}
function isNodeLike(obj) {
return typeof obj.type === 'string';
}
function isPathLike(obj) {
return isNodeLike(obj) && obj.hasOwnProperty('node');
}
function printPositionValue(val) {
return typeof val === 'number' ? String(val) : '';
}
function printPosition(position /*: Position */) {
let res = '';
let hasLine = isNumber(position.line);
let hasColumn = isNumber(position.column);
if (hasLine) res += position.line;
if (hasLine && hasColumn) res += ':';
if (hasColumn) res += position.column;
return res;
}
function printLocation(location /*: Location */, colors /*: Colors */) {
let res = '';
let hasStart = isObject(location.start);
let hasEnd = isObject(location.end);
res += colors.comment.open;
res += '(';
if (hasStart) res += printPosition(location.start);
if (hasStart && hasEnd) res += ', ';
if (hasEnd) res += printPosition(location.end);
res += colors.comment.close;
res += ')';
return res;
}
let DROP_KEYS = {
type: true,
start: true,
end: true,
loc: true,
};
function printPath(path /*: Path */, print, indent, opts, colors) {
return printNode(path.node, print, indent, opts, colors);
}
function printNode(node /*: Node */, print, indent, opts, colors) {
let res = '';
res += colors.tag.open;
res += '"' + node.type + '"';
res += colors.tag.close;
if (node.loc) {
res += ' ' + printLocation(node.loc, colors);
}
let keys = Object.keys(node).filter(key => !DROP_KEYS[key]).sort();
if (keys.length) {
res += opts.edgeSpacing;
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let line = '';
line += key;
line += ': ';
line += print(node[key]);
res += indent(line);
if (i < keys.length - 1) {
res += opts.spacing;
}
}
}
return res;
}
module.exports = {
test(val /*: any */) {
return isObject(val) && typeof val.type === 'string';
},
print(
val /*: Object */,
print /*: Print */,
indent /*: Indent */,
opts /*: Options */,
colors /*: Colors */
) {
let res = '';
if (isPathLike(val)) {
if (!opts.min) res += 'Path ';
val = val.node;
} else {
if (!opts.min) res += 'Node ';
}
res += printNode(val, print, indent, opts, colors);
return res;
},
};