fix(parser): reject optional class members in JavaScript (#28439)

Reject TypeScript optional class members in JavaScript with a diagnostic
on the question mark. The existing TypeScript compiler fixture covers
the rejection.

Validated with scoped Rust checks, full conformance, parser bindings,
allocation tracking.

AI-assisted (Codex).
This commit is contained in:
Boshen authored and GitHub committed 2026-10-30 11:35:35 +08:00
1 parent 5b9a7bc735
commit a1e7e5f13e
3 files changed
+25 -3

No files matched your search

+5
View File
@@ -1186,6 +1186,11 @@ parser_diagnostics! {
.with_label(span)
};
optional_class_member_in_ts(span: Span) => {
OxcDiagnostic::error("Optional class members can only be used in TypeScript files.")
.with_label(span)
};
parameter_modifiers_in_ts(modifier: Modifier, allowed: Option<ModifierKinds>) => {
ts_error("8012", "Parameter modifiers can only be used in TypeScript files.")
.with_label(modifier.span())
+3
View File
@@ -629,6 +629,9 @@ impl<'a, C: Config> ParserImpl<'a, C> {
let optional_span = (cur_token.kind() == Kind::Question).then(|| {
let span = cur_token.span();
self.bump_any();
if !self.is_ts {
self.error(diagnostics::optional_class_member_in_ts(span));
}
span
});
@@ -3,7 +3,7 @@ commit: 8a62bbe7
parser_typescript Summary:
AST Parsed : 9859/9868 (99.91%)
Positive Passed: 9849/9868 (99.81%)
Negative Passed: 1712/2807 (60.99%)
Negative Passed: 1713/2807 (61.03%)
Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler/FunctionDeclaration3.ts
Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler/FunctionDeclaration4.ts
@@ -586,8 +586,6 @@ Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler
Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler/jsFileCompilationDuplicateFunctionImplementationFileOrderReversed.ts
Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler/jsFileCompilationOptionalClassElementSyntaxOfClass.ts
Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler/jsFunctionWithPrototypeNoErrorTruncationNoCrash.ts
Expect Syntax Error: tasks/coverage/typescript/tsc/testdata/tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts
@@ -9102,6 +9100,22 @@ Expect to Parse: tasks/coverage/typescript/tsc/testdata/tests/cases/conformance/
╰────
help: Try inserting a semicolon here
× Optional class members can only be used in TypeScript files.
╭─[typescript/tsc/testdata/tests/cases/compiler/jsFileCompilationOptionalClassElementSyntaxOfClass.ts:2:8]
1 │ class C {
2 │ foo?() {
· ─
3 │ }
╰────
× Optional class members can only be used in TypeScript files.
╭─[typescript/tsc/testdata/tests/cases/compiler/jsFileCompilationOptionalClassElementSyntaxOfClass.ts:4:8]
3 │ }
4 │ bar? = 1;
· ─
5 │ }
╰────
× Expected `,` or `)` but found `?`
╭─[typescript/tsc/testdata/tests/cases/compiler/jsFileCompilationOptionalParameter.ts:1:13]
1 │ function F(p?) { }