1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00
denoland-deno/cli/lsp
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
..
testing feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
analysis.rs fix(lsp): panic on url_to_uri() (#25238) 2024-08-28 05:15:48 +01:00
cache.rs perf(cache): single cache file for remote modules (#24983) 2024-08-26 23:59:17 +00:00
capabilities.rs refactor(lsp): changes for lsp_types 0.97.0 (#25169) 2024-08-24 01:21:21 +01:00
client.rs fix(lsp): panic on url_to_uri() (#25238) 2024-08-28 05:15:48 +01:00
code_lens.rs perf: update deno_ast to 0.41 (#24819) 2024-07-31 18:31:03 -04:00
completions.rs refactor(lsp): changes for lsp_types 0.97.0 (#25169) 2024-08-24 01:21:21 +01:00
config.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
diagnostics.rs fix(lsp): encode url parts before parsing as uri (#25509) 2024-09-11 11:11:39 +01:00
documents.rs BREAKING: remove "emit" and "map" from deno info output (#25468) 2024-09-05 14:22:13 +00:00
jsr.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
language_server.rs fix(lsp): properly resolve jsxImportSource for caching (#25688) 2024-09-17 18:29:19 +01:00
logging.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00
lsp_custom.rs refactor(lsp): changes for lsp_types 0.97.0 (#25169) 2024-08-24 01:21:21 +01:00
mod.rs perf(lsp): lock out requests until init is complete (#23998) 2024-05-29 01:26:43 +01:00
npm.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
parent_process_checker.rs fix(lsp): use a dedicated thread for the parent process checker (#21869) 2024-01-09 11:36:03 -05:00
path_to_regex.rs chore: upgrade to rust 1.79 (#24207) 2024-06-14 17:10:57 +05:30
performance.rs refactor(lsp): cleanup partially locking methods (#23723) 2024-05-08 06:34:42 +01:00
README.md docs: fix broken deno manual link (#20667) 2023-09-25 14:09:27 +02:00
refactor.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00
registries.rs refactor(permissions): split up Descriptor into Allow, Deny, and Query (#25508) 2024-09-16 21:39:37 +01:00
repl.rs fix(lsp): encode url parts before parsing as uri (#25509) 2024-09-11 11:11:39 +01:00
resolver.rs fix(byonm): resolve npm deps of jsr deps (#25399) 2024-09-04 14:00:44 +00:00
search.rs feat(lsp): jsr specifier completions (#22612) 2024-02-29 03:54:16 +00:00
semantic_tokens.rs perf(lsp): Cache semantic tokens for open documents (#23799) 2024-05-15 01:51:48 +00:00
text.rs fix(lsp): handle multiline semantic tokens (#23691) 2024-05-04 21:48:06 +01:00
tsc.rs feat(lsp): auto-import types with 'import type' (#25662) 2024-09-17 18:28:51 +01:00
urls.rs fix(lsp): encode url parts before parsing as uri (#25509) 2024-09-11 11:11:39 +01:00

Deno Language Server

The Deno Language Server provides a server implementation of the Language Server Protocol which is specifically tailored to provide a Deno view of code. It is integrated into the command line and can be started via the lsp sub-command.

This documentation has been moved to the Deno manual.