|
147 | 147 | //! * Implement [`Generator::attrs`] / [`Derive::attrs`] to declare the generator's custom attributes. |
148 | 148 | //! * Implement [`Generator::parse_attr`] / [`Derive::parse_attr`] to parse those attributes |
149 | 149 | //! and mutate the "extension" types in [`Schema`] as required. |
150 | | -//! * Add the attributes' names to the list on `ast_derive` in `crates/oxc_ast_macros/src/lib.rs`. |
151 | 150 | //! |
152 | 151 | //! #### Attributes |
153 | 152 | //! |
|
170 | 169 | //! [`AttrLocation`]: parse::attr::AttrLocation |
171 | 170 | //! [`AttrPart`]: parse::attr::AttrPart |
172 | 171 |
|
173 | | -use std::fmt::Write; |
| 172 | +use std::{fmt::Write, fs}; |
174 | 173 |
|
175 | 174 | use bpaf::{Bpaf, Parser}; |
176 | 175 | use rayon::prelude::*; |
@@ -209,6 +208,9 @@ static SOURCE_PATHS: &[&str] = &[ |
209 | 208 | /// Path to `oxc_ast` crate |
210 | 209 | const AST_CRATE: &str = "crates/oxc_ast"; |
211 | 210 |
|
| 211 | +/// Path to `oxc_ast_macros` crate's `lib.rs` file |
| 212 | +const AST_MACROS_LIB_PATH: &str = "crates/oxc_ast_macros/src/lib.rs"; |
| 213 | + |
212 | 214 | /// Path to write TS type definitions to |
213 | 215 | const TYPESCRIPT_DEFINITIONS_PATH: &str = "npm/oxc-types/types.d.ts"; |
214 | 216 |
|
@@ -288,6 +290,9 @@ fn main() { |
288 | 290 |
|
289 | 291 | logln!("All Derives and Generators... Done!"); |
290 | 292 |
|
| 293 | + // Edit `lib.rs` in `oxc_ast_macros` crate |
| 294 | + outputs.push(generate_updated_proc_macro(&codegen)); |
| 295 | + |
291 | 296 | // Add CI filter file to outputs |
292 | 297 | outputs.sort_unstable_by(|o1, o2| o1.path.cmp(&o2.path)); |
293 | 298 | outputs.push(generate_ci_filter(&outputs)); |
@@ -326,3 +331,26 @@ fn generate_ci_filter(outputs: &[RawOutput]) -> RawOutput { |
326 | 331 |
|
327 | 332 | Output::Yaml { path: GITHUB_WATCH_LIST_PATH.to_string(), code }.into_raw(file!()) |
328 | 333 | } |
| 334 | + |
| 335 | +/// Update the list of helper attributes for `Ast` derive proc macro in `oxc_ast_macros` crate |
| 336 | +/// to include all attrs which generators/derives utilize. |
| 337 | +/// |
| 338 | +/// Unfortunately we can't add a separate generated file for this, as proc macros can only be declared |
| 339 | +/// in the main `lib.rs` of a proc macro crate. So we have to edit the existing file. |
| 340 | +fn generate_updated_proc_macro(codegen: &Codegen) -> RawOutput { |
| 341 | + // Get all attrs which derives/generators use |
| 342 | + let mut attrs = codegen.attrs(); |
| 343 | + attrs.push("generate_derive"); |
| 344 | + attrs.sort_unstable(); |
| 345 | + let attrs = attrs.join(", "); |
| 346 | + |
| 347 | + // Load `oxc_ast_macros` crate's `lib.rs` file. |
| 348 | + // Substitute list of used attrs into `#[proc_macro_derive(Ast, attributes(...))]`. |
| 349 | + let code = fs::read_to_string(AST_MACROS_LIB_PATH).unwrap(); |
| 350 | + let (start, end) = code.split_once("#[proc_macro_derive(").unwrap(); |
| 351 | + let (_, end) = end.split_once(")]").unwrap(); |
| 352 | + assert!(end.starts_with("\npub fn ast_derive(")); |
| 353 | + let code = format!("{start}#[proc_macro_derive(Ast, attributes({attrs}))]{end}"); |
| 354 | + |
| 355 | + Output::RustString { path: AST_MACROS_LIB_PATH.to_string(), code }.into_raw("") |
| 356 | +} |
0 commit comments