refactor(ast_tools/estree): order type and span fields first by default (#11361)

Order `type` field 1st, and `span` field 2nd in ESTree and TS-ESTree ASTs.
This commit is contained in:
overlookmotel committed 2025-05-29 02:20:54 +00:00
1 parent 75ee3a5c47
commit 1d1ebd6287
3 files changed
+21 -5

No files matched your search

+1 -1
View File
@@ -87,7 +87,7 @@ impl<'a> ModuleRecord<'a> {
#[ast]
#[derive(Debug, Clone, PartialEq, Eq)]
#[generate_derive(ESTree)]
#[estree(no_type, no_ts_def)]
#[estree(no_type, no_ts_def, field_order(name, span))]
pub struct NameSpan<'a> {
/// Name
#[estree(rename = "value")]
+1 -1
View File
@@ -83,7 +83,7 @@ impl From<Severity> for ErrorSeverity {
#[ast]
#[generate_derive(ESTree)]
#[estree(no_type, no_ts_def)]
#[estree(no_type, no_ts_def, field_order(message, span))]
pub struct ErrorLabel<'a> {
pub message: Option<Atom<'a>>,
pub span: Span,
+19 -3
View File
@@ -261,16 +261,32 @@ fn prepare_field_orders(schema: &mut Schema, estree_derive_id: DeriveId) {
if struct_def.estree.field_indices.is_empty() {
// No field order specified with `#[estree(field_order(...))]`.
// Default field order is definition order, with `#[estree(add_fields(...)]` extra fields
// added on end in order.
// Default field order is:
// 1. `type` field (if present)
// 2. `span` field (if present)
// 3. Struct fields, in definition order.
// 4. Extra fields (`#[estree(add_fields(...)]`), in order.
let mut field_indices = vec![];
let mut type_field_index = None;
let mut span_field_index = None;
for (field_index, field) in struct_def.fields.iter().enumerate() {
if !should_skip_field(field, schema) {
let field_index = u8::try_from(field_index).unwrap();
field_indices.push(field_index);
match field.name() {
"type" => type_field_index = Some(field_index),
"span" => span_field_index = Some(field_index),
_ => field_indices.push(field_index),
}
}
}
if let Some(span_field_index) = span_field_index {
field_indices.insert(0, span_field_index);
}
if let Some(type_field_index) = type_field_index {
field_indices.insert(0, type_field_index);
}
if !struct_def.estree.add_fields.is_empty() {
let first_index = u8::try_from(struct_def.fields.len()).unwrap();
let last_index =