Skip to content

Commit 2c5d53f

Browse files
vsemozhetbyttargos
authored andcommitted
tools: fix nits in tools/doc/type-parser.js
PR-URL: #19612 Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Khaidi Chu <[email protected]>
1 parent 0daa063 commit 2c5d53f

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

tools/doc/type-parser.js

+44-39
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2-
const nodeDocUrl = '';
2+
33
const jsDocPrefix = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/';
4-
const jsDocUrl = `${jsDocPrefix}Reference/Global_Objects/`;
4+
55
const jsPrimitiveUrl = `${jsDocPrefix}Data_structures`;
66
const jsPrimitives = {
77
'boolean': 'Boolean',
@@ -12,6 +12,8 @@ const jsPrimitives = {
1212
'symbol': 'Symbol',
1313
'undefined': 'Undefined'
1414
};
15+
16+
const jsGlobalObjectsUrl = `${jsDocPrefix}Reference/Global_Objects/`;
1517
const jsGlobalTypes = [
1618
'Array', 'ArrayBuffer', 'AsyncFunction', 'DataView', 'Date', 'Error',
1719
'EvalError', 'Float32Array', 'Float64Array', 'Function', 'Generator',
@@ -21,7 +23,8 @@ const jsGlobalTypes = [
2123
'Uint16Array', 'Uint32Array', 'Uint8Array', 'Uint8ClampedArray', 'WeakMap',
2224
'WeakSet'
2325
];
24-
const typeMap = {
26+
27+
const customTypesMap = {
2528
'Iterable':
2629
`${jsDocPrefix}Reference/Iteration_protocols#The_iterable_protocol`,
2730
'Iterator':
@@ -95,41 +98,43 @@ const typeMap = {
9598

9699
const arrayPart = /(?:\[])+$/;
97100

98-
module.exports = {
99-
toLink: function(typeInput) {
100-
const typeLinks = [];
101-
typeInput = typeInput.replace('{', '').replace('}', '');
102-
const typeTexts = typeInput.split('|');
103-
104-
typeTexts.forEach(function(typeText) {
105-
typeText = typeText.trim();
106-
if (typeText) {
107-
let typeUrl = null;
108-
109-
// To support type[], type[][] etc., we store the full string
110-
// and use the bracket-less version to lookup the type URL
111-
const typeTextFull = typeText;
112-
typeText = typeText.replace(arrayPart, '');
113-
114-
const primitive = jsPrimitives[typeText.toLowerCase()];
115-
116-
if (primitive !== undefined) {
117-
typeUrl = `${jsPrimitiveUrl}#${primitive}_type`;
118-
} else if (jsGlobalTypes.indexOf(typeText) !== -1) {
119-
typeUrl = jsDocUrl + typeText;
120-
} else if (typeMap[typeText]) {
121-
typeUrl = nodeDocUrl + typeMap[typeText];
122-
}
123-
124-
if (typeUrl) {
125-
typeLinks.push(`
126-
<a href="${typeUrl}" class="type">&lt;${typeTextFull}&gt;</a>`);
127-
} else {
128-
typeLinks.push(`<span class="type">&lt;${typeTextFull}&gt;</span>`);
129-
}
101+
function toLink(typeInput) {
102+
const typeLinks = [];
103+
typeInput = typeInput.replace('{', '').replace('}', '');
104+
const typeTexts = typeInput.split('|');
105+
106+
typeTexts.forEach((typeText) => {
107+
typeText = typeText.trim();
108+
if (typeText) {
109+
let typeUrl = null;
110+
111+
// To support type[], type[][] etc., we store the full string
112+
// and use the bracket-less version to lookup the type URL
113+
const typeTextFull = typeText;
114+
typeText = typeText.replace(arrayPart, '');
115+
116+
const primitive = jsPrimitives[typeText.toLowerCase()];
117+
118+
if (primitive !== undefined) {
119+
typeUrl = `${jsPrimitiveUrl}#${primitive}_type`;
120+
} else if (jsGlobalTypes.includes(typeText)) {
121+
typeUrl = `${jsGlobalObjectsUrl}${typeText}`;
122+
} else if (customTypesMap[typeText]) {
123+
typeUrl = customTypesMap[typeText];
130124
}
131-
});
132125

133-
return typeLinks.length ? typeLinks.join(' | ') : typeInput;
134-
}
135-
};
126+
if (typeUrl) {
127+
typeLinks.push(
128+
`<a href="${typeUrl}" class="type">&lt;${typeTextFull}&gt;</a>`);
129+
} else {
130+
typeLinks.push(`<span class="type">&lt;${typeTextFull}&gt;</span>`);
131+
}
132+
} else {
133+
throw new Error(`Empty type slot: ${typeInput}`);
134+
}
135+
});
136+
137+
return typeLinks.length ? typeLinks.join(' | ') : typeInput;
138+
}
139+
140+
module.exports = { toLink };

0 commit comments

Comments
 (0)