Skip to content

Commit

Permalink
Improved typechecking error for unstable props (denoland#5503)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored and bartlomieju committed Jun 5, 2020
1 parent aa1fa50 commit f99e9e2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
80 changes: 77 additions & 3 deletions cli/js/diagnostics_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,79 @@ import {
DiagnosticItem,
} from "./diagnostics.ts";

const unstableDenoGlobalProperties = [
"umask",
"linkSync",
"link",
"symlinkSync",
"symlink",
"DirKind",
"dir",
"loadavg",
"osRelease",
"openPlugin",
"DiagnosticCategory",
"DiagnosticMessageChain",
"DiagnosticItem",
"Diagnostic",
"formatDiagnostics",
"CompilerOptions",
"TranspileOnlyResult",
"transpileOnly",
"compile",
"bundle",
"Location",
"applySourceMap",
"LinuxSignal",
"MacOSSignal",
"Signal",
"SignalStream",
"signal",
"signals",
"setRaw",
"utimeSync",
"utime",
"ShutdownMode",
"shutdown",
"DatagramConn",
"UnixListenOptions",
"listen",
"listenDatagram",
"UnixConnectOptions",
"connect",
"StartTlsOptions",
"startTls",
"kill",
"PermissionName",
"PermissionState",
"RunPermissionDescriptor",
"ReadPermissionDescriptor",
"WritePermissionDescriptor",
"NetPermissionDescriptor",
"EnvPermissionDescriptor",
"PluginPermissionDescriptor",
"HrtimePermissionDescriptor",
"PermissionDescriptor",
"Permissions",
"PermissionStatus",
"hostname",
];

function transformMessageText(messageText: string, code: number): string {
if (code === 2339) {
const property = messageText
.replace(/^Property '/, "")
.replace(/' does not exist on type 'typeof Deno'\.$/, "");
if (
messageText.endsWith("on type 'typeof Deno'.") &&
unstableDenoGlobalProperties.includes(property)
) {
return `${messageText} 'Deno.${property}' is an unstable API. Did you forget to run with the '--unstable' flag?`;
}
}
return messageText;
}

interface SourceInformation {
sourceLine: string;
lineNumber: number;
Expand Down Expand Up @@ -78,7 +151,8 @@ function fromDiagnosticMessageChain(
return undefined;
}

return messageChain.map(({ messageText: message, code, category, next }) => {
return messageChain.map(({ messageText, code, category, next }) => {
const message = transformMessageText(messageText, code);
return {
message,
code,
Expand Down Expand Up @@ -110,9 +184,9 @@ function parseDiagnostic(
let message: string;
let messageChain: DiagnosticMessageChain | undefined;
if (typeof messageText === "string") {
message = messageText;
message = transformMessageText(messageText, code);
} else {
message = messageText.messageText;
message = transformMessageText(messageText.messageText, messageText.code);
messageChain = fromDiagnosticMessageChain([messageText])![0];
}

Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unstable_disabled.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[WILDCARD]
error: TS2339 [ERROR]: Property 'loadavg' does not exist on type 'typeof Deno'.
error: TS2339 [ERROR]: Property 'loadavg' does not exist on type 'typeof Deno'. 'Deno.loadavg' is an unstable API. Did you forget to run with the '--unstable' flag?
console.log(Deno.loadavg);
~~~~~~~
at [WILDCARD]/cli/tests/unstable.ts:1:18

0 comments on commit f99e9e2

Please sign in to comment.