fix(check): ignore certain diagnostics in remote modules and when publishing (#23119)

Unused locals and parameters don't make sense to surface in remote
modules. Additionally, fast check can cause these kind of diagnostics
when publishing, so they should be ignored.

Closes #22959
This commit is contained in:
David Sherret 2024-03-31 16:39:40 -04:00 committed by GitHub
parent 0144594044
commit b8af46e007
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 73 additions and 25 deletions

View File

@ -200,28 +200,13 @@ impl TypeChecker {
check_mode: type_check_mode,
})?;
let mut diagnostics = if type_check_mode == TypeCheckMode::Local {
response.diagnostics.filter(|d| {
if let Some(file_name) = &d.file_name {
if !file_name.starts_with("http") {
if ModuleSpecifier::parse(file_name)
.map(|specifier| !self.node_resolver.in_npm_package(&specifier))
.unwrap_or(true)
{
Some(d.clone())
} else {
None
}
} else {
None
}
} else {
Some(d.clone())
}
})
} else {
response.diagnostics
};
let mut diagnostics = response.diagnostics.filter(|d| {
if self.is_remote_diagnostic(d) {
type_check_mode == TypeCheckMode::All && d.include_when_remote()
} else {
true
}
});
diagnostics.apply_fast_check_source_maps(&graph);
@ -239,6 +224,20 @@ impl TypeChecker {
Ok((graph, diagnostics))
}
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)
}
}
enum CheckHashResult {

View File

@ -924,6 +924,10 @@ async fn build_and_check_graph_for_publish(
},
)
.await?;
// ignore unused parameter diagnostics that may occur due to fast check
// not having function body implementations
let check_diagnostics =
check_diagnostics.filter(|d| d.include_when_remote());
if !check_diagnostics.is_empty() {
bail!(
concat!(

View File

@ -136,6 +136,13 @@ pub struct Diagnostic {
}
impl Diagnostic {
/// If this diagnostic should be included when it comes from a remote module.
pub fn include_when_remote(&self) -> bool {
/// TS6133: value is declared but its value is never read (noUnusedParameters and noUnusedLocals)
const TS6133: u64 = 6133;
self.code != TS6133
}
fn fmt_category_and_code(&self, f: &mut fmt::Formatter) -> fmt::Result {
let category = match self.category {
DiagnosticCategory::Error => "ERROR",
@ -275,11 +282,11 @@ impl Diagnostics {
/// Return a set of diagnostics where only the values where the predicate
/// returns `true` are included.
pub fn filter<P>(&self, predicate: P) -> Self
pub fn filter<P>(self, predicate: P) -> Self
where
P: FnMut(&Diagnostic) -> Option<Diagnostic>,
P: FnMut(&Diagnostic) -> bool,
{
let diagnostics = self.0.iter().filter_map(predicate).collect();
let diagnostics = self.0.into_iter().filter(predicate).collect();
Self(diagnostics)
}

View File

@ -0,0 +1,10 @@
{
"base": "jsr",
"steps": [{
"args": "check --all main.ts",
"output": "main.out"
}, {
"args": "publish --dry-run",
"output": "publish.out"
}]
}

View File

@ -0,0 +1,9 @@
{
"name": "@scope/package",
"version": "0.0.0",
"exports": "./main.ts",
"lock": false,
"compilerOptions": {
"noUnusedParameters": true
}
}

View File

@ -0,0 +1,4 @@
Download http://127.0.0.1:4250/@denotest/add/meta.json
Download http://127.0.0.1:4250/@denotest/add/1.0.0_meta.json
Download http://127.0.0.1:4250/@denotest/add/1.0.0/mod.ts
Check file:///[WILDLINE]/no_unused_params/main.ts

View File

@ -0,0 +1,5 @@
import * as inner from "jsr:@denotest/add";
export function add(a: number, b: number): number {
return inner.add(a, b);
}

View File

@ -0,0 +1,10 @@
Check file:///[WILDLINE]/main.ts
Checking for slow types in the public API...
Check file:///[WILDLINE]/main.ts
Simulating publish of @scope/package@0.0.0 with files:
file:///[WILDLINE]/__test__.jsonc ([WILDLINE])
file:///[WILDLINE]/deno.json ([WILDLINE])
file:///[WILDLINE]/main.out ([WILDLINE])
file:///[WILDLINE]/main.ts ([WILDLINE])
file:///[WILDLINE]/publish.out ([WILDLINE])
Warning Aborting due to --dry-run