diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-07-13 19:29:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-13 23:29:51 +0000 |
commit | b03d82e5d34ed11185fd3b9ce4db7bb838c65658 (patch) | |
tree | ea4d490606168f6bd91f89e16b970b4b3a37ae19 /cli/tsc/99_main_compiler.js | |
parent | 8465bd0037acbaac0c7dfab6a8c6dbf47539934e (diff) |
fix(tsc): more informative diagnostic when `Deno` does not exist (#19825)
Also improved the diagnostic when using something like `Deno.openKv` and
it doesn't exist.
Diffstat (limited to 'cli/tsc/99_main_compiler.js')
-rw-r--r-- | cli/tsc/99_main_compiler.js | 82 |
1 files changed, 78 insertions, 4 deletions
diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js index 56ac5522b..2190dda99 100644 --- a/cli/tsc/99_main_compiler.js +++ b/cli/tsc/99_main_compiler.js @@ -29,6 +29,37 @@ delete Object.prototype.__proto__; /** @type {Map<string, string>} */ const normalizedToOriginalMap = new Map(); + /** @type {ReadonlySet<string>} */ + const unstableDenoProps = new Set([ + "AtomicOperation", + "CreateHttpClientOptions", + "DatagramConn", + "HttpClient", + "Kv", + "KvListIterator", + "KvU64", + "UnsafeCallback", + "UnsafePointer", + "UnsafePointerView", + "UnsafeFnPointer", + "UnixConnectOptions", + "UnixListenOptions", + "createHttpClient", + "dlopen", + "flock", + "flockSync", + "funlock", + "funlockSync", + "listen", + "listenDatagram", + "openKv", + "upgradeHttp", + "umask", + ]); + const unstableMsgSuggestion = + "If not, try changing the 'lib' compiler option to include 'deno.unstable' " + + 'or add a triple-slash directive to your entrypoint: /// <reference lib="deno.unstable" />'; + /** * @param {unknown} value * @returns {value is ts.CreateSourceFileOptions} @@ -303,6 +334,49 @@ delete Object.prototype.__proto__; return isNodeSourceFile; }); + /** + * @param msg {string} + * @param code {number} + */ + function formatMessage(msg, code) { + switch (code) { + case 2304: { + if (msg === "Cannot find name 'Deno'.") { + msg += " Do you need to change your target library? " + + "Try changing the 'lib' compiler option to include 'deno.ns' " + + 'or add a triple-slash directive to your entrypoint: /// <reference lib="deno.ns" />'; + } + return msg; + } + case 2339: { + const property = getProperty(); + if (property && unstableDenoProps.has(property)) { + return `${msg} 'Deno.${property}' is an unstable API. Did you forget to run with the '--unstable' flag? ${unstableMsgSuggestion}`; + } + return msg; + } + default: { + const property = getProperty(); + if (property && unstableDenoProps.has(property)) { + const suggestion = getMsgSuggestion(); + if (suggestion) { + return `${msg} 'Deno.${property}' is an unstable API. Did you forget to run with the '--unstable' flag, or did you mean '${suggestion}'? ${unstableMsgSuggestion}`; + } + } + return msg; + } + } + + function getProperty() { + return /Property '([^']+)' does not exist on type 'typeof Deno'/ + .exec(msg)?.[1]; + } + + function getMsgSuggestion() { + return / Did you mean '([^']+)'\?/.exec(msg)?.[1]; + } + } + /** @param {ts.DiagnosticRelatedInformation} diagnostic */ function fromRelatedInformation({ start, @@ -316,7 +390,7 @@ delete Object.prototype.__proto__; if (typeof msgText === "object") { messageChain = msgText; } else { - messageText = msgText; + messageText = formatMessage(msgText, ri.code); } if (start !== undefined && length !== undefined && file) { const startPos = file.getLineAndCharacterOfPosition(start); @@ -341,7 +415,7 @@ delete Object.prototype.__proto__; } /** @param {readonly ts.Diagnostic[]} diagnostics */ - function fromTypeScriptDiagnostic(diagnostics) { + function fromTypeScriptDiagnostics(diagnostics) { return diagnostics.map(({ relatedInformation: ri, source, ...diag }) => { /** @type {any} */ const value = fromRelatedInformation(diag); @@ -864,7 +938,7 @@ delete Object.prototype.__proto__; performanceProgram({ program }); ops.op_respond({ - diagnostics: fromTypeScriptDiagnostic(diagnostics), + diagnostics: fromTypeScriptDiagnostics(diagnostics), stats: performanceEnd(), }); debug("<<< exec stop"); @@ -1055,7 +1129,7 @@ delete Object.prototype.__proto__; /** @type {Record<string, any[]>} */ const diagnosticMap = {}; for (const specifier of request.specifiers) { - diagnosticMap[specifier] = fromTypeScriptDiagnostic([ + diagnosticMap[specifier] = fromTypeScriptDiagnostics([ ...languageService.getSemanticDiagnostics(specifier), ...languageService.getSuggestionDiagnostics(specifier), ...languageService.getSyntacticDiagnostics(specifier), |