From 9ca13d040d9767dffd56dfa9d0015b6cc11b3b58 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 5 Feb 2024 16:05:51 +0800 Subject: [PATCH] feat(semantic): report type parameter list cannot be empty (#2315) --- crates/oxc_semantic/src/checker/typescript.rs | 16 ++++ tasks/coverage/parser_babel.snap | 16 +++- tasks/coverage/parser_typescript.snap | 83 ++++++++++++++++++- 3 files changed, 109 insertions(+), 6 deletions(-) diff --git a/crates/oxc_semantic/src/checker/typescript.rs b/crates/oxc_semantic/src/checker/typescript.rs index ab2fe522c..4b38d582f 100644 --- a/crates/oxc_semantic/src/checker/typescript.rs +++ b/crates/oxc_semantic/src/checker/typescript.rs @@ -21,11 +21,27 @@ impl EarlyErrorTypeScript { AstKind::SimpleAssignmentTarget(target) => check_simple_assignment_target(target, ctx), AstKind::FormalParameters(params) => check_formal_parameters(params, ctx), AstKind::ArrayPattern(pattern) => check_array_pattern(pattern, ctx), + AstKind::TSTypeParameterDeclaration(declaration) => { + check_ts_type_parameter_declaration(declaration, ctx); + } _ => {} } } } +fn check_ts_type_parameter_declaration( + declaration: &TSTypeParameterDeclaration<'_>, + ctx: &SemanticBuilder<'_>, +) { + #[derive(Debug, Error, Diagnostic)] + #[error("Type parameter list cannot be empty.")] + #[diagnostic()] + struct EmptyTypeParameterList(#[label] Span); + if declaration.params.is_empty() { + ctx.error(EmptyTypeParameterList(declaration.span)); + } +} + #[allow(clippy::cast_possible_truncation)] fn check_variable_declarator(decl: &VariableDeclarator, ctx: &SemanticBuilder<'_>) { #[derive(Debug, Error, Diagnostic)] diff --git a/tasks/coverage/parser_babel.snap b/tasks/coverage/parser_babel.snap index 3d33445ba..0f83d32ad 100644 --- a/tasks/coverage/parser_babel.snap +++ b/tasks/coverage/parser_babel.snap @@ -1,7 +1,7 @@ parser_babel Summary: AST Parsed : 2090/2096 (99.71%) Positive Passed: 2086/2096 (99.52%) -Negative Passed: 1358/1500 (90.53%) +Negative Passed: 1360/1500 (90.67%) Expect Syntax Error: "annex-b/disabled/1.1-html-comments-close/input.js" Expect Syntax Error: "annex-b/disabled/3.1-sloppy-labeled-functions/input.js" Expect Syntax Error: "annex-b/disabled/3.1-sloppy-labeled-functions-if-body/input.js" @@ -61,7 +61,6 @@ Expect Syntax Error: "typescript/class/declare-readonly-field-initializer-w-anno Expect Syntax Error: "typescript/class/duplicate-modifier-1/input.ts" Expect Syntax Error: "typescript/class/duplicate-modifier-2/input.ts" Expect Syntax Error: "typescript/class/duplicates-accessibility/input.ts" -Expect Syntax Error: "typescript/class/empty-type-parameters/input.ts" Expect Syntax Error: "typescript/class/generator-method-with-modifiers/input.ts" Expect Syntax Error: "typescript/class/index-signature-errors/input.ts" Expect Syntax Error: "typescript/class/invalid-modifiers-order/input.ts" @@ -92,7 +91,6 @@ Expect Syntax Error: "typescript/expect-plugin/export-type/input.js" Expect Syntax Error: "typescript/expect-plugin/export-type-named/input.js" Expect Syntax Error: "typescript/export/double-declare/input.ts" Expect Syntax Error: "typescript/export/equals-in-script/input.ts" -Expect Syntax Error: "typescript/function/empty-type-parameters/input.ts" Expect Syntax Error: "typescript/import/equals-in-script/input.ts" Expect Syntax Error: "typescript/import/equals-require-in-script/input.ts" Expect Syntax Error: "typescript/import/export-import-type/input.ts" @@ -9733,6 +9731,12 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts" ╰──── help: Try insert a semicolon here + × Type parameter list cannot be empty. + ╭─[typescript/class/empty-type-parameters/input.ts:1:8] + 1 │ class C<> {} + · ── + ╰──── + × Expected `{` but found `EOF` ╭─[typescript/class/extends-empty/input.ts:2:1] 2 │ } @@ -9880,6 +9884,12 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts" · ─── ╰──── + × Type parameter list cannot be empty. + ╭─[typescript/function/empty-type-parameters/input.ts:1:13] + 1 │ function foo<>() {} + · ── + ╰──── + × A parameter property is only allowed in a constructor implementation. ╭─[typescript/function/parameter-properties/input.ts:3:3] 2 │ readonly r, diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index cdfd07ef3..2247fc96c 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -1,7 +1,7 @@ parser_typescript Summary: AST Parsed : 5239/5243 (99.92%) Positive Passed: 5232/5243 (99.79%) -Negative Passed: 1036/4879 (21.23%) +Negative Passed: 1038/4879 (21.27%) Expect Syntax Error: "compiler/ClassDeclaration10.ts" Expect Syntax Error: "compiler/ClassDeclaration11.ts" Expect Syntax Error: "compiler/ClassDeclaration13.ts" @@ -306,7 +306,6 @@ Expect Syntax Error: "compiler/classStaticPropertyAccess.ts" Expect Syntax Error: "compiler/classTypeParametersInStatics.ts" Expect Syntax Error: "compiler/classUsedBeforeInitializedVariables.ts" Expect Syntax Error: "compiler/classWithDuplicateIdentifier.ts" -Expect Syntax Error: "compiler/classWithEmptyTypeParameter.ts" Expect Syntax Error: "compiler/classWithMultipleBaseClasses.ts" Expect Syntax Error: "compiler/classWithOverloadImplementationOfWrongName.ts" Expect Syntax Error: "compiler/classWithOverloadImplementationOfWrongName2.ts" @@ -3100,7 +3099,6 @@ Expect Syntax Error: "conformance/parser/ecmascript5/ComputedPropertyNames/parse Expect Syntax Error: "conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName8.ts" Expect Syntax Error: "conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName9.ts" Expect Syntax Error: "conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration10.ts" -Expect Syntax Error: "conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts" Expect Syntax Error: "conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration2.ts" Expect Syntax Error: "conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration4.ts" Expect Syntax Error: "conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration6.ts" @@ -4821,6 +4819,13 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" ╰──── help: Try insert a semicolon here + × Type parameter list cannot be empty. + ╭─[compiler/classWithEmptyTypeParameter.ts:1:8] + 1 │ class C<> { + · ── + 2 │ } + ╰──── + × Cannot assign to 'arguments' in strict mode ╭─[compiler/collisionArgumentsClassConstructor.ts:3:31] 2 │ class c1 { @@ -8148,6 +8153,70 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" · ─ ╰──── + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:2:14] + 1 │ class C { + 2 │ constructor<>() { } + · ── + 3 │ constructor<> () { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:3:14] + 2 │ constructor<>() { } + 3 │ constructor<> () { } + · ── + 4 │ constructor <>() { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:4:15] + 3 │ constructor<> () { } + 4 │ constructor <>() { } + · ── + 5 │ constructor <> () { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:5:15] + 4 │ constructor <>() { } + 5 │ constructor <> () { } + · ── + 6 │ constructor< >() { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:6:14] + 5 │ constructor <> () { } + 6 │ constructor< >() { } + · ─── + 7 │ constructor< > () { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:7:14] + 6 │ constructor< >() { } + 7 │ constructor< > () { } + · ─── + 8 │ constructor < >() { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:8:15] + 7 │ constructor< > () { } + 8 │ constructor < >() { } + · ─── + 9 │ constructor < > () { } + ╰──── + + × Type parameter list cannot be empty. + ╭─[compiler/parserConstructorDeclaration12.ts:9:15] + 8 │ constructor < >() { } + 9 │ constructor < > () { } + · ─── + 10 │ } + ╰──── + × Multiple constructor implementations are not allowed. ╭─[compiler/parserConstructorDeclaration12.ts:2:3] 1 │ class C { @@ -16202,6 +16271,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" · ╰── `,` expected ╰──── + × Type parameter list cannot be empty. + ╭─[conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts:2:14] + 1 │ class C { + 2 │ constructor<>() { } + · ── + 3 │ } + ╰──── + × Expected a semicolon or an implicit semicolon after a statement, but found none ╭─[conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration3.ts:2:9] 1 │ class C {