• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const jsDocPrefix = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/';
4
5const jsDataStructuresUrl = `${jsDocPrefix}Data_structures`;
6const jsPrimitives = {
7  boolean: 'Boolean',
8  integer: 'Number', // Not a primitive, used for clarification.
9  null: 'Null',
10  number: 'Number',
11  string: 'String',
12  symbol: 'Symbol',
13  undefined: 'Undefined'
14};
15
16const jsGlobalObjectsUrl = `${jsDocPrefix}Reference/Global_Objects/`;
17const jsGlobalTypes = [
18  'Array', 'ArrayBuffer', 'DataView', 'Date', 'Error',
19  'EvalError', 'Function', 'Map', 'Object', 'Promise', 'RangeError',
20  'ReferenceError', 'RegExp', 'Set', 'SharedArrayBuffer', 'SyntaxError',
21  'TypeError', 'TypedArray', 'URIError', 'Uint8Array',
22];
23
24const customTypesMap = {
25  'any': `${jsDataStructuresUrl}#Data_types`,
26
27  'this': `${jsDocPrefix}Reference/Operators/this`,
28
29  'ArrayBufferView':
30    'https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView',
31
32  'AsyncIterator': 'https://tc39.github.io/ecma262/#sec-asynciterator-interface',
33
34  'bigint': `${jsDocPrefix}Reference/Global_Objects/BigInt`,
35  'WebAssembly.Instance':
36    `${jsDocPrefix}Reference/Global_Objects/WebAssembly/Instance`,
37
38  'Iterable':
39    `${jsDocPrefix}Reference/Iteration_protocols#The_iterable_protocol`,
40  'Iterator':
41    `${jsDocPrefix}Reference/Iteration_protocols#The_iterator_protocol`,
42
43  'Module Namespace Object':
44    'https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects',
45
46  'AsyncHook': 'async_hooks.html#async_hooks_async_hooks_createhook_callbacks',
47  'AsyncResource': 'async_hooks.html#async_hooks_class_asyncresource',
48
49  'brotli options': 'zlib.html#zlib_class_brotlioptions',
50
51  'Buffer': 'buffer.html#buffer_class_buffer',
52
53  'ChildProcess': 'child_process.html#child_process_class_childprocess',
54
55  'cluster.Worker': 'cluster.html#cluster_class_worker',
56
57  'Cipher': 'crypto.html#crypto_class_cipher',
58  'Decipher': 'crypto.html#crypto_class_decipher',
59  'DiffieHellman': 'crypto.html#crypto_class_diffiehellman',
60  'DiffieHellmanGroup': 'crypto.html#crypto_class_diffiehellmangroup',
61  'ECDH': 'crypto.html#crypto_class_ecdh',
62  'Hash': 'crypto.html#crypto_class_hash',
63  'Hmac': 'crypto.html#crypto_class_hmac',
64  'KeyObject': 'crypto.html#crypto_class_keyobject',
65  'Sign': 'crypto.html#crypto_class_sign',
66  'Verify': 'crypto.html#crypto_class_verify',
67  'crypto.constants': 'crypto.html#crypto_crypto_constants_1',
68
69  'dgram.Socket': 'dgram.html#dgram_class_dgram_socket',
70
71  'Domain': 'domain.html#domain_class_domain',
72
73  'errors.Error': 'errors.html#errors_class_error',
74
75  'import.meta': 'esm.html#esm_import_meta',
76
77  'EventEmitter': 'events.html#events_class_eventemitter',
78
79  'FileHandle': 'fs.html#fs_class_filehandle',
80  'fs.Dir': 'fs.html#fs_class_fs_dir',
81  'fs.Dirent': 'fs.html#fs_class_fs_dirent',
82  'fs.FSWatcher': 'fs.html#fs_class_fs_fswatcher',
83  'fs.ReadStream': 'fs.html#fs_class_fs_readstream',
84  'fs.Stats': 'fs.html#fs_class_fs_stats',
85  'fs.StatWatcher': 'fs.html#fs_class_fs_statwatcher',
86  'fs.WriteStream': 'fs.html#fs_class_fs_writestream',
87
88  'http.Agent': 'http.html#http_class_http_agent',
89  'http.ClientRequest': 'http.html#http_class_http_clientrequest',
90  'http.IncomingMessage': 'http.html#http_class_http_incomingmessage',
91  'http.Server': 'http.html#http_class_http_server',
92  'http.ServerResponse': 'http.html#http_class_http_serverresponse',
93
94  'ClientHttp2Session': 'http2.html#http2_class_clienthttp2session',
95  'ClientHttp2Stream': 'http2.html#http2_class_clienthttp2stream',
96  'HTTP/2 Headers Object': 'http2.html#http2_headers_object',
97  'HTTP/2 Settings Object': 'http2.html#http2_settings_object',
98  'http2.Http2ServerRequest': 'http2.html#http2_class_http2_http2serverrequest',
99  'http2.Http2ServerResponse':
100    'http2.html#http2_class_http2_http2serverresponse',
101  'Http2SecureServer': 'http2.html#http2_class_http2secureserver',
102  'Http2Server': 'http2.html#http2_class_http2server',
103  'Http2Session': 'http2.html#http2_class_http2session',
104  'Http2Stream': 'http2.html#http2_class_http2stream',
105  'ServerHttp2Stream': 'http2.html#http2_class_serverhttp2stream',
106
107  'https.Server': 'https.html#https_class_https_server',
108
109  'module': 'modules.html#modules_the_module_object',
110
111  'module.SourceMap':
112    'modules_module.html#modules_module_class_module_sourcemap',
113
114  'require': 'modules.html#modules_require_id',
115
116  'Handle': 'net.html#net_server_listen_handle_backlog_callback',
117  'net.Server': 'net.html#net_class_net_server',
118  'net.Socket': 'net.html#net_class_net_socket',
119
120  'os.constants.dlopen': 'os.html#os_dlopen_constants',
121
122  'Histogram': 'perf_hooks.html#perf_hooks_class_histogram',
123  'PerformanceEntry': 'perf_hooks.html#perf_hooks_class_performanceentry',
124  'PerformanceNodeTiming':
125    'perf_hooks.html#perf_hooks_class_performancenodetiming_extends_performanceentry', // eslint-disable-line max-len
126  'PerformanceObserver':
127    'perf_hooks.html#perf_hooks_class_perf_hooks_performanceobserver',
128  'PerformanceObserverEntryList':
129    'perf_hooks.html#perf_hooks_class_performanceobserverentrylist',
130
131  'readline.Interface': 'readline.html#readline_class_interface',
132
133  'repl.REPLServer': 'repl.html#repl_class_replserver',
134
135  'Stream': 'stream.html#stream_stream',
136  'stream.Duplex': 'stream.html#stream_class_stream_duplex',
137  'stream.Readable': 'stream.html#stream_class_stream_readable',
138  'stream.Transform': 'stream.html#stream_class_stream_transform',
139  'stream.Writable': 'stream.html#stream_class_stream_writable',
140
141  'Immediate': 'timers.html#timers_class_immediate',
142  'Timeout': 'timers.html#timers_class_timeout',
143  'Timer': 'timers.html#timers_timers',
144
145  'tls.SecureContext': 'tls.html#tls_tls_createsecurecontext_options',
146  'tls.Server': 'tls.html#tls_class_tls_server',
147  'tls.TLSSocket': 'tls.html#tls_class_tls_tlssocket',
148
149  'Tracing': 'tracing.html#tracing_tracing_object',
150
151  'URL': 'url.html#url_the_whatwg_url_api',
152  'URLSearchParams': 'url.html#url_class_urlsearchparams',
153
154  'vm.Module': 'vm.html#vm_class_vm_module',
155  'vm.SourceTextModule': 'vm.html#vm_class_vm_sourcetextmodule',
156
157  'MessagePort': 'worker_threads.html#worker_threads_class_messageport',
158
159  'zlib options': 'zlib.html#zlib_class_options',
160};
161
162const arrayPart = /(?:\[])+$/;
163
164function toLink(typeInput) {
165  const typeLinks = [];
166  typeInput = typeInput.replace('{', '').replace('}', '');
167  const typeTexts = typeInput.split('|');
168
169  typeTexts.forEach((typeText) => {
170    typeText = typeText.trim();
171    if (typeText) {
172      let typeUrl;
173
174      // To support type[], type[][] etc., we store the full string
175      // and use the bracket-less version to lookup the type URL.
176      const typeTextFull = typeText;
177      typeText = typeText.replace(arrayPart, '');
178
179      const primitive = jsPrimitives[typeText];
180
181      if (primitive !== undefined) {
182        typeUrl = `${jsDataStructuresUrl}#${primitive}_type`;
183      } else if (jsGlobalTypes.includes(typeText)) {
184        typeUrl = `${jsGlobalObjectsUrl}${typeText}`;
185      } else {
186        typeUrl = customTypesMap[typeText];
187      }
188
189      if (typeUrl) {
190        typeLinks.push(
191          `<a href="${typeUrl}" class="type">&lt;${typeTextFull}&gt;</a>`);
192      } else {
193        throw new Error(
194          `Unrecognized type: '${typeTextFull}'.\n` +
195          "Please, edit the type or update the 'tools/doc/type-parser.js'."
196        );
197      }
198    } else {
199      throw new Error(`Empty type slot: ${typeInput}`);
200    }
201  });
202
203  return typeLinks.length ? typeLinks.join(' | ') : typeInput;
204}
205
206module.exports = { toLink };
207