feat(parser): report error on infer outside conditional type (#19879)

This commit is contained in:
camc314 committed 2026-03-02 05:24:20 +00:00
1 parent 5141bc263e
commit 733d6dcf90
6 files changed
+78 -7

No files matched your search

+1
View File
@@ -122,6 +122,7 @@ pub fn check<'a>(kind: AstKind<'a>, ctx: &SemanticBuilder<'a>) {
AstKind::TSGlobalDeclaration(decl) => ts::check_ts_global_declaration(decl, ctx),
AstKind::TSEnumDeclaration(decl) => ts::check_ts_enum_declaration(decl, ctx),
AstKind::TSTypeAliasDeclaration(decl) => ts::check_ts_type_alias_declaration(decl, ctx),
AstKind::TSInferType(infer_type) => ts::check_ts_infer_type(infer_type, ctx),
AstKind::TSImportEqualsDeclaration(decl) => {
ts::check_ts_import_equals_declaration(decl, ctx);
}
@@ -73,6 +73,19 @@ pub fn check_ts_type_alias_declaration<'a>(
check_type_name_is_reserved(&decl.id, ctx, "Type alias");
}
pub fn check_ts_infer_type<'a>(infer_type: &TSInferType<'a>, ctx: &SemanticBuilder<'a>) {
let is_in_conditional_extends_clause =
ctx.nodes.ancestor_kinds(ctx.current_node_id).any(|kind| {
kind.as_ts_conditional_type().is_some_and(|conditional| {
conditional.extends_type.span().contains_inclusive(infer_type.span)
})
});
if !is_in_conditional_extends_clause {
ctx.error(diagnostics::infer_declaration_only_permitted_in_extends_clause(infer_type.span));
}
}
pub fn check_formal_parameters(params: &FormalParameters, ctx: &SemanticBuilder<'_>) {
if params.kind == FormalParameterKind::Signature && params.items.len() > 1 {
check_duplicate_bound_names(params, ctx);
+10
View File
@@ -354,6 +354,16 @@ pub fn import_alias_cannot_use_import_type(span: Span) -> OxcDiagnostic {
ts_error("1392", "An import alias cannot use 'import type'").with_label(span)
}
/// 'infer' declarations are only permitted in the 'extends' clause of a conditional type. (1338)
#[cold]
pub fn infer_declaration_only_permitted_in_extends_clause(span: Span) -> OxcDiagnostic {
ts_error(
"1338",
"'infer' declarations are only permitted in the 'extends' clause of a conditional type.",
)
.with_label(span)
}
/// - Abstract properties can only appear within an abstract class. (1253)
/// - Abstract methods can only appear within an abstract class. (1244)
#[cold]
+9 -1
View File
@@ -2,7 +2,7 @@ commit: 308c8d85
parser_babel Summary:
AST Parsed : 2227/2227 (100.00%)
Positive Passed: 2214/2227 (99.42%)
Positive Passed: 2213/2227 (99.37%)
Negative Passed: 1676/1692 (99.05%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/cast/unparenthesized-assert-and-assign/input.ts
@@ -296,6 +296,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
32 │ }
╰────
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/literal-string-4/input.ts
× TS(1338): 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/literal-string-4/input.ts:1:15]
1 │ let x: `foo-${infer bar}`;
· ─────────
╰────
× Invalid function declaration
╭─[babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.1-sloppy-labeled-functions-if-body/input.js:1:11]
@@ -3,7 +3,7 @@ commit: 34725489
parser_typescript Summary:
AST Parsed : 9831/9834 (99.97%)
Positive Passed: 9820/9834 (99.86%)
Negative Passed: 1528/2578 (59.27%)
Negative Passed: 1530/2578 (59.35%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration3.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration4.ts
@@ -1980,8 +1980,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/any
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/conditional/conditionalTypes1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/conditional/inferTypes1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/intersection/intersectionReduction.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/intersection/intersectionReductionStrict.ts
@@ -1992,8 +1990,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/key
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/literal/templateLiteralTypes1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/mapped/mappedTypeErrors.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/mapped/mappedTypeWithAny.ts
@@ -26680,6 +26676,38 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
3 │ name = 'twelve'
╰────
× TS(1338): 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
╭─[typescript/tests/cases/conformance/types/conditional/inferTypes1.ts:82:12]
81 │
82 │ type T60 = infer U; // Error
· ───────
83 │ type T61<T> = (infer A) extends infer B ? infer C : infer D; // Error
╰────
× TS(1338): 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
╭─[typescript/tests/cases/conformance/types/conditional/inferTypes1.ts:83:16]
82 │ type T60 = infer U; // Error
83 │ type T61<T> = (infer A) extends infer B ? infer C : infer D; // Error
· ───────
84 │ type T62<T> = U extends (infer U)[] ? U : U; // Error
╰────
× TS(1338): 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
╭─[typescript/tests/cases/conformance/types/conditional/inferTypes1.ts:83:43]
82 │ type T60 = infer U; // Error
83 │ type T61<T> = (infer A) extends infer B ? infer C : infer D; // Error
· ───────
84 │ type T62<T> = U extends (infer U)[] ? U : U; // Error
╰────
× TS(1338): 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
╭─[typescript/tests/cases/conformance/types/conditional/inferTypes1.ts:83:53]
82 │ type T60 = infer U; // Error
83 │ type T61<T> = (infer A) extends infer B ? infer C : infer D; // Error
· ───────
84 │ type T62<T> = U extends (infer U)[] ? U : U; // Error
╰────
× TS(1141): String literal expected.
╭─[typescript/tests/cases/conformance/types/import/importTypeGeneric.ts:1:67]
1 │ export function getFooFrom<T extends "./a" | "./b">(v: T): import(T).Foo {
@@ -26721,6 +26749,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
2 │ const a = import<string, number>
╰────
× TS(1338): 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
╭─[typescript/tests/cases/conformance/types/literal/templateLiteralTypes1.ts:165:15]
164 │
165 │ type TV1 = `${infer X}`;
· ───────
166 │
╰────
× Identifier `T` has already been declared
╭─[typescript/tests/cases/conformance/types/localTypes/localTypes4.ts:18:16]
17 │ // Type parameters and top-level local types are in same declaration space
+4 -1
View File
@@ -2,7 +2,7 @@ commit: 308c8d85
semantic_babel Summary:
AST Parsed : 2227/2227 (100.00%)
Positive Passed: 2027/2227 (91.02%)
Positive Passed: 2026/2227 (90.97%)
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/decorators/decorators-after-export/input.js
Symbol span mismatch for "C":
after transform: SymbolId(0): Span { start: 65, end: 66 }
@@ -1392,6 +1392,9 @@ Unresolved references mismatch:
after transform: ["bar"]
rebuilt : []
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/literal-string-4/input.ts
'infer' declarations are only permitted in the 'extends' clause of a conditional type.
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/mapped-as/input.ts
Unresolved references mismatch:
after transform: ["Exclude", "NewKeyType"]