2025-01-01 04:12:39 +09:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2022-09-02 10:54:40 -04:00
|
|
|
|
2023-03-11 11:43:45 -05:00
|
|
|
use std::collections::HashSet;
|
2024-01-10 17:40:30 -05:00
|
|
|
use std::collections::VecDeque;
|
2022-09-02 10:54:40 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
2025-01-08 14:52:32 -08:00
|
|
|
use deno_config::deno_json;
|
2022-09-02 10:54:40 -04:00
|
|
|
use deno_core::error::AnyError;
|
2025-01-08 14:52:32 -08:00
|
|
|
use deno_error::JsErrorBox;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::Module;
|
2025-01-03 16:49:56 -05:00
|
|
|
use deno_graph::ModuleError;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
2025-01-03 16:49:56 -05:00
|
|
|
use deno_graph::ModuleLoadError;
|
2024-02-07 11:25:14 -05:00
|
|
|
use deno_terminal::colors;
|
2022-09-02 10:54:40 -04:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use regex::Regex;
|
|
|
|
|
2024-09-04 17:39:30 +02:00
|
|
|
use crate::args::check_warn_tsconfig;
|
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-18 13:35:48 +09:00
|
|
|
use crate::args::CheckFlags;
|
2023-04-14 18:05:46 -04:00
|
|
|
use crate::args::CliOptions;
|
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-18 13:35:48 +09:00
|
|
|
use crate::args::Flags;
|
2022-09-02 10:54:40 -04:00
|
|
|
use crate::args::TsConfig;
|
2023-04-14 18:05:46 -04:00
|
|
|
use crate::args::TsConfigType;
|
|
|
|
use crate::args::TsTypeLib;
|
2022-09-02 10:54:40 -04:00
|
|
|
use crate::args::TypeCheckMode;
|
2024-05-29 14:38:18 -04:00
|
|
|
use crate::cache::CacheDBHash;
|
2023-04-14 18:05:46 -04:00
|
|
|
use crate::cache::Caches;
|
2022-09-02 10:54:40 -04:00
|
|
|
use crate::cache::FastInsecureHasher;
|
|
|
|
use crate::cache::TypeCheckCache;
|
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-18 13:35:48 +09:00
|
|
|
use crate::factory::CliFactory;
|
2025-01-03 16:49:56 -05:00
|
|
|
use crate::graph_util::maybe_additional_sloppy_imports_message;
|
2024-02-20 16:29:57 -05:00
|
|
|
use crate::graph_util::BuildFastCheckGraphOptions;
|
|
|
|
use crate::graph_util::ModuleGraphBuilder;
|
2024-12-31 11:29:07 -05:00
|
|
|
use crate::node::CliNodeResolver;
|
2025-01-13 17:35:18 -05:00
|
|
|
use crate::npm::installer::NpmInstaller;
|
2023-04-21 21:02:46 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2025-01-03 16:49:56 -05:00
|
|
|
use crate::sys::CliSys;
|
2022-09-02 10:54:40 -04:00
|
|
|
use crate::tsc;
|
2024-01-10 17:40:30 -05:00
|
|
|
use crate::tsc::Diagnostics;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::tsc::TypeCheckingCjsTracker;
|
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-18 13:35:48 +09:00
|
|
|
use crate::util::extract;
|
2024-05-28 22:34:57 +09:00
|
|
|
use crate::util::path::to_percent_decoded_str;
|
2022-09-02 10:54:40 -04:00
|
|
|
|
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-18 13:35:48 +09:00
|
|
|
pub async fn check(
|
|
|
|
flags: Arc<Flags>,
|
|
|
|
check_flags: CheckFlags,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
|
|
|
|
let main_graph_container = factory.main_module_graph_container().await?;
|
|
|
|
|
|
|
|
let specifiers =
|
|
|
|
main_graph_container.collect_specifiers(&check_flags.files)?;
|
|
|
|
if specifiers.is_empty() {
|
|
|
|
log::warn!("{} No matching files found.", colors::yellow("Warning"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let specifiers_for_typecheck = if check_flags.doc || check_flags.doc_only {
|
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
2024-09-26 02:50:54 +01:00
|
|
|
let root_permissions = factory.root_permissions_container()?;
|
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-18 13:35:48 +09:00
|
|
|
|
|
|
|
let mut specifiers_for_typecheck = if check_flags.doc {
|
|
|
|
specifiers.clone()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
for s in specifiers {
|
2024-09-26 02:50:54 +01:00
|
|
|
let file = file_fetcher.fetch(&s, root_permissions).await?;
|
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-18 13:35:48 +09:00
|
|
|
let snippet_files = extract::extract_snippet_files(file)?;
|
|
|
|
for snippet_file in snippet_files {
|
2024-12-16 18:39:40 -05:00
|
|
|
specifiers_for_typecheck.push(snippet_file.url.clone());
|
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-18 13:35:48 +09:00
|
|
|
file_fetcher.insert_memory_files(snippet_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
specifiers_for_typecheck
|
|
|
|
} else {
|
|
|
|
specifiers
|
|
|
|
};
|
|
|
|
|
|
|
|
main_graph_container
|
2024-09-18 12:15:13 -07:00
|
|
|
.check_specifiers(&specifiers_for_typecheck, None)
|
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-18 13:35:48 +09:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-09-02 10:54:40 -04:00
|
|
|
/// Options for performing a check of a module graph. Note that the decision to
|
|
|
|
/// emit or not is determined by the `ts_config` settings.
|
|
|
|
pub struct CheckOptions {
|
2024-02-20 16:29:57 -05:00
|
|
|
/// Whether to build the fast check type graph if necessary.
|
|
|
|
///
|
|
|
|
/// Note: For perf reasons, the fast check type graph is only
|
|
|
|
/// built if type checking is necessary.
|
|
|
|
pub build_fast_check_graph: bool,
|
2023-04-14 18:05:46 -04:00
|
|
|
/// Default type library to type check with.
|
|
|
|
pub lib: TsTypeLib,
|
|
|
|
/// Whether to log about any ignored compiler options.
|
|
|
|
pub log_ignored_options: bool,
|
2022-09-02 10:54:40 -04:00
|
|
|
/// If true, valid `.tsbuildinfo` files will be ignored and type checking
|
|
|
|
/// will always occur.
|
|
|
|
pub reload: bool,
|
2024-02-21 08:35:25 -05:00
|
|
|
/// Mode to type check with.
|
|
|
|
pub type_check_mode: TypeCheckMode,
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
pub struct TypeChecker {
|
|
|
|
caches: Arc<Caches>,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: Arc<TypeCheckingCjsTracker>,
|
2023-04-14 18:05:46 -04:00
|
|
|
cli_options: Arc<CliOptions>,
|
2024-02-20 16:29:57 -05:00
|
|
|
module_graph_builder: Arc<ModuleGraphBuilder>,
|
2025-01-13 17:35:18 -05:00
|
|
|
npm_installer: Option<Arc<NpmInstaller>>,
|
2024-12-31 11:29:07 -05:00
|
|
|
node_resolver: Arc<CliNodeResolver>,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2025-01-03 16:49:56 -05:00
|
|
|
sys: CliSys,
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
2025-01-08 14:52:32 -08:00
|
|
|
#[derive(Debug, thiserror::Error, deno_error::JsError)]
|
|
|
|
pub enum CheckError {
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
Diagnostics(#[from] Diagnostics),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
ConfigFile(#[from] deno_json::ConfigFileError),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
ToMaybeJsxImportSourceConfig(
|
|
|
|
#[from] deno_json::ToMaybeJsxImportSourceConfigError,
|
|
|
|
),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
TscExec(#[from] tsc::ExecError),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
Other(#[from] JsErrorBox),
|
|
|
|
}
|
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
impl TypeChecker {
|
2025-01-13 17:35:18 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-04-14 18:05:46 -04:00
|
|
|
pub fn new(
|
|
|
|
caches: Arc<Caches>,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: Arc<TypeCheckingCjsTracker>,
|
2023-04-14 18:05:46 -04:00
|
|
|
cli_options: Arc<CliOptions>,
|
2024-02-20 16:29:57 -05:00
|
|
|
module_graph_builder: Arc<ModuleGraphBuilder>,
|
2024-12-31 11:29:07 -05:00
|
|
|
node_resolver: Arc<CliNodeResolver>,
|
2025-01-13 17:35:18 -05:00
|
|
|
npm_installer: Option<Arc<NpmInstaller>>,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2025-01-03 16:49:56 -05:00
|
|
|
sys: CliSys,
|
2023-04-14 18:05:46 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
caches,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker,
|
2023-04-14 18:05:46 -04:00
|
|
|
cli_options,
|
2024-02-20 16:29:57 -05:00
|
|
|
module_graph_builder,
|
2023-04-17 15:36:23 -04:00
|
|
|
node_resolver,
|
2025-01-13 17:35:18 -05:00
|
|
|
npm_installer,
|
2023-04-14 18:05:46 -04:00
|
|
|
npm_resolver,
|
2025-01-03 16:49:56 -05:00
|
|
|
sys,
|
2023-04-14 18:05:46 -04:00
|
|
|
}
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
/// Type check the module graph.
|
|
|
|
///
|
|
|
|
/// It is expected that it is determined if a check and/or emit is validated
|
|
|
|
/// before the function is called.
|
|
|
|
pub async fn check(
|
|
|
|
&self,
|
2024-02-20 16:29:57 -05:00
|
|
|
graph: ModuleGraph,
|
2023-04-14 18:05:46 -04:00
|
|
|
options: CheckOptions,
|
2025-01-08 14:52:32 -08:00
|
|
|
) -> Result<Arc<ModuleGraph>, CheckError> {
|
2024-09-14 11:58:47 +01:00
|
|
|
let (graph, mut diagnostics) =
|
|
|
|
self.check_diagnostics(graph, options).await?;
|
|
|
|
diagnostics.emit_warnings();
|
2024-01-10 17:40:30 -05:00
|
|
|
if diagnostics.is_empty() {
|
2024-02-20 16:29:57 -05:00
|
|
|
Ok(graph)
|
2024-01-10 17:40:30 -05:00
|
|
|
} else {
|
|
|
|
Err(diagnostics.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Type check the module graph returning its diagnostics.
|
|
|
|
///
|
|
|
|
/// It is expected that it is determined if a check and/or emit is validated
|
|
|
|
/// before the function is called.
|
|
|
|
pub async fn check_diagnostics(
|
|
|
|
&self,
|
2024-02-20 16:29:57 -05:00
|
|
|
mut graph: ModuleGraph,
|
2024-01-10 17:40:30 -05:00
|
|
|
options: CheckOptions,
|
2025-01-08 14:52:32 -08:00
|
|
|
) -> Result<(Arc<ModuleGraph>, Diagnostics), CheckError> {
|
2024-03-01 10:54:46 -05:00
|
|
|
if !options.type_check_mode.is_true() || graph.roots.is_empty() {
|
2024-02-20 16:29:57 -05:00
|
|
|
return Ok((graph.into(), Default::default()));
|
2024-01-13 16:06:18 -05:00
|
|
|
}
|
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
// node built-in specifiers use the @types/node package to determine
|
|
|
|
// types, so inject that now (the caller should do this after the lockfile
|
|
|
|
// has been written)
|
2025-01-13 17:35:18 -05:00
|
|
|
if let Some(npm_installer) = &self.npm_installer {
|
2023-09-29 09:26:25 -04:00
|
|
|
if graph.has_node_specifier {
|
2025-01-13 17:35:18 -05:00
|
|
|
npm_installer.inject_synthetic_types_node_package().await?;
|
2023-09-29 09:26:25 -04:00
|
|
|
}
|
2023-04-14 18:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log::debug!("Type checking.");
|
|
|
|
let ts_config_result = self
|
|
|
|
.cli_options
|
|
|
|
.resolve_ts_config_for_emit(TsConfigType::Check { lib: options.lib })?;
|
|
|
|
if options.log_ignored_options {
|
2024-09-04 17:39:30 +02:00
|
|
|
check_warn_tsconfig(&ts_config_result);
|
2023-04-14 18:05:46 -04:00
|
|
|
}
|
|
|
|
|
2024-02-21 08:35:25 -05:00
|
|
|
let type_check_mode = options.type_check_mode;
|
2023-04-14 18:05:46 -04:00
|
|
|
let ts_config = ts_config_result.ts_config;
|
2025-01-03 16:49:56 -05:00
|
|
|
let cache = TypeCheckCache::new(self.caches.type_checking_cache_db());
|
|
|
|
let check_js = ts_config.get_check_js();
|
|
|
|
|
|
|
|
// add fast check to the graph before getting the roots
|
|
|
|
if options.build_fast_check_graph {
|
|
|
|
self.module_graph_builder.build_fast_check_graph(
|
|
|
|
&mut graph,
|
|
|
|
BuildFastCheckGraphOptions {
|
|
|
|
workspace_fast_check: deno_graph::WorkspaceFastCheckOption::Disabled,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filter_remote_diagnostics = |d: &tsc::Diagnostic| {
|
|
|
|
if self.is_remote_diagnostic(d) {
|
|
|
|
type_check_mode == TypeCheckMode::All && d.include_when_remote()
|
|
|
|
} else {
|
|
|
|
true
|
2023-10-03 19:05:06 -04:00
|
|
|
}
|
2023-04-14 18:05:46 -04:00
|
|
|
};
|
2025-01-03 16:49:56 -05:00
|
|
|
let TscRoots {
|
|
|
|
roots: root_names,
|
|
|
|
missing_diagnostics,
|
|
|
|
maybe_check_hash,
|
|
|
|
} = get_tsc_roots(
|
|
|
|
&self.sys,
|
|
|
|
&graph,
|
|
|
|
check_js,
|
|
|
|
self.npm_resolver.check_state_hash(),
|
|
|
|
type_check_mode,
|
|
|
|
&ts_config,
|
|
|
|
);
|
|
|
|
|
|
|
|
let missing_diagnostics =
|
|
|
|
missing_diagnostics.filter(filter_remote_diagnostics);
|
|
|
|
|
|
|
|
if root_names.is_empty() && missing_diagnostics.is_empty() {
|
|
|
|
return Ok((graph.into(), Default::default()));
|
|
|
|
}
|
2023-10-03 19:05:06 -04:00
|
|
|
if !options.reload {
|
2025-01-03 16:49:56 -05:00
|
|
|
// do not type check if we know this is type checked
|
2023-10-03 19:05:06 -04:00
|
|
|
if let Some(check_hash) = maybe_check_hash {
|
|
|
|
if cache.has_check_hash(check_hash) {
|
2024-02-20 16:29:57 -05:00
|
|
|
log::debug!("Already type checked.");
|
|
|
|
return Ok((graph.into(), Default::default()));
|
2023-10-03 19:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 18:05:46 -04:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
for root in &graph.roots {
|
2023-01-04 20:20:36 +08:00
|
|
|
let root_str = root.as_str();
|
2024-05-28 22:34:57 +09:00
|
|
|
log::info!(
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Check"),
|
|
|
|
to_percent_decoded_str(root_str)
|
|
|
|
);
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
2023-02-09 22:00:23 -05:00
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
// while there might be multiple roots, we can't "merge" the build info, so we
|
|
|
|
// try to retrieve the build info for first root, which is the most common use
|
|
|
|
// case.
|
|
|
|
let maybe_tsbuildinfo = if options.reload {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
cache.get_tsbuildinfo(&graph.roots[0])
|
|
|
|
};
|
|
|
|
// to make tsc build info work, we need to consistently hash modules, so that
|
|
|
|
// tsc can better determine if an emit is still valid or not, so we provide
|
|
|
|
// that data here.
|
2025-01-03 16:49:56 -05:00
|
|
|
let tsconfig_hash_data = FastInsecureHasher::new_deno_versioned()
|
2023-07-10 17:45:09 -04:00
|
|
|
.write(&ts_config.as_bytes())
|
|
|
|
.finish();
|
2024-02-20 16:29:57 -05:00
|
|
|
let graph = Arc::new(graph);
|
2023-04-14 18:05:46 -04:00
|
|
|
let response = tsc::exec(tsc::Request {
|
|
|
|
config: ts_config,
|
2024-02-20 16:29:57 -05:00
|
|
|
debug: self.cli_options.log_level() == Some(log::Level::Debug),
|
2023-04-14 18:05:46 -04:00
|
|
|
graph: graph.clone(),
|
2025-01-03 16:49:56 -05:00
|
|
|
hash_data: tsconfig_hash_data,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Some(tsc::RequestNpmState {
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: self.cjs_tracker.clone(),
|
2023-09-28 16:43:45 -04:00
|
|
|
node_resolver: self.node_resolver.clone(),
|
|
|
|
npm_resolver: self.npm_resolver.clone(),
|
|
|
|
}),
|
2023-04-14 18:05:46 -04:00
|
|
|
maybe_tsbuildinfo,
|
|
|
|
root_names,
|
|
|
|
check_mode: type_check_mode,
|
|
|
|
})?;
|
|
|
|
|
2025-01-03 16:49:56 -05:00
|
|
|
let response_diagnostics =
|
|
|
|
response.diagnostics.filter(filter_remote_diagnostics);
|
|
|
|
|
|
|
|
let mut diagnostics = missing_diagnostics;
|
|
|
|
diagnostics.extend(response_diagnostics);
|
2022-09-02 10:54:40 -04:00
|
|
|
|
2024-01-10 17:40:30 -05:00
|
|
|
diagnostics.apply_fast_check_source_maps(&graph);
|
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
if let Some(tsbuildinfo) = response.maybe_tsbuildinfo {
|
|
|
|
cache.set_tsbuildinfo(&graph.roots[0], &tsbuildinfo);
|
|
|
|
}
|
2022-09-02 10:54:40 -04:00
|
|
|
|
2023-04-14 18:05:46 -04:00
|
|
|
if diagnostics.is_empty() {
|
2023-10-03 19:05:06 -04:00
|
|
|
if let Some(check_hash) = maybe_check_hash {
|
|
|
|
cache.add_check_hash(check_hash);
|
|
|
|
}
|
2023-04-14 18:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log::debug!("{}", response.stats);
|
2022-09-02 10:54:40 -04:00
|
|
|
|
2024-02-20 16:29:57 -05:00
|
|
|
Ok((graph, diagnostics))
|
2023-04-14 18:05:46 -04:00
|
|
|
}
|
2024-03-31 16:39:40 -04:00
|
|
|
|
|
|
|
fn is_remote_diagnostic(&self, d: &tsc::Diagnostic) -> bool {
|
|
|
|
let Some(file_name) = &d.file_name else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if file_name.starts_with("https://") || file_name.starts_with("http://") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// check if in an npm package
|
|
|
|
let Ok(specifier) = ModuleSpecifier::parse(file_name) else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
self.node_resolver.in_npm_package(&specifier)
|
|
|
|
}
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
2025-01-03 16:49:56 -05:00
|
|
|
struct TscRoots {
|
|
|
|
roots: Vec<(ModuleSpecifier, MediaType)>,
|
|
|
|
missing_diagnostics: tsc::Diagnostics,
|
|
|
|
maybe_check_hash: Option<CacheDBHash>,
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
2025-01-03 16:49:56 -05:00
|
|
|
/// Transform the graph into root specifiers that we can feed `tsc`. We have to
|
|
|
|
/// provide the media type for root modules because `tsc` does not "resolve" the
|
|
|
|
/// media type like other modules, as well as a root specifier needs any
|
|
|
|
/// redirects resolved. We need to include all the emittable files in
|
|
|
|
/// the roots, so they get type checked and optionally emitted,
|
|
|
|
/// otherwise they would be ignored if only imported into JavaScript.
|
|
|
|
fn get_tsc_roots(
|
|
|
|
sys: &CliSys,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: &ModuleGraph,
|
2025-01-03 16:49:56 -05:00
|
|
|
check_js: bool,
|
|
|
|
npm_cache_state_hash: Option<u64>,
|
2023-04-14 18:05:46 -04:00
|
|
|
type_check_mode: TypeCheckMode,
|
|
|
|
ts_config: &TsConfig,
|
2025-01-03 16:49:56 -05:00
|
|
|
) -> TscRoots {
|
|
|
|
fn maybe_get_check_entry(
|
|
|
|
module: &deno_graph::Module,
|
|
|
|
check_js: bool,
|
|
|
|
hasher: Option<&mut FastInsecureHasher>,
|
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2023-02-22 14:15:25 -05:00
|
|
|
match module {
|
2024-01-31 22:15:22 -05:00
|
|
|
Module::Js(module) => {
|
2025-01-03 16:49:56 -05:00
|
|
|
let result = match module.media_type {
|
2023-02-22 14:15:25 -05:00
|
|
|
MediaType::TypeScript
|
2025-01-03 16:49:56 -05:00
|
|
|
| MediaType::Tsx
|
2023-02-22 14:15:25 -05:00
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
2025-01-03 16:49:56 -05:00
|
|
|
| MediaType::Dts
|
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Dcts => {
|
|
|
|
Some((module.specifier.clone(), module.media_type))
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Jsx => {
|
2025-01-03 16:49:56 -05:00
|
|
|
if check_js || has_ts_check(module.media_type, &module.source) {
|
|
|
|
Some((module.specifier.clone(), module.media_type))
|
|
|
|
} else {
|
|
|
|
None
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MediaType::Json
|
2025-01-03 16:49:56 -05:00
|
|
|
| MediaType::Wasm
|
2024-11-01 12:27:00 -04:00
|
|
|
| MediaType::Css
|
2023-02-22 14:15:25 -05:00
|
|
|
| MediaType::SourceMap
|
2025-01-03 16:49:56 -05:00
|
|
|
| MediaType::Unknown => None,
|
|
|
|
};
|
|
|
|
if result.is_some() {
|
|
|
|
if let Some(hasher) = hasher {
|
|
|
|
hasher.write_str(module.specifier.as_str());
|
|
|
|
hasher.write_str(
|
|
|
|
// the fast check module will only be set when publishing
|
|
|
|
module
|
|
|
|
.fast_check_module()
|
|
|
|
.map(|s| s.source.as_ref())
|
|
|
|
.unwrap_or(&module.source),
|
|
|
|
);
|
|
|
|
}
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
2025-01-03 16:49:56 -05:00
|
|
|
result
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
2023-07-26 17:23:07 -04:00
|
|
|
Module::Node(_) => {
|
|
|
|
// the @types/node package will be in the resolved
|
2025-01-03 16:49:56 -05:00
|
|
|
// snapshot so don't bother including it in the hash
|
|
|
|
None
|
2023-07-26 17:23:07 -04:00
|
|
|
}
|
|
|
|
Module::Npm(_) => {
|
|
|
|
// don't bother adding this specifier to the hash
|
|
|
|
// because what matters is the resolved npm snapshot,
|
|
|
|
// which is hashed below
|
2025-01-03 16:49:56 -05:00
|
|
|
None
|
2023-07-26 17:23:07 -04:00
|
|
|
}
|
|
|
|
Module::Json(module) => {
|
2025-01-03 16:49:56 -05:00
|
|
|
if let Some(hasher) = hasher {
|
|
|
|
hasher.write_str(module.specifier.as_str());
|
|
|
|
hasher.write_str(&module.source);
|
|
|
|
}
|
|
|
|
None
|
2023-07-26 17:23:07 -04:00
|
|
|
}
|
2024-11-19 18:59:23 -05:00
|
|
|
Module::Wasm(module) => {
|
2025-01-03 16:49:56 -05:00
|
|
|
if let Some(hasher) = hasher {
|
|
|
|
hasher.write_str(module.specifier.as_str());
|
|
|
|
hasher.write_str(&module.source_dts);
|
|
|
|
}
|
|
|
|
Some((module.specifier.clone(), MediaType::Dmts))
|
2024-11-19 18:59:23 -05:00
|
|
|
}
|
2023-07-26 17:23:07 -04:00
|
|
|
Module::External(module) => {
|
2025-01-03 16:49:56 -05:00
|
|
|
if let Some(hasher) = hasher {
|
|
|
|
hasher.write_str(module.specifier.as_str());
|
|
|
|
}
|
|
|
|
None
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-03 16:49:56 -05:00
|
|
|
let mut result = TscRoots {
|
|
|
|
roots: Vec::with_capacity(graph.specifiers_count()),
|
|
|
|
missing_diagnostics: Default::default(),
|
|
|
|
maybe_check_hash: None,
|
|
|
|
};
|
|
|
|
let mut maybe_hasher = npm_cache_state_hash.map(|npm_cache_state_hash| {
|
|
|
|
let mut hasher = FastInsecureHasher::new_deno_versioned();
|
|
|
|
hasher.write_hashable(npm_cache_state_hash);
|
|
|
|
hasher.write_u8(match type_check_mode {
|
|
|
|
TypeCheckMode::All => 0,
|
|
|
|
TypeCheckMode::Local => 1,
|
|
|
|
TypeCheckMode::None => 2,
|
|
|
|
});
|
|
|
|
hasher.write_hashable(graph.has_node_specifier);
|
|
|
|
hasher.write(&ts_config.as_bytes());
|
|
|
|
hasher
|
|
|
|
});
|
2023-03-11 11:43:45 -05:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
if graph.has_node_specifier {
|
2023-01-24 15:05:54 +01:00
|
|
|
// inject a specifier that will resolve node types
|
2025-01-03 16:49:56 -05:00
|
|
|
result.roots.push((
|
2023-01-24 15:05:54 +01:00
|
|
|
ModuleSpecifier::parse("asset:///node_types.d.ts").unwrap(),
|
|
|
|
MediaType::Dts,
|
|
|
|
));
|
|
|
|
}
|
2023-03-11 11:43:45 -05:00
|
|
|
|
2024-01-10 17:40:30 -05:00
|
|
|
let mut seen =
|
|
|
|
HashSet::with_capacity(graph.imports.len() + graph.specifiers_count());
|
|
|
|
let mut pending = VecDeque::new();
|
2023-03-11 11:43:45 -05:00
|
|
|
|
|
|
|
// put in the global types first so that they're resolved before anything else
|
2025-01-03 16:49:56 -05:00
|
|
|
let get_import_specifiers = || {
|
|
|
|
graph
|
|
|
|
.imports
|
|
|
|
.values()
|
|
|
|
.flat_map(|i| i.dependencies.values())
|
|
|
|
.filter_map(|dep| dep.get_type().or_else(|| dep.get_code()))
|
|
|
|
};
|
|
|
|
for specifier in get_import_specifiers() {
|
|
|
|
let specifier = graph.resolve(specifier);
|
|
|
|
if seen.insert(specifier) {
|
|
|
|
pending.push_back((specifier, false));
|
2023-03-11 11:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then the roots
|
|
|
|
for root in &graph.roots {
|
2024-01-10 17:40:30 -05:00
|
|
|
let specifier = graph.resolve(root);
|
2025-01-03 16:49:56 -05:00
|
|
|
if seen.insert(specifier) {
|
|
|
|
pending.push_back((specifier, false));
|
2023-03-11 11:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 17:40:30 -05:00
|
|
|
// now walk the graph that only includes the fast check dependencies
|
2025-01-03 16:49:56 -05:00
|
|
|
while let Some((specifier, is_dynamic)) = pending.pop_front() {
|
|
|
|
let module = match graph.try_get(specifier) {
|
|
|
|
Ok(Some(module)) => module,
|
|
|
|
Ok(None) => continue,
|
|
|
|
Err(ModuleError::Missing(specifier, maybe_range)) => {
|
|
|
|
if !is_dynamic {
|
|
|
|
result
|
|
|
|
.missing_diagnostics
|
|
|
|
.push(tsc::Diagnostic::from_missing_error(
|
|
|
|
specifier,
|
|
|
|
maybe_range.as_ref(),
|
|
|
|
maybe_additional_sloppy_imports_message(sys, specifier),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(ModuleError::LoadingErr(
|
|
|
|
specifier,
|
|
|
|
maybe_range,
|
|
|
|
ModuleLoadError::Loader(_),
|
|
|
|
)) => {
|
|
|
|
// these will be errors like attempting to load a directory
|
|
|
|
if !is_dynamic {
|
|
|
|
result
|
|
|
|
.missing_diagnostics
|
|
|
|
.push(tsc::Diagnostic::from_missing_error(
|
|
|
|
specifier,
|
|
|
|
maybe_range.as_ref(),
|
|
|
|
maybe_additional_sloppy_imports_message(sys, specifier),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(_) => continue,
|
2024-01-10 17:40:30 -05:00
|
|
|
};
|
2025-01-03 16:49:56 -05:00
|
|
|
if is_dynamic && !seen.insert(specifier) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(entry) =
|
|
|
|
maybe_get_check_entry(module, check_js, maybe_hasher.as_mut())
|
|
|
|
{
|
|
|
|
result.roots.push(entry);
|
2024-01-10 17:40:30 -05:00
|
|
|
}
|
2025-01-03 16:49:56 -05:00
|
|
|
|
|
|
|
let mut maybe_module_dependencies = None;
|
|
|
|
let mut maybe_types_dependency = None;
|
|
|
|
if let Module::Js(module) = module {
|
|
|
|
maybe_module_dependencies = Some(module.dependencies_prefer_fast_check());
|
|
|
|
maybe_types_dependency = module
|
|
|
|
.maybe_types_dependency
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|d| d.dependency.ok());
|
|
|
|
} else if let Module::Wasm(module) = module {
|
|
|
|
maybe_module_dependencies = Some(&module.dependencies);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_specifier<'a>(
|
|
|
|
graph: &'a ModuleGraph,
|
|
|
|
seen: &mut HashSet<&'a ModuleSpecifier>,
|
|
|
|
pending: &mut VecDeque<(&'a ModuleSpecifier, bool)>,
|
|
|
|
specifier: &'a ModuleSpecifier,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) {
|
|
|
|
let specifier = graph.resolve(specifier);
|
|
|
|
if is_dynamic {
|
|
|
|
if !seen.contains(specifier) {
|
|
|
|
pending.push_back((specifier, true));
|
|
|
|
}
|
|
|
|
} else if seen.insert(specifier) {
|
|
|
|
pending.push_back((specifier, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(deps) = maybe_module_dependencies {
|
2024-01-10 17:40:30 -05:00
|
|
|
for dep in deps.values() {
|
|
|
|
// walk both the code and type dependencies
|
|
|
|
if let Some(specifier) = dep.get_code() {
|
2025-01-03 16:49:56 -05:00
|
|
|
handle_specifier(
|
|
|
|
graph,
|
|
|
|
&mut seen,
|
|
|
|
&mut pending,
|
|
|
|
specifier,
|
|
|
|
dep.is_dynamic,
|
|
|
|
);
|
2024-01-10 17:40:30 -05:00
|
|
|
}
|
|
|
|
if let Some(specifier) = dep.get_type() {
|
2025-01-03 16:49:56 -05:00
|
|
|
handle_specifier(
|
|
|
|
graph,
|
|
|
|
&mut seen,
|
|
|
|
&mut pending,
|
|
|
|
specifier,
|
|
|
|
dep.is_dynamic,
|
|
|
|
);
|
2024-01-10 17:40:30 -05:00
|
|
|
}
|
|
|
|
}
|
2025-01-03 16:49:56 -05:00
|
|
|
}
|
2024-01-10 17:40:30 -05:00
|
|
|
|
2025-01-03 16:49:56 -05:00
|
|
|
if let Some(dep) = maybe_types_dependency {
|
|
|
|
handle_specifier(graph, &mut seen, &mut pending, &dep.specifier, false);
|
2023-03-11 11:43:45 -05:00
|
|
|
}
|
2024-01-10 17:40:30 -05:00
|
|
|
}
|
2023-03-21 11:46:40 -04:00
|
|
|
|
2025-01-03 16:49:56 -05:00
|
|
|
result.maybe_check_hash =
|
|
|
|
maybe_hasher.map(|hasher| CacheDBHash::new(hasher.finish()));
|
|
|
|
|
2023-01-24 15:05:54 +01:00
|
|
|
result
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Matches the `@ts-check` pragma.
|
|
|
|
static TS_CHECK_RE: Lazy<Regex> =
|
2023-04-13 09:08:01 +08:00
|
|
|
lazy_regex::lazy_regex!(r#"(?i)^\s*@ts-check(?:\s+|$)"#);
|
2022-09-02 10:54:40 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
fn has_ts_check(media_type: MediaType, file_text: &str) -> bool {
|
2022-09-02 10:54:40 -04:00
|
|
|
match &media_type {
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Jsx => get_leading_comments(file_text)
|
|
|
|
.iter()
|
|
|
|
.any(|text| TS_CHECK_RE.is_match(text)),
|
2023-02-22 14:15:25 -05:00
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Dts
|
|
|
|
| MediaType::Dcts
|
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Tsx
|
|
|
|
| MediaType::Json
|
|
|
|
| MediaType::Wasm
|
2024-11-01 12:27:00 -04:00
|
|
|
| MediaType::Css
|
2023-02-22 14:15:25 -05:00
|
|
|
| MediaType::SourceMap
|
|
|
|
| MediaType::Unknown => false,
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_leading_comments(file_text: &str) -> Vec<String> {
|
|
|
|
let mut chars = file_text.chars().peekable();
|
|
|
|
|
|
|
|
// skip over the shebang
|
|
|
|
if file_text.starts_with("#!") {
|
|
|
|
// skip until the end of the line
|
|
|
|
for c in chars.by_ref() {
|
|
|
|
if c == '\n' {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut results = Vec::new();
|
|
|
|
// now handle the comments
|
|
|
|
while chars.peek().is_some() {
|
|
|
|
// skip over any whitespace
|
|
|
|
while chars
|
|
|
|
.peek()
|
|
|
|
.map(|c| char::is_whitespace(*c))
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
chars.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if chars.next() != Some('/') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match chars.next() {
|
|
|
|
Some('/') => {
|
|
|
|
let mut text = String::new();
|
|
|
|
for c in chars.by_ref() {
|
|
|
|
if c == '\n' {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
text.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results.push(text);
|
|
|
|
}
|
|
|
|
Some('*') => {
|
|
|
|
let mut text = String::new();
|
|
|
|
while let Some(c) = chars.next() {
|
|
|
|
if c == '*' && chars.peek() == Some(&'/') {
|
|
|
|
chars.next();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
text.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results.push(text);
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
|
|
|
|
use super::get_leading_comments;
|
|
|
|
use super::has_ts_check;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_leading_comments_test() {
|
|
|
|
assert_eq!(
|
|
|
|
get_leading_comments(
|
|
|
|
"#!/usr/bin/env deno\r\n// test\n/* 1 *//*2*///3\n//\n /**/ /*4 */"
|
|
|
|
),
|
|
|
|
vec![
|
|
|
|
" test".to_string(),
|
|
|
|
" 1 ".to_string(),
|
|
|
|
"2".to_string(),
|
|
|
|
"3".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
"4 ".to_string(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_leading_comments("//1 /* */ \na;"),
|
|
|
|
vec!["1 /* */ ".to_string(),]
|
|
|
|
);
|
|
|
|
assert_eq!(get_leading_comments("//"), vec!["".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn has_ts_check_test() {
|
|
|
|
assert!(has_ts_check(
|
|
|
|
MediaType::JavaScript,
|
2023-02-22 14:15:25 -05:00
|
|
|
"// @ts-check\nconsole.log(5);"
|
2022-09-02 10:54:40 -04:00
|
|
|
));
|
|
|
|
assert!(has_ts_check(
|
|
|
|
MediaType::JavaScript,
|
2023-02-22 14:15:25 -05:00
|
|
|
"// deno-lint-ignore\n// @ts-check\n"
|
2022-09-02 10:54:40 -04:00
|
|
|
));
|
|
|
|
assert!(!has_ts_check(
|
|
|
|
MediaType::JavaScript,
|
2023-02-22 14:15:25 -05:00
|
|
|
"test;\n// @ts-check\n"
|
2022-09-02 10:54:40 -04:00
|
|
|
));
|
|
|
|
assert!(!has_ts_check(
|
|
|
|
MediaType::JavaScript,
|
2023-02-22 14:15:25 -05:00
|
|
|
"// ts-check\nconsole.log(5);"
|
2022-09-02 10:54:40 -04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|