Skip to content

Commit

Permalink
fix: wrong hint for deprecated Deno APIs (#1222)
Browse files Browse the repository at this point in the history
fixes #1221
  • Loading branch information
skanehira authored Jan 12, 2024
1 parent 5e45c49 commit 4dbd182
Showing 1 changed file with 62 additions and 12 deletions.
74 changes: 62 additions & 12 deletions src/rules/no_deprecated_deno_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl TryFrom<(&str, &str)> for DeprecatedApi {
enum Replacement {
NameAndUrl(&'static str, &'static str),
Name(&'static str),
NameAndUrls(Vec<(&'static str, &'static str)>),
}

impl DeprecatedApi {
Expand All @@ -124,6 +125,17 @@ impl DeprecatedApi {
Replacement::NameAndUrl(name, url) => {
format!("Use `{}` from {} instead", name, url)
}
Replacement::NameAndUrls(name_and_urls) => {
let mut hint = String::from("Use ");
for (i, (name, url)) in name_and_urls.into_iter().enumerate() {
if i != 0 {
hint.push_str(" and ");
}
hint.push_str(&format!("`{}` from {}", name, url));
}
hint.push_str(" instead");
hint
}
}
}

Expand All @@ -145,24 +157,37 @@ impl DeprecatedApi {
}

fn get_replacement(&self) -> Replacement {
const DENO_API: &str = "https://deno.land/api";
const BUFFER_TS: &str = "https://deno.land/std/io/buffer.ts";
const STREAMS_TS: &str = "https://deno.land/std/streams/conversion.ts";
const DENO_COMMAND_API: &str = "https://deno.land/api?s=Deno.Command";
const BUFFER_TS: &str =
"https://developer.mozilla.org/en-US/docs/Web/API/Streams_API";
const STREAMS_REDABLE_TS: &str = "https://deno.land/api?s=ReadableStream";
const STREAMS_REDABLE_PIPE_TO_TS: &str =
"https://deno.land/api?s=ReadableStream#method_pipeTo_4";
const STREAMS_REDABLE_FROM_TS: &str =
"https://deno.land/api?s=ReadableStream#variable_ReadableStream";
const STREAMS_WRITEABLE_TS: &str = "https://deno.land/api?s=WritableStream";
const STREAMS_TO_ARRAY_BUFFER_TS: &str =
"https://deno.land/std/streams/to_array_buffer.ts?s=toArrayBuffer";

use DeprecatedApi::*;
use Replacement::*;
match *self {
Buffer => NameAndUrl("Buffer", BUFFER_TS),
Copy => NameAndUrl("copy", STREAMS_TS),
Buffer => Name(BUFFER_TS),
Copy => Name(STREAMS_REDABLE_PIPE_TO_TS),
CustomInspect => Name("Symbol.for(\"Deno.customInspect\")"),
Iter => NameAndUrl("iter", STREAMS_TS),
IterSync => NameAndUrl("iterSync", STREAMS_TS),
Iter => Name(STREAMS_REDABLE_TS),
IterSync => Name(STREAMS_REDABLE_TS),
File => Name("Deno.FsFile"),
ReadAll => NameAndUrl("readAll", STREAMS_TS),
ReadAllSync => NameAndUrl("readAllSync", STREAMS_TS),
Run => NameAndUrl("Deno.Command", DENO_API),
WriteAll => NameAndUrl("writeAll", STREAMS_TS),
WriteAllSync => NameAndUrl("writeAllSync", STREAMS_TS),
ReadAll | ReadAllSync => NameAndUrls(vec![
("ReadableStream", STREAMS_REDABLE_TS),
("toArrayBuffer()", STREAMS_TO_ARRAY_BUFFER_TS),
]),
Run => NameAndUrl("Deno.Command", DENO_COMMAND_API),
WriteAll | WriteAllSync => NameAndUrls(vec![
("WritableStream", STREAMS_WRITEABLE_TS),
("ReadableStream.from", STREAMS_REDABLE_FROM_TS),
("ReadableStream.pipeTo", STREAMS_REDABLE_PIPE_TO_TS),
]),
}
}
}
Expand Down Expand Up @@ -535,4 +560,29 @@ Deno.readAll(reader);
],
}
}

#[test]
fn expect_deprecated_api_hint() {
let tests = vec![
(
"Buffer",
"Use `https://developer.mozilla.org/en-US/docs/Web/API/Streams_API` instead",
),
("copy", "Use `https://deno.land/api?s=ReadableStream#method_pipeTo_4` instead"),
("customInspect", "Use `Symbol.for(\"Deno.customInspect\")` instead"),
("File", "Use `Deno.FsFile` instead"),
("iter", "Use `https://deno.land/api?s=ReadableStream` instead"),
("iterSync", "Use `https://deno.land/api?s=ReadableStream` instead"),
("readAll", "Use `ReadableStream` from https://deno.land/api?s=ReadableStream and `toArrayBuffer()` from https://deno.land/std/streams/to_array_buffer.ts?s=toArrayBuffer instead"),
("readAllSync", "Use `ReadableStream` from https://deno.land/api?s=ReadableStream and `toArrayBuffer()` from https://deno.land/std/streams/to_array_buffer.ts?s=toArrayBuffer instead"),
("run", "Use `Deno.Command` from https://deno.land/api?s=Deno.Command instead"),
("writeAll", "Use `WritableStream` from https://deno.land/api?s=WritableStream and `ReadableStream.from` from https://deno.land/api?s=ReadableStream#variable_ReadableStream and `ReadableStream.pipeTo` from https://deno.land/api?s=ReadableStream#method_pipeTo_4 instead"),
("writeAllSync", "Use `WritableStream` from https://deno.land/api?s=WritableStream and `ReadableStream.from` from https://deno.land/api?s=ReadableStream#variable_ReadableStream and `ReadableStream.pipeTo` from https://deno.land/api?s=ReadableStream#method_pipeTo_4 instead"),
];

for test in tests {
let hint = DeprecatedApi::try_from(("Deno", test.0)).unwrap().hint();
assert_eq!(hint, test.1);
}
}
}

0 comments on commit 4dbd182

Please sign in to comment.