forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexamples.js
More file actions
244 lines (210 loc) · 7.13 KB
/
Copy pathexamples.js
File metadata and controls
244 lines (210 loc) · 7.13 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// This file is a source for playground examples.
// Examples integrity is smoke-checked with examples.spec.js
function parse_defaults(source, parse, stringify, transforms) {
// Invoking parser with default config covers the most genearic cases.
// Note how /*** and /* blocks are ignored
/** One-liner */
/** @some-tag {someType} someName */
/**
* Description may go
* over multiple lines followed by @tags
* @param {string} name the name parameter
* @param {any} value the value parameter
*/
/*** ignored */
/* ignored */
const parsed = parse(source);
const stringified = parsed.map((block) => stringify(block));
}
function parse_line_numbering(source, parse, stringify, transforms) {
// Note, line numbers are off by 5 from what you see in editor
//
// Try changeing start line back to 0, or omit the option
// parse(source, {startLine: 0}) -- default
// parse(source, {startLine: 5}) -- enforce alternative start number
/**
* Description may go
* over multiple lines followed by @tags
* @param {string} name the name parameter
* @param {any} value the value parameter
*/
const parsed = parse(source, { startLine: 5 });
const stringified = parsed[0].tags
.map((tag) => `line ${tag.source[0].number + 1} : @${tag.tag} ${tag.name}`)
.join('\n');
}
function parse_spacing(source, parse, stringify, transforms) {
// Note, when spacing option is set to 'compact' or omited, tag and block descriptions are collapsed to look like a single sentense.
//
// Try changeing it to 'preserve' or defining custom function
// parse(source, {spacing: 'compact'}) -- default
// parse(source, {spacing: 'preserve'}) -- preserve spaces and line breaks
// parse(source, {spacing: lines => lines
// .map(tokens => tokens.description.trim())
// .filter(description => description !== '')
// .join(' ');
// }) -- mimic 'compact' implementation
/**
* Description may go
* over multiple lines followed by
* @param {string} name the name parameter
* with multiline description
* @param {function(
* number,
* string
* )} options the options
*/
const parsed = parse(source, { spacing: 'preserve' });
const stringified = parsed[0].tags
.map((tag) => `@${tag.tag} - ${tag.description}\n\n${tag.type}`)
.join('\n----\n');
}
function parse_escaping(source, parse, stringify, transforms) {
// Note, @decorator is not parsed as another tag because block is wrapped into ###.
//
// Try setting alternative escape sequence
// parse(source, {fence: '```'}) -- default
// parse(source, {fence: '###'}) -- update source correspondingly
/**
* @example "some code"
###
@decorator
function hello() {
// do something
}
###
*/
const parsed = parse(source, { fence: '###' });
const stringified = parsed[0].tags
.map((tag) => `@${tag.tag} - ${tag.description}`)
.join('\n');
}
function stringify_formatting(source, parse, stringify, transforms) {
// stringify preserves exact formatting by default, but you can transform parsing result first
// transform = align() -- align name, type, and description
// transform = flow(align(), indent(4)) -- align, then place the block's opening marker at pos 4
/**
* Description may go
* over multiple lines followed by @tags
* @param {string} name the name parameter
* @param {any} value the value parameter
*/
const { flow, align, indent } = transforms;
const transform = flow(align(), indent(4));
const parsed = parse(source);
const stringified = stringify(transform(parsed[0]));
}
function parse_source_exploration(source, parse, stringify, transforms) {
// parse() produces Block[].source keeping accurate track of origin source
/**
* Description may go
* over multiple lines followed by @tags
* @param {string} name the name parameter
* @param {any} value the value parameter
*/
const parsed = parse(source);
const summary = ({ source }) => ({
source: source
.map(
({ tokens }) =>
tokens.start +
tokens.delimiter +
tokens.postDelimiter +
tokens.tag +
tokens.postTag +
tokens.type +
tokens.postType +
tokens.name +
tokens.postName +
tokens.description +
tokens.end
)
.join('\n'),
start: {
line: source[0].number + 1,
column: source[0].tokens.start.length,
},
end: {
line: source[source.length - 1].number + 1,
column: source[source.length - 1].source.length,
},
});
const pos = (p) => p.line + ':' + p.column;
const stringified = parsed[0].tags
.map(summary)
.map((s) => `${pos(s.start)} - ${pos(s.end)}\n${s.source}`);
}
function parse_advanced_parsing(source, parse, _, _, tokenizers) {
// Each '@tag ...' section results into a Spec. The Spec is computed by
// the chain of tokenizers each contributing a change to the the Spec.* and the Spec.tags[].tokens.
// Default parse() options come with stadart tokenizers:
// {
// ...,
// spacing = 'compact',
// tokenizers = [
// tokenizers.tag(),
// tokenizers.type(spacing),
// tokenizers.name(),
// tokenizers.description(spacing),
// ]
// }
// You can reorder those, or even replace any with a custom function (spec: Spec) => Spec
// This example allows to parse "@tag description" comments
/**
* @arg0 my parameter
* @arg1
* another parameter
* with a strange formatting
*/
const parsed = parse(source, {
tokenizers: [tokenizers.tag(), tokenizers.description('preserve')],
});
const stringified = parsed[0].tags
.map((tag) => `@${tag.tag} - ${tag.description}`)
.join('\n');
}
function stringify_rename(source, parse, stringify, transforms) {
// You can do any manipulations with the parsed result
// See how each block is being mapped. If you are updating a Block.source
// then rewireSource(block) should be called on each changed block.
// If changes were made to Block.tags[].source then call rewireSpecs(block).
// This example shows how you can "rename" @param tags: value1 -> value11, value2 -> value22
/**
* Description may go
* over multiple lines followed by @tags
* @param {string} name the name parameter
* @param {any} value1 first value parameter
* with a multipline description
* @param {any} value2 second value parameter
*/
function renameParam(from, to) {
return (block) => {
for (const tag of block.tags) {
if (tag.tag === 'param' && tag.name === from) {
tag.name = to;
for (const line of tag.source) {
if (line.tokens.name === from) line.tokens.name = to;
}
}
}
return block;
};
}
const transform = transforms.flow(
renameParam('value1', 'value11'),
renameParam('value2', 'value22'),
stringify
);
const parsed = parse(source);
const stringified = parsed.map(transform);
}
(typeof window === 'undefined' ? module.exports : window).examples = [
parse_defaults,
parse_line_numbering,
parse_escaping,
parse_spacing,
parse_source_exploration,
parse_advanced_parsing,
stringify_formatting,
stringify_rename,
];