diff options
author | Yusuke Tanaka <yusuktan@maguro.dev> | 2024-11-14 14:04:32 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-13 21:04:32 -0800 |
commit | 15fae197482a9ac84e0227200ef8f4d0d4e4bd33 (patch) | |
tree | be0cce33690ed742d950091bdb46c22ce04b0a1f /cli/util/extract.rs | |
parent | 6b5cb41545086a7a550c698620f5b7bb19b5524f (diff) |
fix(cli): preserve comments in doc tests (#26828)
This commit makes comments in code snippets in JSDoc or markdown
preserved when they are executed as tests. In particular, this is needed
to get TypeScript special comments such as `@ts-ignore` or
`@ts-expect-error` to work correctly.
Fixes #26728
Diffstat (limited to 'cli/util/extract.rs')
-rw-r--r-- | cli/util/extract.rs | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/cli/util/extract.rs b/cli/util/extract.rs index f577cbefe..be68202aa 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); @@ -1165,6 +1168,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 +1406,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 { |