Dunqing 2024-04-27 22:08:02 +08:00 committed by GitHub
parent 1f12aee149
commit c3d8a85eb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 8 deletions

View file

@ -25,6 +25,7 @@ impl EarlyErrorTypeScript {
check_ts_type_parameter_declaration(declaration, ctx);
}
AstKind::TSModuleDeclaration(decl) => check_ts_module_declaration(decl, ctx),
AstKind::TSEnumDeclaration(decl) => check_ts_enum_declaration(decl, ctx),
_ => {}
}
}
@ -158,3 +159,32 @@ fn check_ts_module_declaration<'a>(decl: &TSModuleDeclaration<'a>, ctx: &Semanti
}
}
}
fn check_ts_enum_declaration(decl: &TSEnumDeclaration<'_>, ctx: &SemanticBuilder<'_>) {
#[derive(Debug, Error, Diagnostic)]
#[error("Enum member must have initializer.")]
#[diagnostic()]
struct EnumMemberMustHaveInitializer(#[label] Span);
let mut need_initializer = false;
decl.members.iter().for_each(|member| {
if let Some(initializer) = &member.initializer {
need_initializer = !matches!(
initializer,
// A = 1
Expression::NumericLiteral(_)
// B = A
| Expression::Identifier(_)
// C = E.D
| Expression::MemberExpression(_)
// D = 1 + 2
| Expression::BinaryExpression(_)
// E = -1
| Expression::UnaryExpression(_)
);
} else if need_initializer {
ctx.error(EnumMemberMustHaveInitializer(member.span));
}
});
}

View file

@ -983,14 +983,16 @@ var A = (A => {
```
# babel/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/input.ts
```typescript
var E = (E => {
const a = Math.sin(1);
E[E['a'] = a] = 'a';
const b = 1 + a;
E[E['b'] = b] = 'b';
return E;
})(E || {});
```error
× Enum member must have initializer.
╭─[babel/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/input.ts:3:5]
2 │ a = Math.sin(1),
3 │ b,
· ─
4 │ }
╰────
```