diff options
author | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2018-03-26 14:58:01 +0300 |
---|---|---|
committer | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2018-03-28 11:12:04 +0300 |
commit | 5d387e9403bc67604b5556538fcddc52857638a6 (patch) | |
tree | 2fd40e08b3aa0194e0b8bbb1e32cc3af3cdc8298 /tools | |
parent | f2b10799efbbda8a9d6999c0677dbae238093a97 (diff) | |
download | node-new-5d387e9403bc67604b5556538fcddc52857638a6.tar.gz |
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>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/doc/type-parser.js | 83 |
1 files changed, 44 insertions, 39 deletions
diff --git a/tools/doc/type-parser.js b/tools/doc/type-parser.js index e42b23bdea..dbf1538c10 100644 --- a/tools/doc/type-parser.js +++ b/tools/doc/type-parser.js @@ -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', @@ -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', @@ -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': @@ -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"><${typeTextFull}></a>`); - } else { - typeLinks.push(`<span class="type"><${typeTextFull}></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"><${typeTextFull}></a>`); + } else { + typeLinks.push(`<span class="type"><${typeTextFull}></span>`); + } + } else { + throw new Error(`Empty type slot: ${typeInput}`); + } + }); + + return typeLinks.length ? typeLinks.join(' | ') : typeInput; +} + +module.exports = { toLink }; |