Skip to content

Commit

Permalink
fix(semantic): ? on variable declaration type annotations is a synt…
Browse files Browse the repository at this point in the history
…ax error (#5956)

Closes #5955
  • Loading branch information
DonIsaac committed Sep 22, 2024
1 parent 05f592b commit f1551d6
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/checker/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/misc/fail/oxc-5955-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const x?: number = 1;

4 changes: 4 additions & 0 deletions tasks/coverage/misc/fail/oxc-5955-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

interface B {
e()?: number;
}
3 changes: 3 additions & 0 deletions tasks/coverage/misc/fail/oxc-5955-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
[key: string]?: number;
}
7 changes: 7 additions & 0 deletions tasks/coverage/misc/pass/oxc-5955.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const a = (b?: number) => b;
class B {
c?: number = 1;
}
interface C {
d?: number;
}
4 changes: 2 additions & 2 deletions tasks/coverage/snapshots/codegen_misc.snap
Original file line number Diff line number Diff line change
@@ -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%)
29 changes: 26 additions & 3 deletions tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -244,6 +244,29 @@ Negative Passed: 17/17 (100.00%)
· ───────
╰────

× Unexpected `?` operator
╭─[misc/fail/oxc-5955-1.ts:1:8]
1const x?: number = 1;
· ▲
2
╰────

× Unexpected token
╭─[misc/fail/oxc-5955-2.ts:3:8]
2interface B {
3 │ e()?: number;
· ─
4 │ }
╰────

× Unexpected token
╭─[misc/fail/oxc-5955-3.ts:2:18]
1class A {
2 │ [key: string]?: number;
· ─
3 │ }
╰────

× The keyword 'let' is reserved
╭─[misc/fail/oxc.js:3:1]
2
Expand Down
12 changes: 10 additions & 2 deletions tasks/coverage/snapshots/semantic_misc.snap
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/snapshots/transformer_misc.snap
Original file line number Diff line number Diff line change
@@ -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%)

0 comments on commit f1551d6

Please sign in to comment.