fix(semantic): ? on variable declaration type annotations is a syntax error (#5956)

Closes #5955
This commit is contained in:
DonIsaac 2024-09-22 00:01:47 +00:00
parent 05f592b834
commit f1551d64bc
9 changed files with 57 additions and 10 deletions

View file

@ -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)));

View file

@ -0,0 +1,2 @@
const x?: number = 1;

View file

@ -0,0 +1,4 @@
interface B {
e()?: number;
}

View file

@ -0,0 +1,3 @@
class A {
[key: string]?: number;
}

View file

@ -0,0 +1,7 @@
const a = (b?: number) => b;
class B {
c?: number = 1;
}
interface C {
d?: number;
}

View file

@ -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%)

View file

@ -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 │

View file

@ -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"]

View file

@ -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%)