Skip to content

Commit

Permalink
feat(semantic): report no class name error (#2273)
Browse files Browse the repository at this point in the history
closes #2144
  • Loading branch information
Boshen authored Feb 2, 2024
1 parent 2ceba79 commit 28daf83
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
20 changes: 18 additions & 2 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl EarlyErrorJavaScript {
}
}

AstKind::Class(class) => check_class(class, ctx),
AstKind::Class(class) => check_class(class, node, ctx),
AstKind::Super(sup) => check_super(sup, node, ctx),
AstKind::ObjectProperty(prop) => check_object_property(prop, ctx),

Expand Down Expand Up @@ -743,7 +743,7 @@ fn check_for_statement_left<'a>(
}
}

fn check_class(class: &Class, ctx: &SemanticBuilder<'_>) {
fn check_class(class: &Class, node: &AstNode<'_>, ctx: &SemanticBuilder<'_>) {
#[derive(Debug, Error, Diagnostic)]
#[error("Multiple constructor implementations are not allowed.")]
#[diagnostic()]
Expand All @@ -752,8 +752,24 @@ fn check_class(class: &Class, ctx: &SemanticBuilder<'_>) {
#[label("it cannot be redeclared here")] Span,
);

#[derive(Debug, Error, Diagnostic)]
#[error("A class name is required.")]
#[diagnostic()]
struct RequireClassName(#[label] Span);

check_private_identifier(ctx);

if class.is_declaration()
&& class.id.is_none()
&& !matches!(
ctx.nodes.parent_kind(node.id()),
Some(AstKind::ModuleDeclaration(ModuleDeclaration::ExportDefaultDeclaration(_)))
)
{
let start = class.span.start;
ctx.error(RequireClassName(Span::new(start, start + 5)));
}

// ClassBody : ClassElementList
// It is a Syntax Error if PrototypePropertyNameList of ClassElementList contains more than one occurrence of "constructor".
let mut prev_constructor: Option<Span> = None;
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/misc/fail/oxc-2144.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class {}
export class {}
16 changes: 15 additions & 1 deletion tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_misc Summary:
AST Parsed : 10/10 (100.00%)
Positive Passed: 10/10 (100.00%)
Negative Passed: 6/6 (100.00%)
Negative Passed: 7/7 (100.00%)

× Unexpected token
╭─[fail/oxc-169.js:1:1]
Expand All @@ -24,6 +24,20 @@ Negative Passed: 6/6 (100.00%)
3 │ }
╰────

× A class name is required.
╭─[fail/oxc-2144.js:1:1]
1 │ class {}
· ─────
2export class {}
╰────

× A class name is required.
╭─[fail/oxc-2144.js:2:8]
1 │ class {}
2export class {}
· ─────
╰────

× Unexpected `?` operator
╭─[fail/oxc-2253.ts:1:8]
1const a? = "A"
Expand Down
11 changes: 9 additions & 2 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_typescript Summary:
AST Parsed : 5239/5243 (99.92%)
Positive Passed: 5232/5243 (99.79%)
Negative Passed: 1035/4879 (21.21%)
Negative Passed: 1036/4879 (21.23%)
Expect Syntax Error: "compiler/ClassDeclaration10.ts"
Expect Syntax Error: "compiler/ClassDeclaration11.ts"
Expect Syntax Error: "compiler/ClassDeclaration13.ts"
Expand Down Expand Up @@ -664,7 +664,6 @@ Expect Syntax Error: "compiler/expandoFunctionContextualTypesNoValue.ts"
Expect Syntax Error: "compiler/exportAlreadySeen.ts"
Expect Syntax Error: "compiler/exportAsNamespaceConflict.ts"
Expect Syntax Error: "compiler/exportAssignmentWithExports.ts"
Expect Syntax Error: "compiler/exportClassWithoutName.ts"
Expect Syntax Error: "compiler/exportDeclarationsInAmbientNamespaces2.ts"
Expect Syntax Error: "compiler/exportDeclareClass1.ts"
Expect Syntax Error: "compiler/exportDefaultAlias_excludesEverything.ts"
Expand Down Expand Up @@ -6326,6 +6325,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
· ──────
╰────

× A class name is required.
╭─[compiler/exportClassWithoutName.ts:3:8]
2 │ //@target: es2015
3 │ export class {
· ─────
4 │ }
╰────

× Unexpected token
╭─[compiler/exportDeclarationInInternalModule.ts:17:19]
16 │
Expand Down

0 comments on commit 28daf83

Please sign in to comment.