From f1551d64bc6ee4bba73b9155c09f590c048a37a9 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:01:47 +0000 Subject: [PATCH] fix(semantic): `?` on variable declaration type annotations is a syntax error (#5956) Closes #5955 --- crates/oxc_semantic/src/checker/typescript.rs | 2 +- tasks/coverage/misc/fail/oxc-5955-1.ts | 2 ++ tasks/coverage/misc/fail/oxc-5955-2.ts | 4 +++ tasks/coverage/misc/fail/oxc-5955-3.ts | 3 ++ tasks/coverage/misc/pass/oxc-5955.ts | 7 +++++ tasks/coverage/snapshots/codegen_misc.snap | 4 +-- tasks/coverage/snapshots/parser_misc.snap | 29 +++++++++++++++++-- tasks/coverage/snapshots/semantic_misc.snap | 12 ++++++-- .../coverage/snapshots/transformer_misc.snap | 4 +-- 9 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 tasks/coverage/misc/fail/oxc-5955-1.ts create mode 100644 tasks/coverage/misc/fail/oxc-5955-2.ts create mode 100644 tasks/coverage/misc/fail/oxc-5955-3.ts create mode 100644 tasks/coverage/misc/pass/oxc-5955.ts diff --git a/crates/oxc_semantic/src/checker/typescript.rs b/crates/oxc_semantic/src/checker/typescript.rs index 12fea9138c631..f0a7f90d473de 100644 --- a/crates/oxc_semantic/src/checker/typescript.rs +++ b/crates/oxc_semantic/src/checker/typescript.rs @@ -52,7 +52,7 @@ fn unexpected_optional(span: Span) -> OxcDiagnostic { #[allow(clippy::cast_possible_truncation)] pub fn check_variable_declarator(decl: &VariableDeclarator, ctx: &SemanticBuilder<'_>) { if decl.id.optional { - let start = decl.id.span().end; + let start = decl.id.span().start; let Some(offset) = ctx.source_text[start as usize..].find('?') else { return }; let offset = start + offset as u32; ctx.error(unexpected_optional(Span::new(offset, offset))); diff --git a/tasks/coverage/misc/fail/oxc-5955-1.ts b/tasks/coverage/misc/fail/oxc-5955-1.ts new file mode 100644 index 0000000000000..7b44badf75417 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-5955-1.ts @@ -0,0 +1,2 @@ +const x?: number = 1; + diff --git a/tasks/coverage/misc/fail/oxc-5955-2.ts b/tasks/coverage/misc/fail/oxc-5955-2.ts new file mode 100644 index 0000000000000..4a06ff6ecd3fc --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-5955-2.ts @@ -0,0 +1,4 @@ + +interface B { + e()?: number; +} diff --git a/tasks/coverage/misc/fail/oxc-5955-3.ts b/tasks/coverage/misc/fail/oxc-5955-3.ts new file mode 100644 index 0000000000000..56f705d115c00 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-5955-3.ts @@ -0,0 +1,3 @@ +class A { + [key: string]?: number; +} diff --git a/tasks/coverage/misc/pass/oxc-5955.ts b/tasks/coverage/misc/pass/oxc-5955.ts new file mode 100644 index 0000000000000..ce6b0ba9230d9 --- /dev/null +++ b/tasks/coverage/misc/pass/oxc-5955.ts @@ -0,0 +1,7 @@ +const a = (b?: number) => b; +class B { + c?: number = 1; +} +interface C { + d?: number; +} diff --git a/tasks/coverage/snapshots/codegen_misc.snap b/tasks/coverage/snapshots/codegen_misc.snap index 84323535ec9e6..c5be53e729ad4 100644 --- a/tasks/coverage/snapshots/codegen_misc.snap +++ b/tasks/coverage/snapshots/codegen_misc.snap @@ -1,3 +1,3 @@ codegen_misc Summary: -AST Parsed : 29/29 (100.00%) -Positive Passed: 29/29 (100.00%) +AST Parsed : 30/30 (100.00%) +Positive Passed: 30/30 (100.00%) diff --git a/tasks/coverage/snapshots/parser_misc.snap b/tasks/coverage/snapshots/parser_misc.snap index 295aee31d0d9e..15579535e358c 100644 --- a/tasks/coverage/snapshots/parser_misc.snap +++ b/tasks/coverage/snapshots/parser_misc.snap @@ -1,7 +1,7 @@ parser_misc Summary: -AST Parsed : 29/29 (100.00%) -Positive Passed: 29/29 (100.00%) -Negative Passed: 17/17 (100.00%) +AST Parsed : 30/30 (100.00%) +Positive Passed: 30/30 (100.00%) +Negative Passed: 20/20 (100.00%) × Unexpected token ╭─[misc/fail/oxc-169.js:2:1] @@ -244,6 +244,29 @@ Negative Passed: 17/17 (100.00%) · ─────── ╰──── + × Unexpected `?` operator + ╭─[misc/fail/oxc-5955-1.ts:1:8] + 1 │ const x?: number = 1; + · ▲ + 2 │ + ╰──── + + × Unexpected token + ╭─[misc/fail/oxc-5955-2.ts:3:8] + 2 │ interface B { + 3 │ e()?: number; + · ─ + 4 │ } + ╰──── + + × Unexpected token + ╭─[misc/fail/oxc-5955-3.ts:2:18] + 1 │ class A { + 2 │ [key: string]?: number; + · ─ + 3 │ } + ╰──── + × The keyword 'let' is reserved ╭─[misc/fail/oxc.js:3:1] 2 │ diff --git a/tasks/coverage/snapshots/semantic_misc.snap b/tasks/coverage/snapshots/semantic_misc.snap index 909d08a03bae8..902d60ee00a09 100644 --- a/tasks/coverage/snapshots/semantic_misc.snap +++ b/tasks/coverage/snapshots/semantic_misc.snap @@ -1,6 +1,6 @@ semantic_misc Summary: -AST Parsed : 29/29 (100.00%) -Positive Passed: 17/29 (58.62%) +AST Parsed : 30/30 (100.00%) +Positive Passed: 17/30 (56.67%) tasks/coverage/misc/pass/babel-16776-m.js semantic error: Symbol flags mismatch: after transform: SymbolId(0): SymbolFlags(FunctionScopedVariable | Export) @@ -176,6 +176,14 @@ Scope children mismatch: after transform: ScopeId(0): [ScopeId(1), ScopeId(2)] rebuilt : ScopeId(0): [ScopeId(1)] +tasks/coverage/misc/pass/oxc-5955.ts +semantic error: Bindings mismatch: +after transform: ScopeId(0): ["B", "C", "a"] +rebuilt : ScopeId(0): ["B", "a"] +Scope children mismatch: +after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)] +rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)] + tasks/coverage/misc/pass/swc-7187.ts semantic error: Bindings mismatch: after transform: ScopeId(0): ["K"] diff --git a/tasks/coverage/snapshots/transformer_misc.snap b/tasks/coverage/snapshots/transformer_misc.snap index 8558b1b8ba5f7..f3fd86288b0f8 100644 --- a/tasks/coverage/snapshots/transformer_misc.snap +++ b/tasks/coverage/snapshots/transformer_misc.snap @@ -1,3 +1,3 @@ transformer_misc Summary: -AST Parsed : 29/29 (100.00%) -Positive Passed: 29/29 (100.00%) +AST Parsed : 30/30 (100.00%) +Positive Passed: 30/30 (100.00%)