forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathname.cjs
More file actions
109 lines (91 loc) · 3.03 KB
/
Copy pathname.cjs
File metadata and controls
109 lines (91 loc) · 3.03 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const util_js_1 = require("../../util.cjs");
const isQuoted = s => s && s.startsWith('"') && s.endsWith('"');
/**
* Splits remaining `spec.lines[].tokens.description` into `name` and `descriptions` tokens,
* and populates the `spec.name`
*/
function nameTokenizer() {
const typeEnd = (num, {
tokens
}, i) => tokens.type === '' ? num : i;
return spec => {
// look for the name in the line where {type} ends
const {
tokens
} = spec.source[spec.source.reduce(typeEnd, 0)];
const source = tokens.description.trimLeft();
const quotedGroups = source.split('"'); // if it starts with quoted group, assume it is a literal
if (quotedGroups.length > 1 && quotedGroups[0] === '' && quotedGroups.length % 2 === 1) {
spec.name = quotedGroups[1];
tokens.name = `"${quotedGroups[1]}"`;
[tokens.postName, tokens.description] = (0, util_js_1.splitSpace)(source.slice(tokens.name.length));
return spec;
}
let brackets = 0;
let name = '';
let optional = false;
let defaultValue; // assume name is non-space string or anything wrapped into brackets
for (const ch of source) {
if (brackets === 0 && (0, util_js_1.isSpace)(ch)) break;
if (ch === '[') brackets++;
if (ch === ']') brackets--;
name += ch;
}
if (brackets !== 0) {
spec.problems.push({
code: 'spec:name:unpaired-brackets',
message: 'unpaired brackets',
line: spec.source[0].number,
critical: true
});
return spec;
}
const nameToken = name;
if (name[0] === '[' && name[name.length - 1] === ']') {
optional = true;
name = name.slice(1, -1);
const parts = name.split('=');
name = parts[0].trim();
if (parts[1] !== undefined) defaultValue = parts.slice(1).join('=').trim();
if (name === '') {
spec.problems.push({
code: 'spec:name:empty-name',
message: 'empty name',
line: spec.source[0].number,
critical: true
});
return spec;
}
if (defaultValue === '') {
spec.problems.push({
code: 'spec:name:empty-default',
message: 'empty default value',
line: spec.source[0].number,
critical: true
});
return spec;
} // has "=" and is not a string, except for "=>"
if (!isQuoted(defaultValue) && /=(?!>)/.test(defaultValue)) {
spec.problems.push({
code: 'spec:name:invalid-default',
message: 'invalid default value syntax',
line: spec.source[0].number,
critical: true
});
return spec;
}
}
spec.optional = optional;
spec.name = name;
tokens.name = nameToken;
if (defaultValue !== undefined) spec.default = defaultValue;
[tokens.postName, tokens.description] = (0, util_js_1.splitSpace)(source.slice(tokens.name.length));
return spec;
};
}
exports.default = nameTokenizer;
//# sourceMappingURL=name.cjs.map