Skip to content

Commit

Permalink
feat(semantic): add help message for invalid let x?: number (#5969)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Sep 22, 2024
1 parent 47c2faa commit 74d8714
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/oxc_semantic/src/checker/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,35 @@ pub fn check_variable_declaration(decl: &VariableDeclaration, ctx: &SemanticBuil
}
}

fn unexpected_optional(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected `?` operator").with_label(span)
fn unexpected_optional(span: Span, type_annotation: Option<&str>) -> OxcDiagnostic {
let d = OxcDiagnostic::error("Unexpected `?` operator").with_label(span);
if let Some(ty) = type_annotation {
d.with_help(format!("If you want an optional type, use `{ty} | undefined` instead."))
} else {
d
}
}

#[allow(clippy::cast_possible_truncation)]
pub fn check_variable_declarator(decl: &VariableDeclarator, ctx: &SemanticBuilder<'_>) {
// Check for `let x?: number;`
if decl.id.optional {
// NOTE: BindingPattern spans cover the identifier _and_ the type annotation.
let start = decl.id.span().start;
let Some(offset) = ctx.source_text[start as usize..].find('?') else { return };
let Some(offset) = ctx.source_text[start as usize..].find('?') else {
debug_assert!(false, "Optional flag not found in source text. This is likely indicates a bug in the parser.");
return;
};
let offset = start + offset as u32;
ctx.error(unexpected_optional(Span::new(offset, offset)));
let span = Span::new(offset, offset);
let ty = decl
.id
.type_annotation
.as_ref()
.map(|ty| ty.type_annotation.span())
.map(|span| &ctx.source_text[span]);

ctx.error(unexpected_optional(span, ty));
}
}

Expand Down
1 change: 1 addition & 0 deletions tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ Negative Passed: 20/20 (100.00%)
· ▲
2
╰────
help: If you want an optional type, use `number | undefined` instead.

× Unexpected token
╭─[misc/fail/oxc-5955-2.ts:3:8]
Expand Down

0 comments on commit 74d8714

Please sign in to comment.