Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test --doc: allow import statements to be in a separate code block #25745

Open
skybrian opened this issue Sep 19, 2024 · 1 comment
Open

test --doc: allow import statements to be in a separate code block #25745

skybrian opened this issue Sep 19, 2024 · 1 comment

Comments

@skybrian
Copy link

skybrian commented Sep 19, 2024

I'm trying out the new deno test --doc feature in the Deno 2 release candidate. It works, but might be nicer if I didn't have to write out imports in each code block.

Version: 2.0.0-rc.4

Example

Here is an import that will be needed in the following examples:

```ts
import { assertEquals } from "@std/assert";
```

Here's an example of how to use assertEquals:

```ts
assertEquals("hello".length, 5);
```

Here's a second example:

```ts
assertEquals([1, 2, 3], [1, 2, 3]);
```

What it does now:

Check file:///Users/skybrian/Projects/deno/repeat-test/docs/example.md$3-6.ts
Check file:///Users/skybrian/Projects/deno/repeat-test/docs/example.md$9-12.ts
Check file:///Users/skybrian/Projects/deno/repeat-test/docs/example.md$15-18.ts
error: TS2304 [ERROR]: Cannot find name 'assertEquals'.
    assertEquals([
    ~~~~~~~~~~~~
    at file:///Users/skybrian/Projects/deno/repeat-test/docs/example.md$15-18.ts:2:5

TS2304 [ERROR]: Cannot find name 'assertEquals'.
    assertEquals("hello".length, 5);
    ~~~~~~~~~~~~
    at file:///Users/skybrian/Projects/deno/repeat-test/docs/example.md$9-12.ts:2:5

Found 2 errors.

What I'd like it to do:

Don't report any errors, because the imports were declared in a previous code block.

How would it work?

I don't know what's best, but here are some alternatives:

  • When there is a code block that only contains imports, automatically prepend it to each of the following code blocks.
  • When there is a code block that has no imports, reuse the imports from the previous code block.

Or, perhaps both rules?

@iuioiua
Copy link
Collaborator

iuioiua commented Sep 19, 2024

Why not just add the import statement to your code blocks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants