0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 09:57:11 -05:00
deno/tests/specs/check
Yusuke Tanaka d5c00ef50e
feat(cli): evaluate code snippets in JSDoc and markdown (#25220)
This commit lets `deno test --doc` command actually evaluate code snippets in
JSDoc and markdown files.

## How it works

1. Extract code snippets from JSDoc or code fences
2. Convert them into pseudo files by wrapping them in `Deno.test(...)`
3. Register the pseudo files as in-memory files
4. Run type-check and evaluation

We apply some magic at the step 2 - let's say we have the following file named
`mod.ts` as an input:

````ts
/**
 * ```ts
 * import { assertEquals } from "jsr:@std/assert/equals";
 *
 * assertEquals(add(1, 2), 3);
 * ```
 */
export function add(a: number, b: number) {
  return a + b;
}
````

This is virtually transformed into:

```ts
import { assertEquals } from "jsr:@std/assert/equals";
import { add } from "files:///path/to/mod.ts";

Deno.test("mod.ts$2-7.ts", async () => {
  assertEquals(add(1, 2), 3);
});
```

Note that a new import statement is inserted here to make `add` function
available. In a nutshell, all items exported from `mod.ts` become available in
the generated pseudo file with this automatic import insertion.

The intention behind this design is that, from library user's standpoint, it
should be very obvious that this `add` function is what this example code is
attached to. Also, if there is an explicit import statement like
`import { add } from "./mod.ts"`, this import path `./mod.ts` is not helpful for
doc readers because they will need to import it in a different way.

The automatic import insertion has some edge cases, in particular where there is
a local variable in a snippet with the same name as one of the exported items.
This case is addressed by employing swc's scope analysis (see test cases for
more details).

## "type-checking only" mode stays around

This change will likely impact a lot of existing doc tests in the ecosystem
because some doc tests rely on the fact that they are not evaluated - some cause
side effects if executed, some throw errors at runtime although they do pass the
type check, etc. To help those tests gradually transition to the ones runnable
with the new `deno test --doc`, we will keep providing the ability to run
type-checking only via `deno check --doc`. Additionally there is a `--doc-only`
option added to the `check` subcommand too, which is useful when you want to
type-check on code snippets in markdown files, as normal `deno check` command
doesn't accept markdown.

## Demo

https://github.com/user-attachments/assets/47e9af73-d16e-472d-b09e-1853b9e8f5ce

---

Closes #4716
2024-09-17 21:35:48 -07:00
..
check_types_dts chore: migrate bench, publish, and more itests to spec tests (#23584) 2024-04-29 10:08:27 -04:00
check_with_excluded_file_specified chore: migrate bench, publish, and more itests to spec tests (#23584) 2024-04-29 10:08:27 -04:00
cjs_default_export chore: fix flaky net_listen_allow_localhost_4555 (#23726) 2024-05-07 17:21:56 +00:00
css_import feat(check): allow using side effect imports with unknown module kinds (ex. css modules) (#23392) 2024-04-16 16:46:31 -04:00
export_equals_declaration_file feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
extensionless chore: migrate bench, publish, and more itests to spec tests (#23584) 2024-04-29 10:08:27 -04:00
globbing feat: glob and directory support for deno check and deno cache cli arg paths (#25001) 2024-08-20 18:38:06 +00:00
jsx_import_source_types feat(cli): add support for jsxImportSourceTypes (#23419) 2024-04-30 18:12:35 +00:00
jsx_import_source_types_config feat(cli): add support for jsxImportSourceTypes (#23419) 2024-04-30 18:12:35 +00:00
npm_pkg_empty_main_entry fix(node): handle empty 'main' entry in pkg json (#23155) 2024-04-01 07:07:11 +01:00
package_json test: rewrite some ignored tests to spec tests (#25652) 2024-09-16 15:39:58 +02:00
package_json_auto_install test: rewrite some ignored tests to spec tests (#25652) 2024-09-16 15:39:58 +02:00
package_json_fail_check test: rewrite some ignored tests to spec tests (#25652) 2024-09-16 15:39:58 +02:00
package_json_with_deno_json test: rewrite some ignored tests to spec tests (#25652) 2024-09-16 15:39:58 +02:00
random_extension chore: migrate bench, publish, and more itests to spec tests (#23584) 2024-04-29 10:08:27 -04:00
typecheck_doc_duplicate_identifiers feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
typecheck_doc_failure feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
typecheck_doc_in_markdown feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
typecheck_doc_success feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
types_resolved_relative_config
unstable_suggestion refactor: move WebGPU, FFI and FS typings from unstable to stable (#25488) 2024-09-10 05:04:59 -07:00
use_unknown_in_catch_variables feat(lsp): turn on useUnknownInCatchVariables (#25474) 2024-09-06 12:23:59 +02:00
with_bare_import fix: update hint for deno add <package> (#25455) 2024-09-05 11:08:22 +02:00
workspace feat: npm workspace and better Deno workspace support (#24334) 2024-07-04 00:54:33 +00:00