mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
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
This commit is contained in:
parent
6b5cb41545
commit
15fae19748
7 changed files with 101 additions and 1 deletions
|
@ -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 {
|
||||
|
|
5
tests/specs/test/doc_ts_expect_error/__test__.jsonc
Normal file
5
tests/specs/test/doc_ts_expect_error/__test__.jsonc
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"args": "test --doc mod.ts",
|
||||
"exitCode": 0,
|
||||
"output": "mod.out"
|
||||
}
|
8
tests/specs/test/doc_ts_expect_error/mod.out
Normal file
8
tests/specs/test/doc_ts_expect_error/mod.out
Normal file
|
@ -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)
|
||||
|
13
tests/specs/test/doc_ts_expect_error/mod.ts
Normal file
13
tests/specs/test/doc_ts_expect_error/mod.ts
Normal file
|
@ -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;
|
||||
}
|
5
tests/specs/test/markdown_ts_expect_error/__test__.jsonc
Normal file
5
tests/specs/test/markdown_ts_expect_error/__test__.jsonc
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"args": "test --doc main.md",
|
||||
"exitCode": 0,
|
||||
"output": "main.out"
|
||||
}
|
8
tests/specs/test/markdown_ts_expect_error/main.md
Normal file
8
tests/specs/test/markdown_ts_expect_error/main.md
Normal file
|
@ -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;
|
||||
```
|
6
tests/specs/test/markdown_ts_expect_error/main.out
Normal file
6
tests/specs/test/markdown_ts_expect_error/main.out
Normal file
|
@ -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)
|
||||
|
Loading…
Reference in a new issue