1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-03 17:08:35 -05:00
denoland-deno/cli/util
Yusuke Tanaka 0ea71abdef
fix(cli): handle edge cases around exports in doc tests and default export (#25720)
This commit fixes issues with the pseudo test file generation logic,
namely:

- `export`s declared in snippets
- auto import insertion for `default export`

## Case 1: `export`s declared in snippets

In the previous implementation, `export`s declared in snippets were
moved to the top level of the module in the generated pseudo test file.
This is required because `export` must be at the top level.

This becomes a problem if such a `export` has a body, containing a
reference to a local variable. Suppose we extract this snippet from
JSDoc:

```ts
const logger = createLogger("my-awesome-module");

export function sum(a: number, b: number): number {
  logger.debug("sum called");
  return a + b;
}
```

This gets converted into the following invalid code (note that `export
function sum` is moved to the top level, but its body references
`logger` variable which can't be referenced from here):

```ts
export function sum(a: number, b: number): number {
  logger.debug("sum called");
  return a + b;
}

Deno.test("./base.ts$1-7.ts", async () => {
  const logger = createLogger("my-awesome-module");
});
```

To resolve this issue, this commit adds a logic to remove the `export`
keyword, allowing the exported items to stay in the `Deno.test` block
scope, like so:

```ts
Deno.test("./base.ts$1-7.ts", async () => {
  const logger = createLogger("my-awesome-module");

  function sum(a: number, b: number): number {
    logger.debug("sum called");
    return a + b;
  }
});
```

## Case 2: default export

Previously `default export foo` was not captured by the export
collector, so auto import insertion didn't work for this case. To put it
concretely, the following code snippet didn't work when run with `deno
test --doc` because `import foo from "file:///path/to/mod.ts"` didn't
get inserted automatically:

```ts
/**
 * ```ts
 * console.log(foo);
 * ```
 * 
 * @module
 */

const foo = 42;
export default foo;
```

This commit fixes this issue and the above example works fine.

---

Fixes #25718
2024-09-19 00:19:40 -07:00
..
progress_bar feat(clean): add progress bar (#25026) 2024-08-14 13:04:07 +02:00
sync fix: support npm:bindings and npm:callsites packages (#24727) 2024-07-26 09:08:15 +02:00
v8 refactor(lsp): Have JS drive TSC event loop in LSP (#23565) 2024-05-09 13:49:10 -07:00
archive.rs fix: Don't shell out to unzip in deno upgrade/compile (#24926) 2024-08-08 00:19:05 -07:00
checksum.rs perf(cli): use faster_hex (#22761) 2024-03-07 10:00:43 -07:00
console.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00
diff.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00
display.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00
draw_thread.rs fix: handle showing warnings while the progress bar is shown (#25187) 2024-08-23 22:07:59 +00:00
extract.rs fix(cli): handle edge cases around exports in doc tests and default export (#25720) 2024-09-19 00:19:40 -07:00
file_watcher.rs chore: upgrade to rust 1.80 (#24778) 2024-07-29 12:58:04 -04:00
fs.rs fix(cli): Map error kind to PermissionDenied when symlinking fails due to permissions (#25398) 2024-09-03 14:42:35 -07:00
logger.rs feat: support DENO_LOG env var instead of RUST_LOG (#25356) 2024-09-03 10:36:28 +02:00
mod.rs feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
path.rs BREAKING: remove "emit" and "map" from deno info output (#25468) 2024-09-05 14:22:13 +00:00
result.rs refactor(lsp): Have JS drive TSC event loop in LSP (#23565) 2024-05-09 13:49:10 -07:00
text_encoding.rs fix: better handling of npm resolution occurring on workers (#24094) 2024-06-05 17:04:16 +02:00
unix.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00
v8.rs refactor(lsp): Have JS drive TSC event loop in LSP (#23565) 2024-05-09 13:49:10 -07:00
windows.rs chore: update copyright to 2024 (#21753) 2024-01-01 19:58:21 +00:00