From a06b8b3517e91bf5911a1481633c45e7e4767a9e Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 12 Nov 2024 17:16:45 +0900 Subject: [PATCH 1/4] add unit test --- cli/util/extract.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/cli/util/extract.rs b/cli/util/extract.rs index f577cbefec33ce..61829777d34c67 100644 --- a/cli/util/extract.rs +++ b/cli/util/extract.rs @@ -1165,6 +1165,33 @@ Deno.test("file:///main.ts$3-6.ts", async ()=>{ media_type: MediaType::TypeScript, }], }, + // https://github.com/denoland/deno/issues/26728 + Test { + input: Input { + source: r#" +/** + * ```ts + * // @ts-expect-error: can only add numbers + * add('1', '2'); + * ``` + */ +export function add(first: number, second: number) { + return first + second; +} +"#, + specifier: "file:///main.ts", + }, + expected: vec![Expected { + source: r#"import { add } from "file:///main.ts"; +Deno.test("file:///main.ts$3-7.ts", async ()=>{ + // @ts-expect-error: can only add numbers + add('1', '2'); +}); +"#, + specifier: "file:///main.ts$3-7.ts", + media_type: MediaType::TypeScript, + }], + }, ]; for test in tests { @@ -1376,6 +1403,31 @@ console.log(Foo); media_type: MediaType::TypeScript, }], }, + // https://github.com/denoland/deno/issues/26728 + Test { + input: Input { + source: r#" +/** + * ```ts + * // @ts-expect-error: can only add numbers + * add('1', '2'); + * ``` + */ +export function add(first: number, second: number) { + return first + second; +} +"#, + specifier: "file:///main.ts", + }, + expected: vec![Expected { + source: r#"import { add } from "file:///main.ts"; +// @ts-expect-error: can only add numbers +add('1', '2'); +"#, + specifier: "file:///main.ts$3-7.ts", + media_type: MediaType::TypeScript, + }], + }, ]; for test in tests { From 78334cd012e8509887fa9ddae20b784563017ad5 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 12 Nov 2024 18:45:22 +0900 Subject: [PATCH 2/4] fix --- cli/util/extract.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cli/util/extract.rs b/cli/util/extract.rs index 61829777d34c67..be68202aa1eecf 100644 --- a/cli/util/extract.rs +++ b/cli/util/extract.rs @@ -586,7 +586,10 @@ fn generate_pseudo_file( wrap_kind, })); - let source = deno_ast::swc::codegen::to_code(&transformed); + let source = deno_ast::swc::codegen::to_code_with_comments( + Some(&parsed.comments().as_single_threaded()), + &transformed, + ); log::debug!("{}:\n{}", file.specifier, source); From e11ea8f73351be9bfb9f9ee07bd764fb4c6a3982 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 12 Nov 2024 18:45:44 +0900 Subject: [PATCH 3/4] add spec test --- tests/specs/test/doc_ts_expect_error/__test__.jsonc | 5 +++++ tests/specs/test/doc_ts_expect_error/mod.out | 8 ++++++++ tests/specs/test/doc_ts_expect_error/mod.ts | 13 +++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/specs/test/doc_ts_expect_error/__test__.jsonc create mode 100644 tests/specs/test/doc_ts_expect_error/mod.out create mode 100644 tests/specs/test/doc_ts_expect_error/mod.ts diff --git a/tests/specs/test/doc_ts_expect_error/__test__.jsonc b/tests/specs/test/doc_ts_expect_error/__test__.jsonc new file mode 100644 index 00000000000000..ba64887a32c64e --- /dev/null +++ b/tests/specs/test/doc_ts_expect_error/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc mod.ts", + "exitCode": 0, + "output": "mod.out" +} diff --git a/tests/specs/test/doc_ts_expect_error/mod.out b/tests/specs/test/doc_ts_expect_error/mod.out new file mode 100644 index 00000000000000..d464d13d715175 --- /dev/null +++ b/tests/specs/test/doc_ts_expect_error/mod.out @@ -0,0 +1,8 @@ +Check [WILDCARD]/mod.ts +Check [WILDCARD]/mod.ts$2-10.ts +running 0 tests from ./mod.ts +running 1 test from ./mod.ts$2-10.ts +[WILDCARD]/mod.ts$2-10.ts ... ok ([WILDCARD]ms) + +ok | 1 passed | 0 failed ([WILDCARD]ms) + diff --git a/tests/specs/test/doc_ts_expect_error/mod.ts b/tests/specs/test/doc_ts_expect_error/mod.ts new file mode 100644 index 00000000000000..eeace602a9ab3c --- /dev/null +++ b/tests/specs/test/doc_ts_expect_error/mod.ts @@ -0,0 +1,13 @@ +/** + * ```ts + * import { add } from "./mod.ts"; + * + * add(1, 2); + * + * // @ts-expect-error: can only add numbers + * add('1', '2'); + * ``` + */ +export function add(first: number, second: number) { + return first + second; +} From b7edca867b321728ce349721c419372c283000c5 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 12 Nov 2024 19:07:45 +0900 Subject: [PATCH 4/4] add more test --- tests/specs/test/markdown_ts_expect_error/__test__.jsonc | 5 +++++ tests/specs/test/markdown_ts_expect_error/main.md | 8 ++++++++ tests/specs/test/markdown_ts_expect_error/main.out | 6 ++++++ 3 files changed, 19 insertions(+) create mode 100644 tests/specs/test/markdown_ts_expect_error/__test__.jsonc create mode 100644 tests/specs/test/markdown_ts_expect_error/main.md create mode 100644 tests/specs/test/markdown_ts_expect_error/main.out diff --git a/tests/specs/test/markdown_ts_expect_error/__test__.jsonc b/tests/specs/test/markdown_ts_expect_error/__test__.jsonc new file mode 100644 index 00000000000000..ad73f6df6f522e --- /dev/null +++ b/tests/specs/test/markdown_ts_expect_error/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc main.md", + "exitCode": 0, + "output": "main.out" +} diff --git a/tests/specs/test/markdown_ts_expect_error/main.md b/tests/specs/test/markdown_ts_expect_error/main.md new file mode 100644 index 00000000000000..9be70e919cbd9c --- /dev/null +++ b/tests/specs/test/markdown_ts_expect_error/main.md @@ -0,0 +1,8 @@ +# Documentation + +This test case checks if `@ts-expect-error` comment works as expected. + +```ts +// @ts-expect-error +const a: string = 42; +``` diff --git a/tests/specs/test/markdown_ts_expect_error/main.out b/tests/specs/test/markdown_ts_expect_error/main.out new file mode 100644 index 00000000000000..65990cd3f0ad4b --- /dev/null +++ b/tests/specs/test/markdown_ts_expect_error/main.out @@ -0,0 +1,6 @@ +Check [WILDCARD]/main.md$5-9.ts +running 1 test from ./main.md$5-9.ts +[WILDCARD]/main.md$5-9.ts ... ok ([WILDCARD]ms) + +ok | 1 passed | 0 failed ([WILDCARD]ms) +