From 8bb8adc9f390e2357b2f0ef86da683fcaeb41999 Mon Sep 17 00:00:00 2001 From: Faysal Aberkane Date: Fri, 20 Nov 2026 23:25:11 +0000 Subject: [PATCH] Keep outer attributes on the first file item Collect only leading inner attributes on the file and recognize files that contain inner attributes without items. --- crates/moxy-ast/src/file.rs | 20 +++++++++++++++++--- tests/attributes.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/moxy-ast/src/file.rs b/crates/moxy-ast/src/file.rs index 9f9e699e..890f373e 100644 --- a/crates/moxy-ast/src/file.rs +++ b/crates/moxy-ast/src/file.rs @@ -38,13 +38,19 @@ impl ToTokens for File { impl Parse for File { fn peek(cursor: Cursor<'_>) -> bool { - Shebang::peek(cursor) || Item::peek(cursor) + Shebang::peek(cursor) || peek_inner_attr(cursor) || Item::peek(cursor) } fn parse(parser: &Parser) -> Result { + let shebang = <_ as Parse>::parse(parser)?; + let mut attrs = Attributes::default(); + while peek_inner_attr(parser.cursor()) { + attrs.push(Attribute::parse(parser)?); + } + Ok(Self { - shebang: <_ as Parse>::parse(parser)?, - attrs: <_ as Parse>::parse(parser)?, + shebang, + attrs, items: <_ as Parse>::parse(parser)?, }) } @@ -54,6 +60,10 @@ impl Parse for File { cursor = Shebang::skip(cursor)?; } + while peek_inner_attr(cursor) { + cursor = Attribute::skip(cursor)?; + } + while let Some(next) = Item::skip(cursor) { cursor = next; } @@ -62,6 +72,10 @@ impl Parse for File { } } +fn peek_inner_attr(cursor: Cursor<'_>) -> bool { + AttrStyle::peek(cursor) && ::peek(cursor.offset(1)) +} + #[derive(Clone)] #[cfg_attr(feature = "derives", derive(Debug, PartialEq, Eq))] #[cfg_attr(feature = "serde", derive(serde::Serialize))] diff --git a/tests/attributes.rs b/tests/attributes.rs index 5a116ab7..c1774055 100644 --- a/tests/attributes.rs +++ b/tests/attributes.rs @@ -1,5 +1,33 @@ -use moxy::ast::{Item, Meta}; -use moxy::token::Spanner; +use moxy::ast::{File, Item, Meta, Parse, Parser}; +use moxy::token::{Spanner, TokenStream}; + +#[test] +fn file_and_item_attributes_stay_with_their_declarations() { + let source = "#![allow(dead_code)] #[inline] fn f() {}"; + let file: File = moxy::parse!(source).unwrap(); + let function = file.items[0].as_fn().unwrap(); + + assert_eq!(file.items.len(), 1); + assert_eq!(file.attrs.len(), 1); + assert!(file.attrs[0].style.is_inner()); + assert!(file.attrs[0].path.is_ident("allow")); + assert_eq!(function.attrs.len(), 1); + assert!(function.attrs[0].style.is_outer()); + assert!(function.attrs[0].path.is_ident("inline")); +} + +#[test] +fn a_file_can_contain_only_inner_attributes() { + let source = "#![allow(dead_code)]"; + let tokens: TokenStream = source.parse().unwrap(); + let parser = Parser::from_tokens(&tokens); + let file: File = moxy::parse!(source).unwrap(); + + assert_eq!(file.attrs.len(), 1); + assert!(file.items.is_empty()); + assert!(File::peek(parser.cursor())); + assert!(File::skip(parser.cursor()).unwrap().is_empty()); +} #[test] fn outer_attributes_are_attached_to_the_declaration_and_rendered() {