mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
feat(semantic): report that enum member must have initializer (#3113)
See https://www.typescriptlang.org/play/?target=99#code/KYOwrgtgBAglDeAoKKoEMoF4oFk0BcALAOgGcBLEACgEYBKAGmVQCMmBfRRUSKAIQTMUGbACIaAJgDMopqihtEnRABtg+KADMwIAMZYoVOlgB8USVIDc3cNADCg+SK07dRuaw6IgA
This commit is contained in:
parent
1f12aee149
commit
c3d8a85eb5
2 changed files with 40 additions and 8 deletions
|
|
@ -25,6 +25,7 @@ impl EarlyErrorTypeScript {
|
||||||
check_ts_type_parameter_declaration(declaration, ctx);
|
check_ts_type_parameter_declaration(declaration, ctx);
|
||||||
}
|
}
|
||||||
AstKind::TSModuleDeclaration(decl) => check_ts_module_declaration(decl, 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));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -983,14 +983,16 @@ var A = (A => {
|
||||||
```
|
```
|
||||||
|
|
||||||
# babel/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/input.ts
|
# babel/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/input.ts
|
||||||
```typescript
|
```error
|
||||||
var E = (E => {
|
|
||||||
const a = Math.sin(1);
|
× Enum member must have initializer.
|
||||||
E[E['a'] = a] = 'a';
|
╭─[babel/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/input.ts:3:5]
|
||||||
const b = 1 + a;
|
2 │ a = Math.sin(1),
|
||||||
E[E['b'] = b] = 'b';
|
3 │ b,
|
||||||
return E;
|
· ─
|
||||||
})(E || {});
|
4 │ }
|
||||||
|
╰────
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue