1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00
denoland-deno/cli
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
..
args feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
bench bench: fix CI (#25547) 2024-09-10 00:19:49 +00:00
cache refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
js refactor(cli/js): align error messages (#25406) 2024-09-04 09:19:55 +02:00
lsp feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
napi chore: forward v1.46.3 release commit to main (#25425) 2024-09-04 17:16:24 +00:00
npm refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
ops refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
schemas BREAKING: remove deprecated files config (#25535) 2024-09-09 16:33:09 +00:00
standalone chore: remove warnOnDeprecatedApi() (#25673) 2024-09-16 23:43:36 +00:00
tools feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
tsc feat(lsp): auto-import types with 'import type' (#25662) 2024-09-17 18:28:51 +01:00
util feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
auth_tokens.rs fix: trim space around DENO_AUTH_TOKENS (#25147) 2024-08-22 15:14:50 +02:00
build.rs feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
Cargo.toml chore: bump deno_lint to 0.66.0 (#25697) 2024-09-18 01:48:37 +00:00
cdp.rs chore: upgrade to rust 1.79 (#24207) 2024-06-14 17:10:57 +05:30
clippy.toml refactor(lsp): changes for lsp_types 0.97.0 (#25169) 2024-08-24 01:21:21 +01:00
deno.ico fix(cli): add icon and metadata to deno.exe on Windows (#6693) 2020-07-15 21:54:38 +02:00
emit.rs feat: deprecate import assertions (#25281) 2024-08-29 01:06:09 +00:00
entitlements.plist chore: start codesigning mac release builds (#21303) 2023-11-23 15:30:26 -07:00
errors.rs fix(compile): support workspace members importing other members (#24909) 2024-08-07 07:43:05 +00:00
factory.rs chore: upgrade deno_core (#25674) 2024-09-17 01:13:34 +00:00
file_fetcher.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
graph_container.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
graph_util.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
http_util.rs refactor: version module exports a single const struct (#25014) 2024-08-15 23:47:16 +02:00
integration_tests_runner.rs chore: continue tests/ re-org (#22396) 2024-02-12 17:13:14 -07:00
js.rs chore: rename __runtime_js_source to hmr (#24442) 2024-07-05 17:47:53 +05:30
jsr.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
main.rs feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
mainrt.rs fix(cli): update permission prompt message for compiled binaries (#24081) 2024-08-20 01:20:06 +00:00
module_loader.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
node.rs refactor: decouple node resolution from deno_core (#24724) 2024-07-25 19:08:14 -04:00
README.md docs(cli): do not need gen doc for cli (#17260) 2023-01-04 13:19:58 +01:00
resolver.rs fix(node/byonm): do not accidentally resolve bare node built-ins (#25543) 2024-09-09 21:35:41 +00:00
shared.rs chore: Rust 1.80.1 (#25089) 2024-08-18 22:24:56 -04:00
task_runner.rs fix: lock down allow-run permissions more (#25370) 2024-09-04 14:51:24 +02:00
version.rs build: Allow building 'rc' release from source (#25227) 2024-08-28 14:31:25 +00:00
worker.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00

Deno CLI Crate

crates

This provides the actual deno executable and the user-facing APIs.

The deno crate uses the deno_core to provide the executable.