Skip to content

Commit 5d387e9

Browse files
committed
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 f2b1079 commit 5d387e9

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':
@@ -96,41 +99,43 @@ const typeMap = {
9699

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

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

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

0 commit comments

Comments
 (0)