tools: fix nits in tools/doc/type-parser.js

PR-URL: https://github.com/nodejs/node/pull/19612
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
This commit is contained in:
Vse Mozhet Byt 2018-03-26 14:58:01 +03:00
parent f2b10799ef
commit 5d387e9403

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const nodeDocUrl = '';
const jsDocPrefix = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/'; const jsDocPrefix = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/';
const jsDocUrl = `${jsDocPrefix}Reference/Global_Objects/`;
const jsPrimitiveUrl = `${jsDocPrefix}Data_structures`; const jsPrimitiveUrl = `${jsDocPrefix}Data_structures`;
const jsPrimitives = { const jsPrimitives = {
'boolean': 'Boolean', 'boolean': 'Boolean',
@ -12,6 +12,8 @@ const jsPrimitives = {
'symbol': 'Symbol', 'symbol': 'Symbol',
'undefined': 'Undefined' 'undefined': 'Undefined'
}; };
const jsGlobalObjectsUrl = `${jsDocPrefix}Reference/Global_Objects/`;
const jsGlobalTypes = [ const jsGlobalTypes = [
'Array', 'ArrayBuffer', 'AsyncFunction', 'DataView', 'Date', 'Error', 'Array', 'ArrayBuffer', 'AsyncFunction', 'DataView', 'Date', 'Error',
'EvalError', 'Float32Array', 'Float64Array', 'Function', 'Generator', 'EvalError', 'Float32Array', 'Float64Array', 'Function', 'Generator',
@ -21,7 +23,8 @@ const jsGlobalTypes = [
'Uint16Array', 'Uint32Array', 'Uint8Array', 'Uint8ClampedArray', 'WeakMap', 'Uint16Array', 'Uint32Array', 'Uint8Array', 'Uint8ClampedArray', 'WeakMap',
'WeakSet' 'WeakSet'
]; ];
const typeMap = {
const customTypesMap = {
'Iterable': 'Iterable':
`${jsDocPrefix}Reference/Iteration_protocols#The_iterable_protocol`, `${jsDocPrefix}Reference/Iteration_protocols#The_iterable_protocol`,
'Iterator': 'Iterator':
@ -96,41 +99,43 @@ const typeMap = {
const arrayPart = /(?:\[])+$/; const arrayPart = /(?:\[])+$/;
module.exports = { function toLink(typeInput) {
toLink: function(typeInput) { const typeLinks = [];
const typeLinks = []; typeInput = typeInput.replace('{', '').replace('}', '');
typeInput = typeInput.replace('{', '').replace('}', ''); const typeTexts = typeInput.split('|');
const typeTexts = typeInput.split('|');
typeTexts.forEach(function(typeText) { typeTexts.forEach((typeText) => {
typeText = typeText.trim(); typeText = typeText.trim();
if (typeText) { if (typeText) {
let typeUrl = null; let typeUrl = null;
// To support type[], type[][] etc., we store the full string // To support type[], type[][] etc., we store the full string
// and use the bracket-less version to lookup the type URL // and use the bracket-less version to lookup the type URL
const typeTextFull = typeText; const typeTextFull = typeText;
typeText = typeText.replace(arrayPart, ''); typeText = typeText.replace(arrayPart, '');
const primitive = jsPrimitives[typeText.toLowerCase()]; const primitive = jsPrimitives[typeText.toLowerCase()];
if (primitive !== undefined) { if (primitive !== undefined) {
typeUrl = `${jsPrimitiveUrl}#${primitive}_type`; typeUrl = `${jsPrimitiveUrl}#${primitive}_type`;
} else if (jsGlobalTypes.indexOf(typeText) !== -1) { } else if (jsGlobalTypes.includes(typeText)) {
typeUrl = jsDocUrl + typeText; typeUrl = `${jsGlobalObjectsUrl}${typeText}`;
} else if (typeMap[typeText]) { } else if (customTypesMap[typeText]) {
typeUrl = nodeDocUrl + typeMap[typeText]; typeUrl = customTypesMap[typeText];
}
if (typeUrl) {
typeLinks.push(`
<a href="${typeUrl}" class="type">&lt;${typeTextFull}&gt;</a>`);
} else {
typeLinks.push(`<span class="type">&lt;${typeTextFull}&gt;</span>`);
}
} }
});
return typeLinks.length ? typeLinks.join(' | ') : typeInput; if (typeUrl) {
} typeLinks.push(
}; `<a href="${typeUrl}" class="type">&lt;${typeTextFull}&gt;</a>`);
} else {
typeLinks.push(`<span class="type">&lt;${typeTextFull}&gt;</span>`);
}
} else {
throw new Error(`Empty type slot: ${typeInput}`);
}
});
return typeLinks.length ? typeLinks.join(' | ') : typeInput;
}
module.exports = { toLink };