Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions tools/doc/type-parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const nodeDocUrl = '';

const jsDocPrefix = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/';
const jsDocUrl = `${jsDocPrefix}Reference/Global_Objects/`;

const jsPrimitiveUrl = `${jsDocPrefix}Data_structures`;
const jsPrimitives = {
'boolean': 'Boolean',
Expand All @@ -12,6 +12,8 @@ const jsPrimitives = {
'symbol': 'Symbol',
'undefined': 'Undefined'
};

const jsGlobalObjectsUrl = `${jsDocPrefix}Reference/Global_Objects/`;
const jsGlobalTypes = [
'Array', 'ArrayBuffer', 'AsyncFunction', 'DataView', 'Date', 'Error',
'EvalError', 'Float32Array', 'Float64Array', 'Function', 'Generator',
Expand All @@ -21,7 +23,8 @@ const jsGlobalTypes = [
'Uint16Array', 'Uint32Array', 'Uint8Array', 'Uint8ClampedArray', 'WeakMap',
'WeakSet'
];
const typeMap = {

const customTypesMap = {
'Iterable':
`${jsDocPrefix}Reference/Iteration_protocols#The_iterable_protocol`,
'Iterator':
Expand Down Expand Up @@ -96,41 +99,43 @@ const typeMap = {

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

module.exports = {
toLink: function(typeInput) {
const typeLinks = [];
typeInput = typeInput.replace('{', '').replace('}', '');
const typeTexts = typeInput.split('|');

typeTexts.forEach(function(typeText) {
typeText = typeText.trim();
if (typeText) {
let typeUrl = null;

// To support type[], type[][] etc., we store the full string
// and use the bracket-less version to lookup the type URL
const typeTextFull = typeText;
typeText = typeText.replace(arrayPart, '');

const primitive = jsPrimitives[typeText.toLowerCase()];

if (primitive !== undefined) {
typeUrl = `${jsPrimitiveUrl}#${primitive}_type`;
} else if (jsGlobalTypes.indexOf(typeText) !== -1) {
typeUrl = jsDocUrl + typeText;
} else if (typeMap[typeText]) {
typeUrl = nodeDocUrl + typeMap[typeText];
}

if (typeUrl) {
typeLinks.push(`
<a href="${typeUrl}" class="type">&lt;${typeTextFull}&gt;</a>`);
} else {
typeLinks.push(`<span class="type">&lt;${typeTextFull}&gt;</span>`);
}
function toLink(typeInput) {
const typeLinks = [];
typeInput = typeInput.replace('{', '').replace('}', '');
const typeTexts = typeInput.split('|');

typeTexts.forEach((typeText) => {
typeText = typeText.trim();
if (typeText) {
let typeUrl = null;

// To support type[], type[][] etc., we store the full string
// and use the bracket-less version to lookup the type URL
const typeTextFull = typeText;
typeText = typeText.replace(arrayPart, '');

const primitive = jsPrimitives[typeText.toLowerCase()];

if (primitive !== undefined) {
typeUrl = `${jsPrimitiveUrl}#${primitive}_type`;
} else if (jsGlobalTypes.includes(typeText)) {
typeUrl = `${jsGlobalObjectsUrl}${typeText}`;
} else if (customTypesMap[typeText]) {
typeUrl = customTypesMap[typeText];
}
});

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 };