Skip to content

Commit eb5f085

Browse files
vsemozhetbytMylesBorins
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 4a1b064 commit eb5f085

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

‎tools/doc/type-parser.js‎

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2-
constnodeDocUrl='';
2+
33
constjsDocPrefix='https://developer.mozilla.org/en-US/docs/Web/JavaScript/';
4-
constjsDocUrl=`${jsDocPrefix}Reference/Global_Objects/`;
4+
55
constjsPrimitiveUrl=`${jsDocPrefix}Data_structures`;
66
constjsPrimitives={
77
'boolean': 'Boolean',
@@ -12,6 +12,8 @@ const jsPrimitives ={
1212
'symbol': 'Symbol',
1313
'undefined': 'Undefined'
1414
};
15+
16+
constjsGlobalObjectsUrl=`${jsDocPrefix}Reference/Global_Objects/`;
1517
constjsGlobalTypes=[
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-
consttypeMap={
26+
27+
constcustomTypesMap={
2528
'Iterable':
2629
`${jsDocPrefix}Reference/Iteration_protocols#The_iterable_protocol`,
2730
'Iterator':
@@ -95,41 +98,43 @@ const typeMap ={
9598

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

98-
module.exports={
99-
toLink: function(typeInput){
100-
consttypeLinks=[];
101-
typeInput=typeInput.replace('{','').replace('}','');
102-
consttypeTexts=typeInput.split('|');
103-
104-
typeTexts.forEach(function(typeText){
105-
typeText=typeText.trim();
106-
if(typeText){
107-
lettypeUrl=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-
consttypeTextFull=typeText;
112-
typeText=typeText.replace(arrayPart,'');
113-
114-
constprimitive=jsPrimitives[typeText.toLowerCase()];
115-
116-
if(primitive!==undefined){
117-
typeUrl=`${jsPrimitiveUrl}#${primitive}_type`;
118-
}elseif(jsGlobalTypes.indexOf(typeText)!==-1){
119-
typeUrl=jsDocUrl+typeText;
120-
}elseif(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+
functiontoLink(typeInput){
102+
consttypeLinks=[];
103+
typeInput=typeInput.replace('{','').replace('}','');
104+
consttypeTexts=typeInput.split('|');
105+
106+
typeTexts.forEach((typeText)=>{
107+
typeText=typeText.trim();
108+
if(typeText){
109+
lettypeUrl=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+
consttypeTextFull=typeText;
114+
typeText=typeText.replace(arrayPart,'');
115+
116+
constprimitive=jsPrimitives[typeText.toLowerCase()];
117+
118+
if(primitive!==undefined){
119+
typeUrl=`${jsPrimitiveUrl}#${primitive}_type`;
120+
}elseif(jsGlobalTypes.includes(typeText)){
121+
typeUrl=`${jsGlobalObjectsUrl}${typeText}`;
122+
}elseif(customTypesMap[typeText]){
123+
typeUrl=customTypesMap[typeText];
130124
}
131-
});
132125

133-
returntypeLinks.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+
thrownewError(`Empty type slot: ${typeInput}`);
134+
}
135+
});
136+
137+
returntypeLinks.length ? typeLinks.join(' | ') : typeInput;
138+
}
139+
140+
module.exports={ toLink };

0 commit comments

Comments
(0)